-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 1.1 #1674
base: master
Are you sure you want to change the base?
Solution 1.1 #1674
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.
Hi, it is bad practice to write all the code in one component, you need to separate the logic into different components, for example Header, Footer, TodoItem, TodoList
src/App.tsx
Outdated
client | ||
.get<Todo[]>(`/todos?userId=${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.
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 👍
Let's improve your code
src/App.tsx
Outdated
client | ||
.get<Todo[]>(`/todos?userId=${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.
src/App.tsx
Outdated
.then(todosFromServer => { | ||
setVisibleTodos(todosFromServer); | ||
setLoading(false); | ||
}) | ||
.catch(() => { | ||
setError(true); | ||
setErrorMessage('Unable to load todos'); | ||
setLoading(false); | ||
}) | ||
.finally(() => { | ||
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.
.then(todosFromServer => { | |
setVisibleTodos(todosFromServer); | |
setLoading(false); | |
}) | |
.catch(() => { | |
setError(true); | |
setErrorMessage('Unable to load todos'); | |
setLoading(false); | |
}) | |
.finally(() => { | |
setLoading(false); | |
}); | |
.then(setVisibleTodos) | |
.catch(() => { | |
setError(true); | |
setErrorMessage('Unable to load todos') | |
}) | |
.finally(() => setLoading(false)); |
src/components/Footer/Footer.tsx
Outdated
return ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{visibleTodos.filter(todo => !todo.completed).length} items left |
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 this logic from jsx to the helper varible and use it here
src/components/Footer/Footer.tsx
Outdated
} | ||
|
||
setSelectedFilter('all'); | ||
getTodos().then(allTodos => setVisibleTodos(allTodos)); |
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.
Remove getTodos from here everywhere
src/components/Footer/Footer.tsx
Outdated
<a | ||
href="#/" | ||
className={classNames('filter__link', { | ||
selected: selectedFilter === 'all', |
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 enum for 'all', 'active', 'completed' and use it everywhere
src/components/Footer/Footer.tsx
Outdated
<a | ||
href="#/" | ||
className={classNames('filter__link', { | ||
selected: selectedFilter === 'all', | ||
})} | ||
data-cy="FilterLinkAll" | ||
onClick={() => { | ||
if (selectedFilter === 'all') { | ||
return; | ||
} | ||
|
||
setSelectedFilter('all'); | ||
getTodos().then(allTodos => setVisibleTodos(allTodos)); |
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 Object.values(your created enum)
and render these options with map()
method
src/components/Header/Header.tsx
Outdated
onSubmit={event => { | ||
event.preventDefault(); | ||
if (inputText.trim() === '') { | ||
setError(true); | ||
setErrorMessage('Title should not be empty'); | ||
} else { | ||
createNewTodo(inputText, setError, setErrorMessage); | ||
} | ||
}} |
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 this logic from jsx to the helper function
src/components/TodoList/TodoList.tsx
Outdated
</label> | ||
|
||
<span data-cy="TodoTitle" className="todo__title"> | ||
{todo.title} |
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 destructuring for 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.
Almost done!
Let's make your code better
src/components/Footer/Footer.tsx
Outdated
switch (curFilter) { | ||
case FilterEnum.ALL: | ||
getTodos().then(allTodos => setVisibleTodos(allTodos)); | ||
break; | ||
case FilterEnum.ACTIVE: | ||
getActiveTodos().then(activeTodos => | ||
setVisibleTodos(activeTodos), | ||
); | ||
break; | ||
case FilterEnum.COMPLETED: | ||
getCompletedTodos().then(completedTodos => | ||
setVisibleTodos(completedTodos), | ||
); | ||
break; | ||
default: | ||
break; | ||
} |
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 this logic from jsx
src/api/todos.ts
Outdated
export const getCompletedTodos = () => { | ||
return client | ||
.get<Todo[]>(`/todos?userId=${USER_ID}`) | ||
.then(allTodos => allTodos.filter(todo => todo.completed)); | ||
}; | ||
|
||
export const getActiveTodos = () => { | ||
return client | ||
.get<Todo[]>(`/todos?userId=${USER_ID}`) | ||
.then(allTodos => allTodos.filter(todo => !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.
|
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.
Well done
<section className="todoapp__main" data-cy="TodoList"> | ||
{visibleTodos.map(({ id, title, completed }) => { | ||
return ( | ||
<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.
Create separate component for Todo
DEMO LINK