-
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 #925
base: master
Are you sure you want to change the base?
add task solution #925
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.
GJ 👍
One thing should be fixed (cannot add comment), also, added a few comments to improve code :)
src/App.tsx
Outdated
useEffect(() => { | ||
getAllUsers() | ||
.then(usersFromServer => { | ||
if (usersFromServer) { |
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.
I guess this check is redundant here
If the request is successful we will get an array (at least empty)
but not critical
setPosts(postsFromServer); | ||
} | ||
}) | ||
.finally(() => setIsLoading(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.
would be nice to add .catch
too here
src/App.tsx
Outdated
{(!posts.length | ||
&& selectedUser | ||
&& !errorMessage | ||
&& !isLoading | ||
&& !isDropdownActive | ||
) && ( |
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.
I'd move it into a variable above. Do you need isDropdownActive
here?
src/components/PostsList.tsx
Outdated
setSelectedPost, | ||
setIsCommentButtonClicked, | ||
}) => { | ||
const selectedPostHandler = (post: 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.
create this function in the App.tsx and pass as prop instead of setSelectedPost, and setIsCommentButtonClicked,
src/components/UserSelector.tsx
Outdated
const dropdown = useRef<HTMLDivElement>(null); | ||
|
||
useEffect(() => { | ||
const closeDropdown = (e: MouseEvent) => { |
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.
define function outside the useEffect
hook
src/components/PostDetails.tsx
Outdated
.finally(() => setIsCommentsLoading(false)); | ||
}, [selectedPost]); | ||
|
||
const deleteComment = (id: number) => { |
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.
I'd move all such functions into a separate file
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.
Still not fixed from the previous review
src/components/PostDetails.tsx
Outdated
return client.delete(`/comments/${id}`); | ||
}; | ||
|
||
const handleDeletingComment = (id: number) => { |
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 handleDeletingComment = (id: number) => { | |
const handleDeleteComment = (id: number) => { |
src/components/NewCommentForm.tsx
Outdated
const handleNameChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setCommentName(event.target.value); | ||
setFormError({ ...formError, 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.
you may save all values in state as object (the same as errors), and create 1 handler. Use event.target.name as key for object:
setFormValue(prevValues => ({ ...prevValues, [name]: value }))
src/components/NewCommentForm.tsx
Outdated
const addComment = (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
if (!commentName) { |
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.
it's better to trim value here - count spaces only as an empty string
src/components/NewCommentForm.tsx
Outdated
setFormError(rest => ({ ...rest, name: 'Enter some text' })); | ||
} | ||
|
||
if (selectedPost?.id && !isEmptyField) { |
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 selectedPost?.id
is 0 number it will not work
generally, it doesn't work, also, there is a strange error for the name field:
Uploading Screencast from 28-09-23 19:49:44.webm…
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.
Not fixed
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 👍
Let's improve your code
src/App.tsx
Outdated
const getAllUsers = () => { | ||
return client.get<User[]>('/users'); | ||
}; |
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.
Still not fixed from the previous review, will be better if you create a separate file for all helpers functions
src/App.tsx
Outdated
|
||
export const App: React.FC = () => { | ||
const [users, setUsers] = useState<User[]>([]); | ||
const [posts, setPosts] = useState<Post[]>([]); | ||
const [errorMessage, setErrorMessage] = useState(''); |
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.
Add a generic types everywher
const [errorMessage, setErrorMessage] = useState(''); | |
const [errorMessage, setErrorMessage] = useState<string>(''); |
src/App.tsx
Outdated
<div className="notification is-warning" data-cy="NoPostsYet"> | ||
No posts yet | ||
</div> | ||
{(posts.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.
{(posts.length > 0 | |
{(!!posts.length |
src/App.tsx
Outdated
<PostDetails /> | ||
</div> | ||
|
||
{selectedPost !== null && ( |
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.
{selectedPost !== null && ( | |
{selectedPost && ( |
src/App.tsx
Outdated
@@ -50,12 +132,19 @@ export const App: React.FC = () => { | |||
'is-parent', | |||
'is-8-desktop', | |||
'Sidebar', | |||
'Sidebar--open', | |||
{ 'Sidebar--open': selectedPost !== null }, |
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.
{ 'Sidebar--open': selectedPost !== null }, | |
{ 'Sidebar--open': selectedPost }, |
src/components/PostDetails.tsx
Outdated
.finally(() => setIsCommentsLoading(false)); | ||
}, [selectedPost]); | ||
|
||
const deleteComment = (id: number) => { |
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.
Still not fixed from the previous review
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 work! Almost done
src/components/NewCommentForm.tsx
Outdated
setFormError(rest => ({ ...rest, name: 'Enter some text' })); | ||
} | ||
|
||
if (selectedPost?.id && !isEmptyField) { |
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.
Not fixed
src/components/UserSelector.tsx
Outdated
}) => { | ||
const dropdownHandler = () => setIsDropdownActive(!isDropdownActive); | ||
|
||
const dropdown = useRef<HTMLDivElement>(null); |
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.
Not fixed
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 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.
Great work!
DEMO LINK