Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaDamage91 committed Jan 13, 2025
1 parent 4e3c5ae commit a0d8949
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions src/components/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ export const TodoItem: React.FC<Props> = ({ todo }) => {
}, 0);
}

const deleteTodo = (id: number) => {
const filteredTodos = todos.filter(todo => todo.id !== id);

Check failure on line 24 in src/components/TodoItem/TodoItem.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

'todo' is already declared in the upper scope on line 10 column 45

setTodos(filteredTodos);
localStorage.setItem('todos', JSON.stringify(filteredTodos));

const inputField = document.querySelector(".todoapp__new-todo") as HTMLInputElement;
inputField!.focus();
}

const editTodo = (title: string, id: number) => {
const updatedTodos = todos.map(todo => todo.id === id ? { ...todo, title } : todo);

setTodos(updatedTodos);
localStorage.setItem('todos', JSON.stringify(updatedTodos));

Check failure on line 37 in src/components/TodoItem/TodoItem.tsx

View workflow job for this annotation

GitHub Actions / run_linter (20.x)

'todo' is already declared in the upper scope on line 10 column 45
};

const handleKeyUp = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Escape') {
setIsEditForm(false);
}
};

const editTitle = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();

Expand All @@ -38,43 +61,20 @@ export const TodoItem: React.FC<Props> = ({ todo }) => {
};

const handleTitleBlur = (e: React.ChangeEvent<HTMLInputElement>) => {
const newTitle = e.target.value.trim();
const newTitleBlur = e.target.value.trim();

if (!newTitle) {
if (!newTitleBlur) {
deleteTodo(todo.id);
return;
}

if (newTitle !== todo.title) {
editTodo(newTitle, todo.id);
if (newTitleBlur !== todo.title) {
editTodo(newTitleBlur, todo.id);
}

setIsEditForm(false);
};

const editTodo = (title: string, id: number) => {
const updatedTodos = todos.map(todo => todo.id === id ? { ...todo, title } : todo);

setTodos(updatedTodos);
localStorage.setItem('todos', JSON.stringify(updatedTodos));
};

const handleKeyUp = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Escape') {
setIsEditForm(false);
}
};

const deleteTodo = (id: number) => {
const filteredTodos = todos.filter(todo => todo.id !== id);

setTodos(filteredTodos);
localStorage.setItem('todos', JSON.stringify(filteredTodos));

const inputField = document.querySelector(".todoapp__new-todo") as HTMLInputElement;
inputField!.focus();
}

const toggleTodoStatus = (id: number) => {
const updatedTodos = todos.map(todo =>
todo.id === id ? { ...todo, completed: !todo.completed } : todo
Expand Down

0 comments on commit a0d8949

Please sign in to comment.