-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
dev #783
base: master
Are you sure you want to change the base?
dev #783
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 👍
Let's improve your code
src/App.tsx
Outdated
const handleSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
if (query.trim()) { | ||
setTodos([...todos, { id: Date.now(), title: query, 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.
setTodos([...todos, { id: Date.now(), title: query, completed: false }]); | |
const todo = { | |
id: Date.now(), | |
title: query, | |
completed: false, | |
}; | |
setTodos([...todos, todo]); |
<a href="#/completed">Completed</a> | ||
</li> | ||
</ul> | ||
<TodosContext.Provider |
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 provider to the context file, it will be a good practice
src/components/TodoList.tsx
Outdated
<ul className="todo-list" data-cy="todosList"> | ||
{todos.filter((todo) => { | ||
switch (todosFilter) { | ||
case '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.
src/components/TodoList.tsx
Outdated
{todos.filter((todo) => { | ||
switch (todosFilter) { | ||
case 'Active': | ||
return !todo.completed; | ||
case 'Completed': | ||
return todo.completed; | ||
default: | ||
return true; | ||
} | ||
}).map((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.
Move this logic from the jsx to the helper function and use it here
src/App.tsx
Outdated
completed: false, | ||
}; | ||
|
||
setTodos([...todos, 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.
setTodos([...todos, todo]); | |
setTodos(prevTodos => [...prevTodos, todo]); |
It is safer to pass updater callback when new state values depends on previous one
src/components/TodoContent.tsx
Outdated
setTodos(todos.map(todo => { | ||
return { ...todo, completed: !isCompleted }; | ||
})); |
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.
same here pass callback
src/components/TodoFooter.tsx
Outdated
const isCompleted = todos.some(todo => todo.completed); | ||
|
||
const todosLeft = todos.filter(todo => todo.completed === false).length; |
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 isCompleted = todos.some(todo => todo.completed); | |
const todosLeft = todos.filter(todo => todo.completed === false).length; | |
const isCompleted = todos.some(todo => todo.completed); | |
const todosLeft = todos.filter(todo => !todo.completed).length; |
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 improve your code
src/components/TodoItem.tsx
Outdated
case 'Escape': | ||
resetChange(); | ||
|
||
break; | ||
case 'Enter': |
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.
case 'Escape': | |
resetChange(); | |
break; | |
case 'Enter': | |
case 'Escape': | |
resetChange(); | |
break; | |
case 'Enter': |
src/components/TodoItem.tsx
Outdated
saveChange(); | ||
|
||
break; | ||
default: |
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.
saveChange(); | |
break; | |
default: | |
saveChange(); | |
break; | |
default: |
src/components/TodoList.tsx
Outdated
|
||
return ( | ||
<ul className="todo-list" data-cy="todosList"> | ||
{filterTodos(todos, todosFilter).map((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.
Consider will be better if you create a variable for this checking and use it here filterTodos(todos, todosFilter)
{filterTodos(todos, todosFilter).map((todo) => ( | |
{filterTodos(todos, todosFilter).map((todo) => ( |
src/hooks/useLocalStorage.ts
Outdated
if (data === null) { | ||
return startValue; | ||
} |
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.
if (data === null) { | |
return startValue; | |
} | |
if (!data) { | |
return startValue; | |
} |
src/components/TodoContent.tsx
Outdated
setTodos((prevTodos: Todo[]) => { | ||
const updatedTodos = prevTodos.map(todo => ({ | ||
...todo, | ||
completed: !isCompleted, | ||
})); | ||
|
||
return updatedTodos; | ||
}); |
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.
setTodos((prevTodos: Todo[]) => { | |
const updatedTodos = prevTodos.map(todo => ({ | |
...todo, | |
completed: !isCompleted, | |
})); | |
return updatedTodos; | |
}); | |
setTodos((prevTodos: Todo[]) => prevTodos.map(todo => ({ | |
...todo, | |
completed: !isCompleted, | |
})); | |
); |
src/components/TodoItem.tsx
Outdated
case 'Escape': | ||
resetChange(); | ||
break; | ||
|
||
case 'Enter': |
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.
Please move Enter and Escape to the enum as it will make a code cleaner
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 🔥
DEMO LINK