mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-26 05:16:41 +07:00
removed almost all node sagas
This commit is contained in:
parent
f76a5a4798
commit
168ba8cc04
30 changed files with 268 additions and 448 deletions
62
src/hooks/comments/useNodeComments.ts
Normal file
62
src/hooks/comments/useNodeComments.ts
Normal file
|
@ -0,0 +1,62 @@
|
|||
import { useCallback } from 'react';
|
||||
import { IComment } from '~/redux/types';
|
||||
import { useGetComments } from '~/hooks/comments/useGetComments';
|
||||
import { apiLockComment, apiPostComment } from '~/redux/node/api';
|
||||
import { showErrorToast } from '~/utils/errors/showToast';
|
||||
|
||||
export const useNodeComments = (nodeId: number) => {
|
||||
const { comments, isLoading, onLoadMoreComments, hasMore, data, mutate } = useGetComments(nodeId);
|
||||
|
||||
const onDelete = useCallback(
|
||||
async (id: IComment['id'], isLocked: boolean) => {
|
||||
try {
|
||||
const { deleted_at } = await apiLockComment({ id, nodeId, isLocked });
|
||||
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
await mutate(
|
||||
prev =>
|
||||
prev?.map(list =>
|
||||
list.map(comment => (comment.id === id ? { ...comment, deleted_at } : comment))
|
||||
),
|
||||
false
|
||||
);
|
||||
} catch (error) {
|
||||
showErrorToast(error);
|
||||
}
|
||||
},
|
||||
[data, mutate, nodeId]
|
||||
);
|
||||
|
||||
const onEdit = useCallback(
|
||||
async (comment: IComment) => {
|
||||
const result = await apiPostComment({ id: nodeId, data: comment });
|
||||
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Comment was created
|
||||
if (!comment.id) {
|
||||
await mutate(
|
||||
data.map((list, index) => (index === 0 ? [result.comment, ...list] : list)),
|
||||
false
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
await mutate(
|
||||
prev =>
|
||||
prev?.map(list =>
|
||||
list.map(it => (it.id === result.comment.id ? { ...it, ...result.comment } : it))
|
||||
),
|
||||
false
|
||||
);
|
||||
},
|
||||
[data, mutate, nodeId]
|
||||
);
|
||||
|
||||
return { onLoadMoreComments, onDelete, comments, hasMore, isLoading, onEdit };
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue