1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 12:56:41 +07:00

removed lab reducer

This commit is contained in:
Fedor Katurov 2022-01-05 15:57:29 +07:00
parent e24ea4afeb
commit 2b7b756212
26 changed files with 242 additions and 369 deletions

View file

@ -0,0 +1,85 @@
import useSWRInfinite from 'swr/infinite';
import { KeyLoader } from 'swr';
import { GetLabNodesRequest, ILabNode } from '~/types/lab';
import { getLabNodes } from '~/api/lab';
import { flatten, last, uniqBy } from 'ramda';
import { useLabStore } from '~/store/lab/useLabStore';
import { useCallback, useEffect } from 'react';
import { INode } from '~/redux/types';
import { useUser } from '~/hooks/user/userUser';
const getKey: (isUser: boolean) => KeyLoader<ILabNode[]> = isUser => (index, prev) => {
if (!isUser) return null;
if (index > 0 && !prev?.length) return null;
const lastNode = last(prev || []);
if (!lastNode && index > 0) {
return null;
}
const props: GetLabNodesRequest = {
after: lastNode?.node.commented_at || lastNode?.node.created_at,
};
return JSON.stringify(props);
};
const parseKey = (key: string): GetLabNodesRequest => {
try {
return JSON.parse(key);
} catch (error) {
return {};
}
};
export const useGetLabNodes = () => {
const labStore = useLabStore();
const { is_user } = useUser();
const { data, isValidating, size, setSize, mutate } = useSWRInfinite(
getKey(is_user),
async (key: string) => {
const result = await getLabNodes(parseKey(key));
return result.nodes;
},
{
fallbackData: [labStore.nodes],
onSuccess: data => labStore.setNodes(flatten(data)),
}
);
const nodes = uniqBy(n => n.node.id, flatten(data || []));
const hasMore = (data?.[size - 1]?.length || 0) >= 1;
const loadMore = useCallback(() => setSize(size + 1), [setSize, size]);
/** prepends list with a node */
const unshift = useCallback(
async (node: ILabNode) => {
await mutate([[node], ...(data || [])]);
},
[data, mutate]
);
/** updates node on cache */
const updateNode = useCallback(
async (nodeId: number, node: Partial<INode>) => {
await mutate(
data?.map(page =>
page.map(it => (it.node.id === nodeId ? { ...it, node: { ...it.node, node } } : it))
)
);
},
[data, mutate]
);
/** purge cache on exit */
useEffect(() => {
if (is_user) {
return;
}
labStore.setNodes([]);
}, [is_user, labStore]);
return { nodes, isLoading: !data && isValidating, hasMore, loadMore, unshift, updateNode };
};