-
Notifications
You must be signed in to change notification settings - Fork 1.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
solution #1726
base: master
Are you sure you want to change the base?
solution #1726
Conversation
src/App.tsx
Outdated
useEffect(() => { | ||
getTodos() | ||
.then(response => { | ||
let filteredTodos = response; | ||
|
||
if (filter === 'active') { | ||
filteredTodos = response.filter(todo => todo.completed === false); | ||
} | ||
|
||
if (filter === 'completed') { | ||
filteredTodos = response.filter(todo => todo.completed === true); | ||
} | ||
|
||
setTodos(filteredTodos); | ||
setErrorMessage(''); | ||
}) | ||
.catch(() => { | ||
handleErrorMessage('Unable to load todos'); | ||
}); | ||
}, [filter]); |
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.
useEffect(() => { | |
getTodos() | |
.then(response => { | |
let filteredTodos = response; | |
if (filter === 'active') { | |
filteredTodos = response.filter(todo => todo.completed === false); | |
} | |
if (filter === 'completed') { | |
filteredTodos = response.filter(todo => todo.completed === true); | |
} | |
setTodos(filteredTodos); | |
setErrorMessage(''); | |
}) | |
.catch(() => { | |
handleErrorMessage('Unable to load todos'); | |
}); | |
}, [filter]); | |
useEffect(() => { | |
getTodos() | |
.then(response => { | |
setTodos(response); | |
setErrorMessage(''); | |
}) | |
.catch(() => { | |
handleErrorMessage('Unable to load todos'); | |
}); | |
}, []); | |
const filteredTodos = getFilteredTodos(todos, filter) |
to avoid redundant fetch requests
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, TodoList, TodoItem
src/App.tsx
Outdated
}, []); | ||
|
||
function getFilterTodos(todoForFilter: Todo[], forFilter: string): Todo[] { | ||
if (forFilter === 'active') { |
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/App.tsx
Outdated
|
||
function getFilterTodos(todoForFilter: Todo[], forFilter: string): Todo[] { | ||
if (forFilter === 'active') { | ||
return todoForFilter.filter(todo => todo.completed === 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.
return todoForFilter.filter(todo => todo.completed === false); | |
return todoForFilter.filter(todo => !todo.completed); |
src/App.tsx
Outdated
} | ||
|
||
if (forFilter === 'completed') { | ||
return todoForFilter.filter(todo => todo.completed === true); |
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.
return todoForFilter.filter(todo => todo.completed === true); | |
return todoForFilter.filter(todo => todo.completed); |
src/App.tsx
Outdated
}); | ||
}; | ||
|
||
function handleClearCompleted() { |
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 is important to follow the same way of creating functions everywhere, fix it everywhere
function handleClearCompleted() { | |
const handleClearCompleted = () => { |
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
}); | ||
}, []); | ||
|
||
function getFilterTodos(todoForFilter: Todo[], enumFilter: number): 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.
It is important to follow the same way of creating functions everywhere, fix it everywhere
function getFilterTodos(todoForFilter: Todo[], enumFilter: number): Todo[] { | |
const getFilterTodos = (todoForFilter: Todo[], enumFilter: number): Todo[] => { |
src/components/AddTodo/index.jsx
Outdated
@@ -0,0 +1 @@ | |||
export * from './AddTodo'; |
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/components/Footer/Footer.tsx
Outdated
return ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{[...todos].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 to the helper variable and use it here
{[...todos].filter(todo => !todo.completed).length} items left | |
{todos.filter(todo => !todo.completed).length} items left |
src/components/Footer/Footer.tsx
Outdated
<a | ||
href="#/" | ||
className={classNames('filter__link', !filter && 'selected')} | ||
data-cy="FilterLinkAll" | ||
onClick={() => { | ||
setFilter(Filter.all); | ||
}} | ||
> | ||
All | ||
</a> | ||
|
||
<a | ||
href="#/active" | ||
className={classNames( | ||
'filter__link', | ||
filter === Filter.active && 'selected', | ||
)} | ||
data-cy="FilterLinkActive" | ||
onClick={() => { | ||
setFilter(Filter.active); | ||
}} | ||
> | ||
Active | ||
</a> | ||
|
||
<a | ||
href="#/completed" | ||
className={classNames( | ||
'filter__link', | ||
filter === Filter.completed && 'selected', | ||
)} |
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(Filter)
and render these options with map()
method
src/components/Footer/Footer.tsx
Outdated
className="todoapp__clear-completed" | ||
data-cy="ClearCompletedButton" | ||
onClick={handleClearCompleted} | ||
disabled={todos.filter(todo => todo.completed === true).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.
Move it to helper variable
disabled={todos.filter(todo => todo.completed === true).length === 0} | |
disabled={todos.filter(todo => todo.completed).length === 0} |
src/components/TodoList/TodoList.tsx
Outdated
<div | ||
key={todo.id} | ||
data-cy="Todo" | ||
className={`todo ${todo.completed ? 'completed' : 'item-enter-done'}`} |
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 classnames library for add classes with conditions, 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.
src/App.tsx
Outdated
deleteTodo(todo.id).catch(() => { | ||
handleErrorMessage('Unable to delete a todo'); | ||
|
||
return Promise.reject({ id: todo.id }); // Передаємо id в reason |
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 all comments
return Promise.reject({ id: todo.id }); // Передаємо id в reason | |
return Promise.reject({ id: todo.id }); |
DEMO LINK