Feat: 댓글 스토어 구현

This commit is contained in:
정현조 2024-09-26 19:47:06 +09:00
parent 2f861b6f94
commit 958fd49365

View File

@ -0,0 +1,43 @@
import { create } from 'zustand';
import { CommentResponse } from '@/types';
interface CommentWithToggle extends CommentResponse {
isOpen?: boolean;
}
interface CommentState {
comments: CommentWithToggle[];
addComment: (comment: CommentResponse) => void;
updateComment: (updatedComment: CommentResponse) => void;
deleteComment: (commentId: number) => void;
toggleComment: (commentId: number) => void;
}
const useCommentStore = create<CommentState>((set) => ({
comments: [],
addComment: (comment) =>
set((state) => ({
comments: [...state.comments, { ...comment, isOpen: false }],
})),
updateComment: (updatedComment) =>
set((state) => ({
comments: state.comments.map((comment) =>
comment.id === updatedComment.id ? { ...updatedComment, isOpen: comment.isOpen } : comment
),
})),
deleteComment: (commentId) =>
set((state) => ({
comments: state.comments.filter((comment) => comment.id !== commentId),
})),
toggleComment: (commentId) =>
set((state) => ({
comments: state.comments.map((comment) =>
comment.id === commentId ? { ...comment, isOpen: !comment.isOpen } : comment
),
})),
}));
export default useCommentStore;