Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

KatOlista
Copy link

Copy link

@FrontKid FrontKid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job!

Copy link

@BudnikOleksii BudnikOleksii left a 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.

Comment on lines 7 to 12
enum FilterOption {
Default = '',
All = 'All',
Active = 'Active',
Completed = 'Completed',
}

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

Comment on lines 16 to 17
function filterTodos(option: FilterOption, todos: Todo[]) {
const filteredTodos = todos.filter(todo => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to utils folder


const OPTIONS = [FilterOption.All, FilterOption.Completed, FilterOption.Active];

export const Footer: React.FC<Props> = () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export const Footer: React.FC<Props> = () => {
export const Footer = () => {

If you don't have any props the types for them are redundant

Comment on lines 53 to 61
useEffect(() => {
setFilteredTodos(filterTodos(selectedOption, todos));
setHasCompleted(todos.some(todo => todo.completed));
}, [selectedOption, isTodoChange, changingItems]);

const handleFilterTodos = (option: FilterOption) => {
setFilteredTodos(filterTodos(option, todos));
setSelectedOption(option);
};

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.

Comment on lines 72 to 78
setTempTodo({
id: 0,
title: trimmedInputValue,
userId: USER_ID,
completed: false,
});
addTodo({ title: trimmedInputValue, userId: USER_ID, completed: false });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);

Comment on lines 51 to 68
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Comment on lines 99 to 101
if (!trimmedInputValue) {
handleRemoveTodo(todo);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!trimmedInputValue) {
handleRemoveTodo(todo);
}
if (!trimmedInputValue) {
handleRemoveTodo(todo);
return;
}

You have to return after this as in this case, it tries to update todo too and I get an error.
image
image

Comment on lines 119 to 121
useEffect(() => {
setIsLoading(changingItems.includes(id));
}, [changingItems]);

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

Comment on lines 6 to 8
type Props = {};

export const TodoList: React.FC<Props> = () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type Props = {};
export const TodoList: React.FC<Props> = () => {
export const TodoList = () => {

You don't have any props

Comment on lines 6 to 13
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',
}

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

Copy link

@roman-mirzoian roman-mirzoian left a 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 */

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

Comment on lines 15 to 23
useEffect(() => {
if (timerId.current) {
window.clearTimeout(timerId.current);
}

timerId.current = window.setTimeout(() => {
setAlarm(ErrorMessage.Default);
}, 3000);
}, [alarm]);

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const timerId = useRef<number>(0);
const timerIdRef = useRef<number>(0);

@@ -0,0 +1,78 @@
/* eslint-disable jsx-a11y/control-has-associated-label */

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

return (
<footer className="todoapp__footer" data-cy="Footer">
<span className="todo-count" data-cy="TodosCounter">
{`${counterTodos} items left`}

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.


return (
<header className="todoapp__header">
{ !!todos.length && (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{ !!todos.length && (
{!!todos.length && (

@@ -0,0 +1,173 @@
/* eslint-disable jsx-a11y/control-has-associated-label */

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

@@ -0,0 +1,29 @@
/* eslint-disable jsx-a11y/control-has-associated-label */

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

todo={tempTodo}
/>
)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@@ -0,0 +1,101 @@
/* eslint-disable jsx-a11y/control-has-associated-label */

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
Copy link

@roman-mirzoian roman-mirzoian left a 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.

Copy link

@roman-mirzoian roman-mirzoian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants