Skip to content

Commit a164a70

Browse files
committed
solution
1 parent 924f40a commit a164a70

File tree

5 files changed

+51
-120
lines changed

5 files changed

+51
-120
lines changed

src/components/TodoItem.1.tsx

Lines changed: 0 additions & 85 deletions
This file was deleted.

src/components/TodoItem.tsx

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { Dispatch, SetStateAction } from 'react';
1+
import React, { useContext, Dispatch, SetStateAction } from 'react';
2+
import { TodoContext } from '../store/TodoContext';
3+
import { useTodoItem } from '../hooks/useTodoItem';
4+
5+
import cn from 'classnames';
26

37
import { Todo } from '../types/Todo';
48

@@ -8,40 +12,17 @@ export type Props = {
812
setShowEditId: Dispatch<SetStateAction<number | null>>;
913
};
1014

11-
import cn from 'classnames';
12-
import React, { useContext, useState } from 'react';
13-
import { TodoContext } from '../store/TodoContext';
14-
1515
export const TodoItem: React.FC<Props> = ({
1616
todo,
1717
showEditId,
1818
setShowEditId,
1919
}) => {
20-
const { changeComplete, handleDeleteTodo, editTitle, setIsDeleted } =
21-
useContext(TodoContext);
22-
23-
const [title, setTitle] = useState(todo.title);
24-
25-
const handleSubmitEditedTodo = (event: React.FormEvent<HTMLFormElement>) => {
26-
event.preventDefault();
27-
28-
if (!title.trim().length) {
29-
handleDeleteTodo(todo);
30-
setIsDeleted(true);
31-
} else if (title.trim() === todo.title) {
32-
setShowEditId(null);
33-
} else if (showEditId && title.trim()) {
34-
editTitle(todo, title);
35-
setShowEditId(null);
36-
}
37-
};
38-
39-
const handleEscape = (e: React.KeyboardEvent) => {
40-
if (e.key === 'Escape') {
41-
setTitle(todo.title);
42-
setShowEditId(null);
43-
}
44-
};
20+
const { changeComplete, handleDeleteTodo } = useContext(TodoContext);
21+
const { handleEscape, handleSubmitEditedTodo, title, setTitle } = useTodoItem(
22+
todo,
23+
setShowEditId,
24+
showEditId,
25+
);
4526

4627
return (
4728
<div data-cy="Todo" className={cn('todo', { completed: todo.completed })}>

src/components/TodoList.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { useContext, useState } from 'react';
22

3-
// import { TodoItem } from './TodoItem';
43
import { TodoContext } from '../store/TodoContext';
54
import { TodoItem } from './TodoItem';
65

src/hooks/useTodoItem.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React, { Dispatch, SetStateAction, useContext, useState } from 'react';
2+
import { TodoContext } from '../store/TodoContext';
3+
import { Todo } from '../types/Todo';
4+
5+
export const useTodoItem = (
6+
todo: Todo,
7+
setShowEditId: Dispatch<SetStateAction<number | null>>,
8+
showEditId: boolean,
9+
) => {
10+
const [title, setTitle] = useState(todo.title);
11+
12+
const { editTitle, setIsDeleted, handleDeleteTodo } = useContext(TodoContext);
13+
14+
const handleSubmitEditedTodo = (event: React.FormEvent<HTMLFormElement>) => {
15+
event.preventDefault();
16+
17+
if (!title.trim().length) {
18+
handleDeleteTodo(todo);
19+
setIsDeleted(true);
20+
} else if (title.trim() === todo.title) {
21+
setShowEditId(null);
22+
} else if (showEditId && title.trim()) {
23+
editTitle(todo, title);
24+
setShowEditId(null);
25+
}
26+
};
27+
28+
const handleEscape = (e: React.KeyboardEvent) => {
29+
if (e.key === 'Escape') {
30+
setTitle(todo.title);
31+
setShowEditId(null);
32+
}
33+
};
34+
35+
return { handleEscape, handleSubmitEditedTodo, title, setTitle };
36+
};

src/store/TodoContext.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import React, {
66
useMemo,
77
useState,
88
} from 'react';
9-
10-
import { Todo } from '../types/Todo';
11-
import { Field } from '../types/Field';
129
import {
1310
getLocaleStorageTodos,
1411
setLocaleStorageTodos,
1512
} from '../utils/localStorageTodos';
13+
1614
import { filterByTodos, filteredTodos } from '../service/service';
1715

16+
import { Todo } from '../types/Todo';
17+
import { Field } from '../types/Field';
18+
1819
type TodoContextType = {
1920
todos: Todo[];
2021
field: Field;
@@ -153,7 +154,6 @@ export const TodoProvider: React.FC<Props> = ({ children }) => {
153154
editField,
154155
changeComplete,
155156
changeCompleteAll,
156-
157157
deleteCompletedTodos,
158158
}),
159159
[

0 commit comments

Comments
 (0)