-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Develop #1124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Develop #1124
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| /build | ||
| /node_modules | ||
| /src | ||
|
|
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| 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; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,9 @@ | ||
| import { configureStore, ThunkAction, Action } from '@reduxjs/toolkit'; | ||
| import AppSlice from './appSlice'; | ||
| // eslint-disable-next-line import/no-cycle | ||
| import counterReducer from '../features/counter/counterSlice'; | ||
|
|
||
| export const store = configureStore({ | ||
| reducer: { | ||
| counter: counterReducer, | ||
| app: AppSlice, | ||
|
||
| }, | ||
| }); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,61 +1,47 @@ | ||
| import React, { useEffect, useState } from 'react'; | ||
| import { Loader } from './Loader'; | ||
| import { NewCommentForm } from './NewCommentForm'; | ||
|
|
||
| import * as commentsApi from '../api/comments'; | ||
|
|
||
| import { Post } from '../types/Post'; | ||
| import { Comment, CommentData } from '../types/Comment'; | ||
| import { CommentData } from '../types/Comment'; | ||
| import { useAppDispatch, useAppSelector } from '../app/hooks'; // 👈 Імпортуємо хуки | ||
| import { | ||
| setComments, | ||
| setCommentsLoading, | ||
| setCommentsError, | ||
| addComment as addCommentAction, | ||
| removeComment as removeCommentAction, | ||
| } from '../app/appSlice'; | ||
|
|
||
| type Props = { | ||
| post: Post; | ||
| }; | ||
|
|
||
| export const PostDetails: React.FC<Props> = ({ post }) => { | ||
| const [comments, setComments] = useState<Comment[]>([]); | ||
| const [loaded, setLoaded] = useState(false); | ||
| const [hasError, setError] = useState(false); | ||
| const [visible, setVisible] = useState(false); | ||
| const dispatch = useAppDispatch(); | ||
|
|
||
| function loadComments() { | ||
| setLoaded(false); | ||
| setError(false); | ||
| setVisible(false); | ||
|
|
||
| commentsApi | ||
| .getPostComments(post.id) | ||
| .then(setComments) // save the loaded comments | ||
| .catch(() => setError(true)) // show an error when something went wrong | ||
| .finally(() => setLoaded(true)); // hide the spinner | ||
| } | ||
| const { | ||
| items: comments, | ||
| loaded, | ||
| hasError, | ||
| } = useAppSelector(state => state.app.comments); | ||
|
|
||
| useEffect(loadComments, [post.id]); | ||
| const [visible, setVisible] = useState(false); | ||
|
|
||
| // The same useEffect with async/await | ||
| /* | ||
| async function loadComments() { | ||
| setLoaded(false); | ||
| useEffect(() => { | ||
| setVisible(false); | ||
| setError(false); | ||
|
|
||
| try { | ||
| const commentsFromServer = await commentsApi.getPostComments(post.id); | ||
| dispatch(setCommentsLoading()); | ||
|
|
||
| setComments(commentsFromServer); | ||
| } catch (error) { | ||
| setError(true); | ||
| } finally { | ||
| setLoaded(true); | ||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| loadComments(); | ||
| }, []); | ||
|
|
||
| useEffect(loadComments, [post.id]); // Wrong! | ||
| // effect can return only a function but not a Promise | ||
| */ | ||
| commentsApi | ||
| .getPostComments(post.id) | ||
| .then(data => { | ||
| dispatch(setComments(data)); | ||
| }) | ||
| .catch(() => { | ||
| dispatch(setCommentsError()); | ||
| }); | ||
| }, [post.id, dispatch]); | ||
|
|
||
| const addComment = async ({ name, email, body }: CommentData) => { | ||
| try { | ||
|
|
@@ -66,27 +52,20 @@ export const PostDetails: React.FC<Props> = ({ post }) => { | |
| postId: post.id, | ||
| }); | ||
|
|
||
| setComments(currentComments => [...currentComments, newComment]); | ||
|
|
||
| // setComments([...comments, newComment]); | ||
| // works wrong if we wrap `addComment` with `useCallback` | ||
| // because it takes the `comments` cached during the first render | ||
| // not the actual ones | ||
| dispatch(addCommentAction(newComment)); | ||
| } catch (error) { | ||
| // we show an error message in case of any error | ||
| setError(true); | ||
| dispatch(setCommentsError()); | ||
| } | ||
| }; | ||
|
|
||
| const deleteComment = async (commentId: number) => { | ||
| // we delete the comment immediately so as | ||
| // not to make the user wait long for the actual deletion | ||
| // eslint-disable-next-line max-len | ||
| setComments(currentComments => | ||
| currentComments.filter(comment => comment.id !== commentId), | ||
| ); | ||
|
|
||
| await commentsApi.deleteComment(commentId); | ||
| dispatch(removeCommentAction(commentId)); | ||
|
|
||
| try { | ||
| await commentsApi.deleteComment(commentId); | ||
| } catch { | ||
| dispatch(setCommentsError()); | ||
| } | ||
|
Comment on lines
63
to
79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good use of an optimistic update for a better user experience. However, consider what happens if the API call in the |
||
| }; | ||
|
|
||
| return ( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job refactoring the Redux state into separate slices for
users,posts, andcomments! 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.