-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
implement update logic, failed from 2 to 5 tests, every time different. Work in progress #806
base: master
Are you sure you want to change the base?
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!
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.
Looks good 🚀
Left some comments to improve your suggestion. Check them and fix the bug when editing todo is empty it deletes todo, but then it tries to update it.
src/components/Footer/Footer.tsx
Outdated
enum FilterOption { | ||
Default = '', | ||
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.
Move types to the types folder
src/components/Footer/Footer.tsx
Outdated
function filterTodos(option: FilterOption, todos: Todo[]) { | ||
const filteredTodos = todos.filter(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 to utils folder
src/components/Footer/Footer.tsx
Outdated
|
||
const OPTIONS = [FilterOption.All, FilterOption.Completed, FilterOption.Active]; | ||
|
||
export const Footer: React.FC<Props> = () => { |
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 Footer: React.FC<Props> = () => { | |
export const Footer = () => { |
If you don't have any props the types for them are redundant
src/components/Footer/Footer.tsx
Outdated
useEffect(() => { | ||
setFilteredTodos(filterTodos(selectedOption, todos)); | ||
setHasCompleted(todos.some(todo => todo.completed)); | ||
}, [selectedOption, isTodoChange, changingItems]); | ||
|
||
const handleFilterTodos = (option: FilterOption) => { | ||
setFilteredTodos(filterTodos(option, todos)); | ||
setSelectedOption(option); | ||
}; |
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 do setFilteredTodos
in this case twice.
src/components/Header/Header.tsx
Outdated
setTempTodo({ | ||
id: 0, | ||
title: trimmedInputValue, | ||
userId: USER_ID, | ||
completed: false, | ||
}); | ||
addTodo({ title: trimmedInputValue, userId: USER_ID, 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.
setTempTodo({ | |
id: 0, | |
title: trimmedInputValue, | |
userId: USER_ID, | |
completed: false, | |
}); | |
addTodo({ title: trimmedInputValue, userId: USER_ID, completed: false }); | |
const newTodo = { | |
title: trimmedInputValue, | |
userId: USER_ID, | |
completed: false, | |
}; | |
setTempTodo({ | |
...newTodo, | |
id: 0, | |
}); | |
addTodo(newTodo); |
src/components/TodoItem/TodoItem.tsx
Outdated
const handleUpdateTodo = (val: boolean | string) => { | ||
let newTodo = todo; | ||
|
||
if (typeof val === 'boolean') { | ||
newTodo = { | ||
...todo, | ||
completed: val, | ||
}; | ||
} | ||
|
||
if (typeof val === 'string' && val.trim() !== '') { | ||
newTodo = { | ||
...todo, | ||
title: val, | ||
}; | ||
} | ||
|
||
if (newTodo) { |
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 handleUpdateTodo = (val: boolean | string) => { | |
let newTodo = todo; | |
if (typeof val === 'boolean') { | |
newTodo = { | |
...todo, | |
completed: val, | |
}; | |
} | |
if (typeof val === 'string' && val.trim() !== '') { | |
newTodo = { | |
...todo, | |
title: val, | |
}; | |
} | |
if (newTodo) { | |
const handleUpdateTodo = (updateFields: Partial<Todo>) => { | |
const updatedTodo = { | |
...todo, | |
...updateFields, | |
}; |
You can pass directly the fields to update and in this case, you don't need such strange checks. Something like this.
handleUpdateTodo({ completed: !isCompleted });
Also, you always have todo so the last check is redundant.
src/components/TodoItem/TodoItem.tsx
Outdated
if (!trimmedInputValue) { | ||
handleRemoveTodo(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.
src/components/TodoItem/TodoItem.tsx
Outdated
useEffect(() => { | ||
setIsLoading(changingItems.includes(id)); | ||
}, [changingItems]); |
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 isLoading
state and this useEffect
.
Just const isLoading = changingItems.includes(id);
enough
src/components/TodoList/TodoList.tsx
Outdated
type Props = {}; | ||
|
||
export const TodoList: React.FC<Props> = () => { |
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.
type Props = {}; | |
export const TodoList: React.FC<Props> = () => { | |
export const TodoList = () => { |
You don't have any props
export enum ErrorMessage { | ||
Default = '', | ||
isLoadTodoError = 'Unable to load todos', | ||
isTitleEmpty = 'Title should not be empty', | ||
isUnableAddTodo = 'Unable to add a todo', | ||
isUnableDeleteTodo = 'Unable to delete a todo', | ||
isUnableUpdateTodo = 'Unable to update a 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 enum to types folder
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 there is quite a bit of work to be done.
Also, now the temporary state of an unupdated todo is saved after closing the editing mode and saves that state for the next such mode:
https://gyazo.com/d88cbd12c346e3f76aabe94f6a736136
@@ -0,0 +1,45 @@ | |||
/* eslint-disable jsx-a11y/control-has-associated-label */ |
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.
Try to rewrite the code so that it is not necessary to disable the linter
useEffect(() => { | ||
if (timerId.current) { | ||
window.clearTimeout(timerId.current); | ||
} | ||
|
||
timerId.current = window.setTimeout(() => { | ||
setAlarm(ErrorMessage.Default); | ||
}, 3000); | ||
}, [alarm]); |
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.
setAlarm, | ||
} = useContext(TodosContext); | ||
|
||
const timerId = useRef<number>(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.
const timerId = useRef<number>(0); | |
const timerIdRef = useRef<number>(0); |
src/components/Footer/Footer.tsx
Outdated
@@ -0,0 +1,78 @@ | |||
/* eslint-disable jsx-a11y/control-has-associated-label */ |
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.
Try to rewrite the code so that it is not necessary to disable the linter
src/components/Footer/Footer.tsx
Outdated
return ( | ||
<footer className="todoapp__footer" data-cy="Footer"> | ||
<span className="todo-count" data-cy="TodosCounter"> | ||
{`${counterTodos} 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.
When there is only one todo there should not be plural in the message.
src/components/Header/Header.tsx
Outdated
|
||
return ( | ||
<header className="todoapp__header"> | ||
{ !!todos.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.
{ !!todos.length && ( | |
{!!todos.length && ( |
src/components/TodoItem/TodoItem.tsx
Outdated
@@ -0,0 +1,173 @@ | |||
/* eslint-disable jsx-a11y/control-has-associated-label */ |
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.
Try to rewrite the code so that it is not necessary to disable the linter
src/components/TodoList/TodoList.tsx
Outdated
@@ -0,0 +1,29 @@ | |||
/* eslint-disable jsx-a11y/control-has-associated-label */ |
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.
Try to rewrite the code so that it is not necessary to disable the linter
src/components/TodoList/TodoList.tsx
Outdated
todo={tempTodo} | ||
/> | ||
)} | ||
|
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.
@@ -0,0 +1,101 @@ | |||
/* eslint-disable jsx-a11y/control-has-associated-label */ |
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.
Try to rewrite the code so that it is not necessary to disable the linter
…ename var, fix plural, rewrite setTimeout into useEffect
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 there is quite a bit of work to be done.
Also, now the temporary state of an unupdated todo is saved after closing the editing mode and saves that state for the next such mode: https://gyazo.com/d88cbd12c346e3f76aabe94f6a736136
Good fixed, but it still doesn't work correct.
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 🔥
DEMO LINK