-
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
Solution #891
base: master
Are you sure you want to change the base?
Solution #891
Conversation
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! Check comments and try to write improvements.
src/App.tsx
Outdated
getUsers() | ||
.then(setUsers) | ||
.catch(() => setIsError(true)); |
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.
If you are good with async/await try to rewrite this code. But its no required.
src/App.tsx
Outdated
setLoading(true); | ||
setSelectedPost(null); | ||
|
||
if (selectedUser) { | ||
getPosts(selectedUser.id) | ||
.then(setPosts) | ||
.catch(() => setIsError(true)) | ||
.finally(() => setLoading(false)); | ||
} else { | ||
setLoading(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.
Try to rewrite this code, without else. Loading starts every time when updating the user, maybe you can add a condition first of all?
src/App.tsx
Outdated
!selectedUser | ||
&& ( |
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.
Please note, if we receive a 0 in these conditions, it will be reflected on our page.
In this case, we know that the value is not 0. But if we have a small chance of it, use !! for the type conversions.
Example: !!!selectedUser &&
src/App.tsx
Outdated
|
||
<Loader /> | ||
{ | ||
loading |
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.
The best way to name the param isLoading.
src/App.tsx
Outdated
> | ||
Something went wrong! | ||
</div> | ||
{posts.length === 0 && selectedUser && !isError && ( |
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.
!post.length
src/components/PostDetails.tsx
Outdated
</p> | ||
</div> | ||
|
||
<div className="block"> | ||
<Loader /> | ||
{loading |
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.
isLoading
src/components/PostDetails.tsx
Outdated
<p className="title is-4" data-cy="NoCommentsMessage"> | ||
No comments yet | ||
</p> | ||
{!isError && !loading && comments.length !== 0 && ( |
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.
{!isError && !loading && comments.length !== 0 && ( | |
{!loading && !isError && !!comments.length && ( |
src/components/PostDetails.tsx
Outdated
type="button" | ||
className="delete is-small" | ||
aria-label="delete" | ||
onClick={() => deleteCommentHandler(comment.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.
You can write a handler function for it.
src/components/PostsList.tsx
Outdated
className={classNames('button is-link', { | ||
'is-light': post.id !== selectedPost?.id, | ||
})} | ||
onClick={() => toggleSelectedPostHandler(post)} |
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.
You can write a handler for it.
src/components/UserSelector.tsx
Outdated
className={classNames('dropdown-item', { | ||
'is-active': user.id === selectedUser?.id, | ||
})} | ||
onClick={() => selectUserHandler(user)} |
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.
You can write a handler for it.
const [formFields, setFormFields] = useState<FormFields>({ | ||
name: '', | ||
email: '', | ||
text: '', | ||
}); |
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 work!
const changNameHandler = ( | ||
event: React.ChangeEvent<HTMLInputElement>, | ||
) => { | ||
setName(event.target.value); | ||
setNameError(false); | ||
setFormFields(currFields => ({ | ||
...currFields, | ||
name: event.target.value, | ||
})); | ||
|
||
setFormErrors(currErrors => ({ | ||
...currErrors, | ||
isNameError: 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.
Try to use one handler function for all fields.
isNameError: false, | ||
isEmailError: false, | ||
isTextError: 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.
Use the initial state for this error obj, and then for the clear function.
isNameError: false, | ||
isEmailError: false, | ||
isTextError: 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.
This is the same object, use in this place initialErrorState, for example.
name: '', | ||
email: '', | ||
text: '', |
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.
This is the same object, use in this place initialFormState, for example.
</div> | ||
)} | ||
{/* eslint-disable-next-line no-extra-boolean-cast */} | ||
{!!!posts.length && selectedUser && !isError && ( |
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.
Interesting work)
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.
Check out @DanilWeda last review, some things are not fixed yet
https://outofs.github.io/react_dynamic-list-of-posts/