Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Excellent work on implementing Redux and connecting the components! The application functions well, but I'm requesting changes because a core architectural requirement hasn't been met. The task specifies creating separate Redux slices for features like users, posts, and comments, but the current code uses a single monolithic slice (appSlice). Please refactor this into multiple, more focused slices and combine them in src/app/store.ts to align with the requirements.
Additionally, while not a blocker, the optimistic comment deletion in PostDetails.tsx could be improved. Consider adding error handling in the catch block to revert the UI change if the API call fails. Looking forward to seeing the updated slice structure!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Important
Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.
Tip
If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.
Caution
Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.
Footnotes
src/app/store.ts
Outdated
| export const store = configureStore({ | ||
| reducer: { | ||
| counter: counterReducer, | ||
| app: AppSlice, |
There was a problem hiding this comment.
The task requires creating separate slices for users, author, posts, selectedPost, and comments. Instead, a single large slice (AppSlice) has been created for the entire application state under the app key. You should refactor your Redux state into multiple, more focused slices and combine their reducers here.
| dispatch(removeCommentAction(commentId)); | ||
|
|
||
| try { | ||
| await commentsApi.deleteComment(commentId); | ||
| } catch { | ||
| dispatch(setCommentsError()); | ||
| } |
There was a problem hiding this comment.
This is a good use of an optimistic update for a better user experience. However, consider what happens if the API call in the try block fails. The comment is already removed from the UI by the action on line 62, but it still exists on the server. A more robust implementation would handle this inconsistency, for example, by re-fetching the comments in the catch block to synchronize the UI with the server state.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Excellent work on refactoring the Redux state! You've successfully broken down the monolithic slice into feature-specific slices for users, posts, and comments, which was the main goal here. The new structure is clean, scalable, and follows best practices perfectly. All the requirements for this task have been met, so I am approving your solution. As a final cleanup step, you can delete the old src/app/appSlice.ts file, as it's no longer used. Great job!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
| import { createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
| import { User } from '../types/User'; | ||
| import { Post } from '../types/Post'; | ||
| import { Comment } from '../types/Comment'; | ||
|
|
||
| export interface AppState { | ||
| users: User[]; | ||
| author: number | null; | ||
| posts: { | ||
| loaded: boolean; | ||
| hasError: boolean; | ||
| items: Post[]; | ||
| }; | ||
|
|
||
| selectedPost: number | null; | ||
|
|
||
| comments: { | ||
| loaded: boolean; | ||
| hasError: boolean; | ||
| items: Comment[]; | ||
| }; | ||
| } | ||
|
|
||
| const initialState: AppState = { | ||
| users: [], | ||
| author: null, | ||
| posts: { | ||
| loaded: false, | ||
| hasError: false, | ||
| items: [], | ||
| }, | ||
|
|
||
| selectedPost: null, | ||
|
|
||
| comments: { | ||
| loaded: false, | ||
| hasError: false, | ||
| items: [], | ||
| }, | ||
| }; | ||
|
|
||
| const appSlice = createSlice({ | ||
| name: 'app', | ||
| initialState, | ||
| reducers: { | ||
| setUsers(state, action: PayloadAction<User[]>) { | ||
| state.users = action.payload; | ||
| }, | ||
| setAuthor(state, action: PayloadAction<number>) { | ||
| state.author = action.payload; | ||
| }, | ||
| setPosts(state, action: PayloadAction<Post[]>) { | ||
| state.posts.hasError = false; | ||
| state.posts.loaded = true; | ||
| state.posts.items = action.payload; | ||
| }, | ||
| setSelectedPost(state, action: PayloadAction<number>) { | ||
| state.selectedPost = action.payload; | ||
| }, | ||
| clearAuthor(state) { | ||
| state.author = null; | ||
| }, | ||
| setPostsLoading(state) { | ||
| state.posts.loaded = false; | ||
| state.posts.hasError = false; | ||
| }, | ||
| setPostsError(state) { | ||
| state.posts.hasError = true; | ||
| state.posts.loaded = true; | ||
| }, | ||
| clearSelectedPost(state) { | ||
| state.selectedPost = null; | ||
| }, | ||
| setCommentsLoading(state) { | ||
| state.comments.loaded = false; | ||
| state.comments.hasError = false; | ||
| }, | ||
| setComments(state, action: PayloadAction<Comment[]>) { | ||
| state.comments.hasError = false; | ||
| state.comments.loaded = true; | ||
| state.comments.items = action.payload; | ||
| }, | ||
| setCommentsError(state) { | ||
| state.comments.hasError = true; | ||
| state.comments.loaded = true; | ||
| }, | ||
| addComment(state, action: PayloadAction<Comment>) { | ||
| state.comments.items.push(action.payload); | ||
| }, | ||
| removeComment(state, action: PayloadAction<number>) { | ||
| state.comments.items = state.comments.items.filter( | ||
| comment => comment.id !== action.payload, | ||
| ); | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| export const { | ||
| setUsers, | ||
| setAuthor, | ||
| setPosts, | ||
| setSelectedPost, | ||
| clearAuthor, | ||
| setPostsLoading, | ||
| setPostsError, | ||
| clearSelectedPost, | ||
| setCommentsLoading, | ||
| setComments, | ||
| setCommentsError, | ||
| addComment, | ||
| removeComment, | ||
| } = appSlice.actions; | ||
|
|
||
| export default appSlice.reducer; |
There was a problem hiding this comment.
Great job refactoring the Redux state into separate slices for users, posts, and comments! This file appears to be a leftover from the previous implementation. Since it's no longer used anywhere in the application, it should be deleted to remove the dead code.
DEMO LINK