-
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
Created Todo app #679
base: master
Are you sure you want to change the base?
Created Todo app #679
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 work 👍
Left some comments to improve your solution.
Use functional setState and array iteration methods to update your todos
src/TodosContext.tsx
Outdated
// [JSON.parse(localStorage.getItem('todos') | ||
// || '{}')] as 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.
Remove comments
src/components/TodoApp/TodoApp.tsx
Outdated
type Props = {}; | ||
|
||
export const TodoApp: React.FC<Props> = () => { | ||
const { setTodos } = useContext(TodosContext); |
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 hook for your context instead of passing TodosContext
every time
const useTodos = useContext(TodosContext);
src/components/TodoApp/TodoApp.tsx
Outdated
setTodos((prev: Todo[]) => [...prev, | ||
{ id: uuidv4(), title: newTodoTitle, 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((prev: Todo[]) => [...prev, | |
{ id: uuidv4(), title: newTodoTitle, completed: false }]); | |
setTodos((prev: Todo[]) => [ | |
...prev, | |
{ id: uuidv4(), title: newTodoTitle, completed: false } | |
]); |
src/components/TodoItem/TodoItem.tsx
Outdated
const index = todos.findIndex(({ id }) => id === todo.id); | ||
|
||
const handleRemoveTodo = () => { | ||
const todosCopy = [...todos]; | ||
|
||
todosCopy.splice(index, 1); | ||
|
||
setTodos(todosCopy); | ||
}; |
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.
Don't use splice
in this case, use filter
setTodos(previousTodos => previousTodos.filter(
currentTodo => todo.id !== currentTodo.id
));
src/components/TodoItem/TodoItem.tsx
Outdated
const handleCompleteTodo = ( | ||
event: React.ChangeEvent<HTMLInputElement>, | ||
) => { | ||
const todosCopy = [...todos]; | ||
|
||
todosCopy.splice(index, 1, { ...todo, completed: event.target.checked }); | ||
|
||
setTodos(todosCopy); | ||
}; |
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 map
instead of splice to change your todo
src/components/TodoItem/TodoItem.tsx
Outdated
const handleEditTodo = ( | ||
title: string, | ||
) => { | ||
const todosCopy = [...todos]; | ||
|
||
if (title && title !== todo.title) { | ||
todosCopy.splice(index, 1, { ...todo, title }); | ||
|
||
setTodos(todosCopy); | ||
setIsEditingEnabled(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.
The same, don't use splice
src/components/TodoList/TodoList.tsx
Outdated
export const TodoList = () => { | ||
const { todos, setTodos } = useContext(TodosContext); | ||
|
||
const [filterParam, setFilterParam] = useState('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 an enum for your filter types, don't pass just strings
src/components/TodoList/TodoList.tsx
Outdated
const todosCopy = [...todos].map(todo => ({ | ||
...todo, completed: event.target.checked, | ||
})); |
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.
map creates a new array, you don't need to create a copy.
Also, use functional setState
src/components/TodoList/TodoList.tsx
Outdated
{todos.filter(todo => { | ||
switch (filterParam) { | ||
case ('All'): | ||
return true; | ||
case ('Active'): | ||
return !todo.completed; | ||
case ('Completed'): | ||
return todo.completed; | ||
default: | ||
return 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.
Move it outside jsx, create a handler or a helper function for this, or just store filteredTodos in a variable(you can use useMemo for this variable if you want)
enum Status { | ||
All = 'All', | ||
Active = 'Active', | ||
Completed = '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.
So you have the enum but don't use it in TodoList component 🤷♂️
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.
Be more attentive to the comments and if you update some part of the code, take a look, maybe you should remove something extra.
src/TodosContext.tsx
Outdated
export const useTodos = () => { | ||
const todos = useContext(TodosContext); | ||
|
||
return todos; | ||
}; |
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 useTodos = () => { | |
const todos = useContext(TodosContext); | |
return todos; | |
}; | |
export const useTodos = () => { | |
return useContext(TodosContext); | |
}; |
src/components/TodoItem/TodoItem.tsx
Outdated
const handleRemoveTodo = () => { | ||
const todosCopy = [...todos]; | ||
|
||
todosCopy.splice(index, 1); | ||
|
||
setTodos(prev => prev.filter(({ id }) => id !== todo.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.
#679 (comment) - not fully fixed
src/components/TodoItem/TodoItem.tsx
Outdated
const handleEditTodo = ( | ||
title: string, | ||
) => { | ||
const todosCopy = [...todos]; | ||
|
||
if (title && title !== todo.title) { | ||
todosCopy.splice(index, 1, { ...todo, title }); | ||
|
||
setTodos(prev => prev.map(currentTodo => { | ||
if (currentTodo.id === todo.id) { | ||
return { ...currentTodo, title }; | ||
} | ||
|
||
return currentTodo; | ||
})); |
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.
#679 (comment) - not fully fixed
src/components/TodoList/TodoList.tsx
Outdated
const filteredTodos = useMemo(() => { | ||
return todos.filter(todo => { | ||
switch (filterParam) { | ||
case (Status.All): | ||
return todo; | ||
case (Status.Active): | ||
return !todo.completed; | ||
case (Status.Completed): | ||
return todo.completed; | ||
default: | ||
return todo; | ||
} | ||
}); | ||
}, [filterParam, todos]); |
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.
#679 (comment) - not fully fixed
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 🚀
export interface Todo { | ||
id: string, | ||
title: string, | ||
completed: true | 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.
completed: true | false, | |
completed: boolean, |
boolean type is the same
DEMO LINK