-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
first solution #2127
base: master
Are you sure you want to change the base?
first solution #2127
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 job! Made a few suggestions about your code.
const findUserById = (id: number) => { | ||
return usersFromServer.find(user => user.id === id) || 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.
It's better to relocate this function to a separate file.
<form | ||
action="/api/todos" | ||
method="POST" | ||
onSubmit={handleSubmit} | ||
> | ||
<div className="field"> | ||
<input type="text" data-cy="titleInput" /> | ||
<span className="error">Please enter a title</span> | ||
<input | ||
type="text" | ||
data-cy="titleInput" | ||
placeholder="Enter a Title" | ||
value={title} | ||
onChange={handleTitleInput} | ||
/> | ||
|
||
{hasTitleError && <span className="error">Please enter a title</span>} | ||
</div> | ||
|
||
<div className="field"> | ||
<select data-cy="userSelect"> | ||
<select | ||
data-cy="userSelect" | ||
value={userId} | ||
onChange={handleUserSelect} | ||
> | ||
<option value="0" disabled>Choose a user</option> | ||
{usersFromServer.map(user => ( | ||
<option value={user.id} key={user.id}>{user.name}</option> | ||
))} | ||
</select> | ||
|
||
<span className="error">Please choose a user</span> | ||
{hasUserError && <span className="error">Please choose a user</span>} | ||
</div> | ||
|
||
<button type="submit" data-cy="submitButton"> | ||
Add | ||
</button> | ||
</form> |
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.
Consider creating a separate file for a ToDoForm and it's hooks, it will keep your App file more readable
if (!title) { | ||
setHasTitleError(!hasTitleError); | ||
|
||
return; | ||
} | ||
|
||
if (!userId) { | ||
setHasUserError(!hasUserError); | ||
|
||
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.
you actually don't need these
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 need this to prevent changes and raise individual errors if there are any
const handleSubmit = ((e: React.ChangeEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
|
||
if (!title && !userId) { |
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.
better to use ||
operator, then you won't need separate conditions below
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.
Then I will always have both mistakes
src/components/TodoInfo/TodoInfo.tsx
Outdated
className={cn('TodoInfo', { | ||
'TodoInfo--completed': todo.completed, |
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.
Guess it's better to move this part to a new variable
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!
DEMO LINK