mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
fixed node actions
This commit is contained in:
parent
e8effb92f1
commit
0bc2ff250f
13 changed files with 107 additions and 201 deletions
|
@ -4,11 +4,13 @@ import React, { createContext, FC, useContext } from 'react';
|
|||
|
||||
export interface NodeContextProps {
|
||||
node: INode;
|
||||
update: (node: Partial<INode>) => Promise<unknown>;
|
||||
isLoading: boolean;
|
||||
}
|
||||
|
||||
export const NodeContext = createContext<NodeContextProps>({
|
||||
node: EMPTY_NODE,
|
||||
update: async () => {},
|
||||
isLoading: false,
|
||||
});
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import { useOnNodeSeen } from '~/utils/hooks/node/useOnNodeSeen';
|
|||
import { apiGetNode } from '~/redux/node/api';
|
||||
import { useCallback } from 'react';
|
||||
import { INode } from '~/redux/types';
|
||||
import { EMPTY_NODE } from '~/redux/node/constants';
|
||||
|
||||
export const useGetNode = (id: number) => {
|
||||
const { data, isValidating, mutate } = useSWR<ApiGetNodeResponse>(API.NODE.GET_NODE(id), () =>
|
||||
|
@ -25,5 +26,5 @@ export const useGetNode = (id: number) => {
|
|||
|
||||
useOnNodeSeen(data?.node);
|
||||
|
||||
return { node: data?.node, isLoading: isValidating && !data, update };
|
||||
return { node: data?.node || EMPTY_NODE, isLoading: isValidating && !data, update };
|
||||
};
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import { INode } from '~/redux/types';
|
||||
import { useCallback } from 'react';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import { nodeLike, nodeLock, nodeStar } from '~/redux/node/actions';
|
||||
import { modalShowDialog } from '~/redux/modal/actions';
|
||||
import { NODE_EDITOR_DIALOGS } from '~/constants/dialogs';
|
||||
import { apiLockNode, apiPostNodeHeroic, apiPostNodeLike } from '~/redux/node/api';
|
||||
import { showErrorToast } from '~/utils/errors/showToast';
|
||||
|
||||
export const useNodeActions = (node: INode) => {
|
||||
export const useNodeActions = (node: INode, update: (node: Partial<INode>) => Promise<unknown>) => {
|
||||
const dispatch = useDispatch();
|
||||
|
||||
const onEdit = useCallback(() => {
|
||||
|
@ -16,9 +17,38 @@ export const useNodeActions = (node: INode) => {
|
|||
dispatch(modalShowDialog(NODE_EDITOR_DIALOGS[node.type]));
|
||||
}, [dispatch, node]);
|
||||
|
||||
const onLike = useCallback(() => dispatch(nodeLike(node.id)), [dispatch, node]);
|
||||
const onStar = useCallback(() => dispatch(nodeStar(node.id)), [dispatch, node]);
|
||||
const onLock = useCallback(() => dispatch(nodeLock(node.id, !node.deleted_at)), [dispatch, node]);
|
||||
const onLike = useCallback(async () => {
|
||||
try {
|
||||
const result = await apiPostNodeLike({ id: node.id });
|
||||
const likeCount = node.like_count || 0;
|
||||
|
||||
if (result.is_liked) {
|
||||
await update({ like_count: likeCount + 1 });
|
||||
} else {
|
||||
await update({ like_count: likeCount - 1 });
|
||||
}
|
||||
} catch (error) {
|
||||
showErrorToast(error);
|
||||
}
|
||||
}, [node.id, node.like_count, update]);
|
||||
|
||||
const onStar = useCallback(async () => {
|
||||
try {
|
||||
const result = await apiPostNodeHeroic({ id: node.id });
|
||||
await update({ is_heroic: result.is_heroic });
|
||||
} catch (error) {
|
||||
showErrorToast(error);
|
||||
}
|
||||
}, [node.id, update]);
|
||||
|
||||
const onLock = useCallback(async () => {
|
||||
try {
|
||||
const result = await apiLockNode({ id: node.id, is_locked: !node.deleted_at });
|
||||
await update({ deleted_at: result.deleted_at });
|
||||
} catch (error) {
|
||||
showErrorToast(error);
|
||||
}
|
||||
}, [node.deleted_at, node.id, update]);
|
||||
|
||||
return { onEdit, onLike, onStar, onLock };
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue