-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
add task solution #1150
Open
dima91020
wants to merge
2
commits into
mate-academy:master
Choose a base branch
from
dima91020:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add task solution #1150
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
import React, { | ||
createContext, | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
import { SharedContextValue } from './types/ContextValues'; | ||
import { getUsers } from './api/users'; | ||
import { User } from './types/User'; | ||
import { Post } from './types/Post'; | ||
import { getPosts } from './api/posts'; | ||
import { Comment, CommentData } from './types/Comment'; | ||
import { createComment, deleteComment, getComments } from './api/comments'; | ||
|
||
export const SharedContext = createContext<SharedContextValue | null>(null); | ||
|
||
type Props = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
export const SharedProvider: React.FC<Props> = ({ children }) => { | ||
const [users, setUsers] = useState<User[]>([]); | ||
const [posts, setPosts] = useState<Post[]>([]); | ||
const [comments, setComments] = useState<Comment[]>([]); | ||
const [selectedUserId, setSelectedUserId] = useState<number | null>(null); | ||
const [selectedPostId, setSelectedPostId] = useState<number | null>(null); | ||
const [isLoadingPosts, setIsLoadingPosts] = useState(false); | ||
const [isLoadingComments, setIsLoadingComments] = useState(false); | ||
const [isSumbitting, setIsSumbitting] = useState(false); | ||
const [isError, setIsError] = useState(false); | ||
|
||
const handleCreateComment = useCallback( | ||
async ({ name, email, body }: Omit<CommentData, 'postId'>) => { | ||
setIsSumbitting(true); | ||
|
||
const trimmedName = name.trim(); | ||
const trimmedEmail = email.trim(); | ||
const trimmedBody = body.trim(); | ||
|
||
if (trimmedName && trimmedEmail && trimmedBody && selectedPostId) { | ||
try { | ||
const newComment = await createComment({ | ||
postId: selectedPostId, | ||
name: trimmedName, | ||
email: trimmedEmail, | ||
body: trimmedBody, | ||
}); | ||
|
||
setComments(prevComments => [...prevComments, newComment]); | ||
} catch { | ||
setIsError(true); | ||
} finally { | ||
setIsSumbitting(false); | ||
} | ||
} | ||
}, | ||
[selectedPostId, setIsError], | ||
); | ||
|
||
const handleDeleteComment = useCallback( | ||
async (commentId: number) => { | ||
try { | ||
await deleteComment(commentId); | ||
|
||
setComments(currentComments => | ||
currentComments.filter(comm => comm.id !== commentId), | ||
); | ||
} catch { | ||
setIsError(true); | ||
} | ||
}, | ||
[setIsError], | ||
); | ||
|
||
const handleSelectUser = useCallback( | ||
async (userId: number) => { | ||
setSelectedUserId(userId); | ||
setIsLoadingPosts(true); | ||
|
||
try { | ||
const loadedPosts = await getPosts(userId); | ||
|
||
setPosts(loadedPosts); | ||
} catch { | ||
setIsError(true); | ||
} finally { | ||
setIsLoadingPosts(false); | ||
} | ||
}, | ||
[setIsError], | ||
); | ||
|
||
const handleSelectPost = useCallback( | ||
async (postId: number) => { | ||
setSelectedPostId(postId); | ||
|
||
try { | ||
const loadedUsers = await getUsers(); | ||
|
||
setUsers(loadedUsers); | ||
} catch { | ||
setIsError(true); | ||
} | ||
}, | ||
[setIsError], | ||
); | ||
|
||
const handleLoadComments = useCallback( | ||
async (postId: number) => { | ||
setIsLoadingComments(true); | ||
// setIsError(false); | ||
|
||
try { | ||
const loadedComments = await getComments(postId); | ||
|
||
setComments(loadedComments); | ||
} catch { | ||
setIsError(true); | ||
} finally { | ||
setIsLoadingComments(false); | ||
} | ||
}, | ||
[setIsError], | ||
); | ||
|
||
const handleClosePostDetails = useCallback(() => { | ||
setSelectedPostId(null); | ||
}, []); | ||
|
||
const selectedUser = useMemo( | ||
() => users?.find(user => user.id === selectedUserId) || null, | ||
[users, selectedUserId], | ||
); | ||
|
||
const selectedPost = useMemo( | ||
() => posts?.find(post => post.id === selectedPostId) || null, | ||
[posts, selectedPostId], | ||
); | ||
|
||
const sharedValue = useMemo( | ||
() => ({ | ||
users, | ||
posts, | ||
comments, | ||
isError, | ||
selectedUser, | ||
selectedPost, | ||
isLoadingPosts, | ||
isLoadingComments, | ||
isSumbitting, | ||
handleCreateComment, | ||
handleDeleteComment, | ||
handleLoadComments, | ||
handleSelectUser, | ||
handleSelectPost, | ||
handleClosePostDetails, | ||
}), | ||
[ | ||
users, | ||
posts, | ||
comments, | ||
isError, | ||
selectedUser, | ||
selectedPost, | ||
isLoadingPosts, | ||
isLoadingComments, | ||
isSumbitting, | ||
handleCreateComment, | ||
handleDeleteComment, | ||
handleLoadComments, | ||
handleSelectUser, | ||
handleSelectPost, | ||
handleClosePostDetails, | ||
], | ||
); | ||
|
||
useEffect(() => { | ||
const fetchUsers = async () => { | ||
try { | ||
const loadedUsers = await getUsers(); | ||
|
||
setUsers(loadedUsers); | ||
} catch { | ||
setIsError(true); | ||
} | ||
}; | ||
|
||
fetchUsers(); | ||
}, []); | ||
|
||
return ( | ||
<SharedContext.Provider value={sharedValue}> | ||
{children} | ||
</SharedContext.Provider> | ||
); | ||
}; | ||
|
||
export const useValues = () => { | ||
const value = useContext(SharedContext); | ||
|
||
if (!value) { | ||
throw new Error('Something is wrong with provider SharedContext'); | ||
} | ||
|
||
return value; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Comment, CommentData } from '../types/Comment'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const getComments = (postId: number) => { | ||
return client.get<Comment[]>(`/comments?postId=${postId}`); | ||
}; | ||
|
||
export const createComment = (comment: CommentData) => { | ||
return client.post<Comment>(`/comments`, comment); | ||
}; | ||
|
||
export const deleteComment = (commentId: number) => { | ||
return client.delete(`/comments/${commentId}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Post } from '../types/Post'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const getPosts = (userId: number) => { | ||
return client.get<Post[]>(`/posts?userId=${userId}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { User } from '../types/User'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const getUsers = () => { | ||
return client.get<User[]>(`/users`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
import { useValues } from '../SharedContext'; | ||
import { CommentsListItem } from './CommentsListItem'; | ||
|
||
export const CommentsList = React.memo(() => { | ||
const { comments } = useValues(); | ||
|
||
return ( | ||
<> | ||
<p className="title is-4">Comments:</p> | ||
|
||
{comments?.map(comment => ( | ||
<CommentsListItem key={comment.id} comment={comment} /> | ||
))} | ||
</> | ||
); | ||
}); | ||
|
||
CommentsList.displayName = 'CommentsList'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react'; | ||
import { Comment } from '../types/Comment'; | ||
import { useValues } from '../SharedContext'; | ||
|
||
type Props = { | ||
comment: Comment; | ||
}; | ||
|
||
export const CommentsListItem: React.FC<Props> = ({ comment }) => { | ||
const { id, name, email, body } = comment; | ||
const { handleDeleteComment } = useValues(); | ||
|
||
const handleSubmitDeleteComment = () => { | ||
handleDeleteComment(id); | ||
}; | ||
|
||
return ( | ||
<article className="message is-small" data-cy="Comment"> | ||
<div className="message-header"> | ||
<a href={email} data-cy="CommentAuthor"> | ||
{name} | ||
</a> | ||
<button | ||
data-cy="CommentDelete" | ||
type="button" | ||
className="delete is-small" | ||
aria-label="delete" | ||
onClick={handleSubmitDeleteComment} | ||
> | ||
delete button | ||
</button> | ||
</div> | ||
|
||
<div className="message-body" data-cy="CommentBody"> | ||
{body} | ||
</div> | ||
</article> | ||
); | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.