-
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
Develop #919
base: master
Are you sure you want to change the base?
Develop #919
Conversation
… logic in PostDetails
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.
Good job, but there is quite a bit of work to be done.
import React, { useState } from 'react'; | ||
import { Comment } from '../../types/Comment'; | ||
|
||
type CC = { |
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.
What does it mean - CC
?
Do not abbreviate variable names to unreadable ones
{modalUser ? ( | ||
<> | ||
{isLoad ? <Loader /> : ( | ||
<> | ||
{isError ? ( | ||
<div | ||
className="notification is-danger" | ||
data-cy="PostsLoadingError" | ||
> | ||
Something went wrong! | ||
</div> | ||
) : ( | ||
<> | ||
{posts.length ? ( | ||
<PostsList posts={posts} /> | ||
) : ( | ||
<div | ||
className="notification is-warning" | ||
data-cy="NoPostsYet" | ||
> | ||
No posts yet | ||
</div> | ||
)} | ||
</> | ||
)} | ||
</> | ||
)} | ||
</> | ||
) : ( | ||
<p data-cy="NoSelectedUser"> | ||
No user selected | ||
</p> | ||
)} |
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.
Everything works fine now, but such a large nestedness is hard to read and maintain in the future.
Try to break it down into smaller components
import React, { useState } from 'react'; | ||
import { Post } from '../../types/Post'; | ||
|
||
type MPC = { |
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.
Do not abbreviate variable names to unreadable ones
import React, { useState } from 'react'; | ||
import { User } from '../../types/User'; | ||
|
||
type MUC = { |
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.
Do not abbreviate variable names to unreadable ones
src/components/NewCommentForm.tsx
Outdated
const { comments, setComments } = useContext(CommentsContext); | ||
const { modalPost } = useContext(ModalPostContext); | ||
|
||
const errorCondition = (key: keyof CommentData) => { |
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.
const errorCondition = (key: keyof CommentData) => { | |
const checkForErrors = (key: keyof CommentData) => { |
src/components/NewCommentForm.tsx
Outdated
} | ||
}; | ||
|
||
const handleReset = (event: React.FormEvent<HTMLFormElement>) => { |
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.
Handle reset of what?
src/components/NewCommentForm.tsx
Outdated
setFormFields(DEFAULT_FIELDS); | ||
}; | ||
|
||
const handleFieldChange = ( |
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.
const handleFieldChange = ( | |
const handleFormFieldChange = ( |
const handleFieldChange = ( | |
const handleInputChange = ( |
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.
Almost done!
const showLoad = isLoad && !isError && !posts.length; | ||
const showError = !isLoad && isError && !posts.length; | ||
const showPostList = !isLoad && !isError && !!posts.length; | ||
const showNoPostsYet = !isLoad && !isError && !posts.length; |
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.
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.
Nicely done 🚀
const handleDeleteComment = () => { | ||
setComments(comments.filter(({ id: comId }) => comId !== id)); | ||
|
||
deleteCommentById(id); |
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.
In case of error, the user doesn't know anything about it and think that they successfully deleted the comment 🤷♂️
setIsError(false); | ||
setIsLoad(true); | ||
if (modalUser) { | ||
(async () => { | ||
try { | ||
const serverPosts = await getPostsByUserId(modalUser.id); | ||
|
||
setPosts(serverPosts); | ||
} catch { | ||
setIsError(true); | ||
} finally { | ||
setIsLoad(false); | ||
} | ||
})(); | ||
} |
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.
setIsError(false); | |
setIsLoad(true); | |
if (modalUser) { | |
(async () => { | |
try { | |
const serverPosts = await getPostsByUserId(modalUser.id); | |
setPosts(serverPosts); | |
} catch { | |
setIsError(true); | |
} finally { | |
setIsLoad(false); | |
} | |
})(); | |
} | |
if (modalUser) { | |
setIsError(false); | |
setIsLoad(true); | |
(async () => { | |
try { | |
const serverPosts = await getPostsByUserId(modalUser.id); | |
setPosts(serverPosts); | |
} catch { | |
setIsError(true); | |
} finally { | |
setIsLoad(false); | |
} | |
})(); | |
} |
If no modalUser
you still set is load to true
'is-danger': checkForErrors('name'), | ||
})} | ||
value={formFields.name} | ||
onChange={event => handleFormFieldChange(event, 'name')} |
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.
Hint, you can get the input name from event.target
const handleFormFieldChange = (
event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
) => {
const { name, value } = event.target;
setFormFields(prevState => ({ ...prevState, [key]: value }));
};
DEMO LINK
Something wrong with tests, please proceed to review the demo link.