-
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
implementation #2160
base: master
Are you sure you want to change the base?
implementation #2160
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.
src/services/findUserById.ts
Outdated
import { User } from '../types/user'; | ||
|
||
export const findUserById = (id: number): User => ( | ||
usersFromServer.find(user => user.id === id) as 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.
find can return the undefined
usersFromServer.find(user => user.id === id) as User | |
usersFromServer.find(user => user.id === id) || null |
src/components/TodoInfo/TodoInfo.tsx
Outdated
<h2 className="TodoInfo__title"> | ||
{todo.title} | ||
</h2> | ||
<UserInfo user={findUserById(todo.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.
user can be null, you need to handle this case here
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 decide to move this check using user?.name
into TodoInfo
component. In case User
is null
no info will be displayed, but in current implementation we have no possibilities to meet User === null
, because when it's null we'll recive ann error 'Please choose 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.
@Daniilart01 but you don't know where else this component can be reused so need to check if user exist
src/App.tsx
Outdated
id="user" | ||
data-cy="userSelect" | ||
defaultValue={0} | ||
onChange={event => handleUserChange(event)} |
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.
onChange={event => handleUserChange(event)} | |
onChange={handleUserChange} |
src/App.tsx
Outdated
setFormData({ | ||
title: event.currentTarget.value, | ||
user: formData.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.
setFormData({ | |
title: event.currentTarget.value, | |
user: formData.user, | |
}); | |
setFormData({ | |
...formData, | |
title: event.currentTarget.value, | |
}); |
src/App.tsx
Outdated
const [emptyTitleFieldError, setEmptyTitleFieldError] = useState(false); | ||
const [emptyUserFieldError, setEmptyUserFieldError] = useState(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.
I think enough just:
const [emptyTitleFieldError, setEmptyTitleFieldError] = useState(false); | |
const [emptyUserFieldError, setEmptyUserFieldError] = useState(false); | |
const [titleError, setTitleError] = useState(false); | |
const [userError, setUserError] = useState(false); |
src/App.tsx
Outdated
const [todosList, setTodosList] = useState(todosFromServer); | ||
|
||
const [formData, setFormData] | ||
= useState<{ title: string, user: User | null }>({ title: '', user: 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.
better to create an interface for it:
interface FormFileds {
title: string;
user: User | 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.
= useState<{ title: string, user: User | null }>({ title: '', user: null }); | |
= useState<FormFields>({ title: '', user: null }); |
src/App.tsx
Outdated
setTodosList([...todosList, { | ||
id: getNextTodoId(todosList), | ||
title: formData.title.trim(), | ||
completed: false, | ||
userId: formData.user.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.
setTodosList([...todosList, { | |
id: getNextTodoId(todosList), | |
title: formData.title.trim(), | |
completed: false, | |
userId: formData.user.id, | |
}]); | |
setTodosList(prevTodos => [...prevTodos, { | |
id: getNextTodoId(todosList), | |
title: formData.title.trim(), | |
completed: false, | |
userId: formData.user.id, | |
}]); |
src/components/TodoInfo/TodoInfo.tsx
Outdated
<h2 className="TodoInfo__title"> | ||
{todo.title} | ||
</h2> | ||
<UserInfo user={findUserById(todo.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.
@Daniilart01 but you don't know where else this component can be reused so need to check if user exist
src/components/UserInfo/UserInfo.tsx
Outdated
type Props = { | ||
user: User | null | ||
}; | ||
|
||
export const UserInfo: React.FC<Props> = ({ user }) => { | ||
return ( | ||
<a className="UserInfo" href={`mailto:${user?.email}`}> | ||
{user?.name} | ||
</a> | ||
); | ||
}; |
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.
type Props = { | |
user: User | null | |
}; | |
export const UserInfo: React.FC<Props> = ({ user }) => { | |
return ( | |
<a className="UserInfo" href={`mailto:${user?.email}`}> | |
{user?.name} | |
</a> | |
); | |
}; | |
type Props = { | |
user: User; | |
}; | |
export const UserInfo: React.FC<Props> = ({ user }) => { | |
return ( | |
<a className="UserInfo" href={`mailto:${user.email}`}> | |
{user.name} | |
</a> | |
); | |
}; |
now If |
src/services/findUserById.ts
Outdated
import { User } from '../types/user'; | ||
|
||
export const findUserById = (id: number): User => { | ||
return usersFromServer.find(user => user.id === id) as 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.
better to avoid the assertions in TS
return usersFromServer.find(user => user.id === id) as User; | |
return usersFromServer.find(user => user.id === id) || null; |
src/services/findUserById.ts
Outdated
import usersFromServer from '../api/users'; | ||
import { User } from '../types/user'; | ||
|
||
export const findUserById = (id: number): 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.
export const findUserById = (id: number): User => { | |
export const findUserById = (id: number): User | null => { |
src/components/TodoInfo/TodoInfo.tsx
Outdated
<h2 className="TodoInfo__title"> | ||
{todo.title} | ||
</h2> | ||
{todo.userId !== 0 && (<UserInfo user={findUserById(todo.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.
move findUserById(todo.userId) this to some var and check if the user is not null
src/App.tsx
Outdated
<input | ||
placeholder="Enter a title" | ||
id="title" | ||
onInput={event => handleTitleChange(event)} |
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.
onInput={event => handleTitleChange(event)} | |
onInput={handleTitleChange} |
we can simplify it
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.
fix it everywhere
src/components/TodoInfo/TodoInfo.tsx
Outdated
<h2 className="TodoInfo__title"> | ||
{todo.title} | ||
</h2> | ||
{todo.userId !== 0 && user && (<UserInfo user={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.
{todo.userId !== 0 && user && (<UserInfo user={user} />)} | |
{user && (<UserInfo user={user} />)} |
src/App.tsx
Outdated
<input | ||
placeholder="Enter a title" | ||
id="title" | ||
onInput={event => handleTitleChange(event)} |
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.
fix it everywhere
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.
lgtm
DEMO LINK