mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import { INode } from '~/redux/types';
|
|
import { useCallback } from 'react';
|
|
import { apiLockNode, apiPostNodeHeroic, apiPostNodeLike } from '~/api/node';
|
|
import { showErrorToast } from '~/utils/errors/showToast';
|
|
|
|
export const useNodeActions = (node: INode, update: (node: Partial<INode>) => Promise<unknown>) => {
|
|
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 { onLike, onStar, onLock };
|
|
};
|