mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
fixed node actions
This commit is contained in:
parent
e8effb92f1
commit
0bc2ff250f
13 changed files with 107 additions and 201 deletions
|
@ -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