In the existing Posts app, we have implemented the functionality to manage posts. We added API functions, store functions, and custom hooks to manage posts. In this task, we will extend the app to manage comments for each post.
Step 1: Add API functions for managing comments
In this step, we will add API functions to fetch, create, edit, and delete comments for a post.
Add a new type for comments
First, we need to add a new type for comments. Update web/src/types.ts file to include a new type for comments:
Notice the postId field in the CommentType type. This field is used to associate a comment with a post.
Fetch comments for each post
Next, we will add a new function to fetch comments for a post in the web/src/data/api.ts file:
Notice the postId parameter in the fetchComments function. This parameter is used to fetch comments for a specific post.
Make sure to import CommentType at the top of the file:
Delete a comment
Now, we will add a new function to delete a comment for a post in the web/src/data/api.ts file:
Notice the postId and commentId parameters in the deleteComment function. These parameters are used to delete a specific comment for a post.
Create a comment
Add a new function to web/src/data/api.ts file to create a comment for a post:
Notice the postId and content parameters in the createComment function. These parameters are used to create a new comment for a post.
Edit a comment
Finally, we will add a new function to edit a comment for a post in the web/src/data/api.ts file:
Notice the postId, commentId, and content parameters in the editComment function. These parameters are used to edit a specific comment for a post.
Step 2: And store functions for managing comments
In this step, we will add state and action functions to manage comments in the store.
Add state for comments
Update web/src/lib/store.ts file to include state for managing comments:
Make sure to import CommentType at the top of the file:
Add comments to the store
Next, we will add a new function to web/src/lib/store.ts file to set comments in the store:
Add a comment to the store
Add a new function to the web/src/lib/store.ts file to add a comment to the store:
Make sure the new comment is added at the beginning of the list. This way, the latest comment will be displayed first which aligns with the sorting order of comments fetched from the API.
Remove a comment from the store
Add a new function to the web/src/lib/store.ts file to remove a comment from the store:
Update a comment in the store
Finally, add a new function to the web/src/lib/store.ts file to update the content of a comment in the store:
For simplicity, we are updating the content of the comment only. You can extend this function to update other fields as well.
Step 3: Create custom hooks for managing comments
In this step, we will create custom hooks for managing comments, similar to the hooks we created for managing posts.
Create useQueryComments hook
Add a new file web/src/hooks/use-query-comments.tsx:
This hook fetches comments for a post and updates the store with the fetched comments. Notice the postId parameter in the useQueryComments hook. If the postId changes, the hook refetches the comments for the new post.
Create useMutationComments hook
Add a new file web/src/hooks/use-mutation-comments.tsx:
This hook provides functions to delete, add, and update comments. Notice the postId parameter in the useMutationComments hook. This parameter is used to associate comments with a post.
Conclusion
In this task, we added API functions, store functions, and custom hooks to manage comments for each post. We can now fetch, create, edit, and delete comments for a post. In the next task, we will integrate these functions with the UI to display comments and allow users to interact with them.