-
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 #1025
base: master
Are you sure you want to change the base?
add task solution #1025
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, but let's improve ur code a bit 🥹
src/api.ts
Outdated
@@ -22,4 +22,4 @@ function get<T>(url: string): Promise<T> { | |||
|
|||
export const getTodos = () => get<Todo[]>('/todos'); | |||
|
|||
export const getUser = (userId: number) => get<User>(`/users/${userId}`); | |||
export const getUser = (userId: number) => get<User>(`/users/${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.
I think it will be more readable)
export const getUser = (userId: number) => get<User>(`/users/${userId}`); | |
export const getUserById = (userId: number) => get<User>(`/users/${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.
I think it will be more readable)
Not fixed
const setStatus = (status: string) => { | ||
dispatch(filterActions.setStatus(status as Status)); | ||
}; |
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.
set? use handle
prefix for such functions.
also handle functions receive event
by default
const setStatus = (status: string) => { | |
dispatch(filterActions.setStatus(status as Status)); | |
}; | |
const handleChangeStatus = (event: React.ChangeEvent<HTMLSelectElement>) => { | |
dispatch(filterActions.setStatus(event.target.value)); | |
}; |
do it for all the cases!
<select | ||
data-cy="statusSelect" | ||
value={currentStatus} | ||
onChange={event => setStatus(event.target.value)} |
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 => setStatus(event.target.value)} | |
onChange={handleChangeStatus} |
src/features/currentTodo.ts
Outdated
// we use string literal as a type to avoid mistype in future | ||
type RemoveTodoAction = { type: 'currentTodo/REMOVE' }; | ||
|
||
// payload is a typical name for an action data | ||
type SetTodoAction = { | ||
type: 'currentTodo/SET'; | ||
payload: Todo; | ||
}; | ||
|
||
// Action creator return type protect us from a mistype | ||
const removeTodo = (): RemoveTodoAction => ({ type: 'currentTodo/REMOVE' }); | ||
|
||
export const currentTodoSlice = createSlice({ | ||
name: 'currentTodo', | ||
initialState, | ||
reducers: {}, | ||
const setTodo = (todo: Todo): SetTodoAction => ({ | ||
type: 'currentTodo/SET', | ||
payload: todo, | ||
}); | ||
|
||
// These actions will be used in the application |
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.
why do I only see comments in this file?
GPT?
}; | ||
|
||
const setQuery = (inputValue: string) => { | ||
dispatch(filterActions.setQuery(inputValue)); |
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 trim
method to ignore spaces
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 👍
Left some comments and suggestions
src/App.tsx
Outdated
setIsLoading(true); | ||
getTodos() | ||
.then(result => { | ||
dispatch(todosSlice.actions.addTodos(filteredTodo(result))); |
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 don't need to fetch todos every time when filters changes
src/App.tsx
Outdated
<div className="block"> | ||
<TodoFilter /> | ||
</div> | ||
const filteredTodo = useCallback( |
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 filteredTodo = useCallback( | |
const filteredTodos = useMemo( |
Just filter the todos that you have in the state, store in a variable and then show only them
src/App.tsx
Outdated
<div className="block"> | ||
<Loader /> | ||
<TodoList /> | ||
<div className="block">{isLoading ? <Loader /> : <TodoList />}</div> |
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.
<div className="block">{isLoading ? <Loader /> : <TodoList />}</div> | |
<div className="block">{isLoading ? <Loader /> : <TodoList todos={filteredTodos} />}</div> |
Like this
src/api.ts
Outdated
@@ -22,4 +22,4 @@ function get<T>(url: string): Promise<T> { | |||
|
|||
export const getTodos = () => get<Todo[]>('/todos'); | |||
|
|||
export const getUser = (userId: number) => get<User>(`/users/${userId}`); | |||
export const getUser = (userId: number) => get<User>(`/users/${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.
I think it will be more readable)
Not fixed
src/components/TodoList/TodoList.tsx
Outdated
return ( | ||
<tr | ||
key={id} | ||
data-cy="todo" |
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 a todo component
src/components/TodoList/TodoList.tsx
Outdated
} | ||
> | ||
<span className="icon"> | ||
{currentTodo?.id === 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 compare ids twice, create a variable
setTimeout(() => { | ||
getUser(userId) | ||
.then(setUser) | ||
.finally(() => setIsLoading(false)); | ||
}, 1000); |
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.
Why do you need a timeout?
src/features/filter.ts
Outdated
status: (state: State, action: PayloadAction<string>) => ({ | ||
...state, | ||
status: action.payload, | ||
}), |
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.
status: (state: State, action: PayloadAction<string>) => ({ | |
...state, | |
status: action.payload, | |
}), | |
status: (state: State, action: PayloadAction<string>) => { | |
state.status = action.payload, | |
}, |
In the redux toolkit you can mutate the state
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.
Approved with a small comment.
Many thanks)
<div className="block"> | ||
<TodoFilter /> | ||
</div> | ||
const filteredTodos = useMemo(() => { |
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 move the function outside the file, and then use it as a helper func in this place.
DEMO LINK