refactor(community): extract comment store and post/comment API adapters
This commit is contained in:
@@ -17,6 +17,8 @@ interface CreatePostInput {
|
||||
interface PostState {
|
||||
posts: Post[]
|
||||
createPost: (input: CreatePostInput) => Post
|
||||
togglePostLike: (postId: string) => void
|
||||
incrementCommentCount: (postId: string) => void
|
||||
}
|
||||
|
||||
export const usePostStore = create<PostState>((set) => ({
|
||||
@@ -45,4 +47,22 @@ export const usePostStore = create<PostState>((set) => ({
|
||||
|
||||
return post
|
||||
},
|
||||
togglePostLike: (postId) =>
|
||||
set((state) => ({
|
||||
posts: state.posts.map((post) => {
|
||||
if (post.id !== postId) return post
|
||||
const liked = !post.liked
|
||||
return {
|
||||
...post,
|
||||
liked,
|
||||
likeCount: liked ? post.likeCount + 1 : Math.max(0, post.likeCount - 1),
|
||||
}
|
||||
}),
|
||||
})),
|
||||
incrementCommentCount: (postId) =>
|
||||
set((state) => ({
|
||||
posts: state.posts.map((post) =>
|
||||
post.id === postId ? { ...post, commentCount: post.commentCount + 1 } : post,
|
||||
),
|
||||
})),
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user