Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
minimal005 committed Dec 23, 2024
1 parent 924f40a commit a164a70
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 120 deletions.
85 changes: 0 additions & 85 deletions src/components/TodoItem.1.tsx

This file was deleted.

41 changes: 11 additions & 30 deletions src/components/TodoItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Dispatch, SetStateAction } from 'react';
import React, { useContext, Dispatch, SetStateAction } from 'react';
import { TodoContext } from '../store/TodoContext';
import { useTodoItem } from '../hooks/useTodoItem';

import cn from 'classnames';

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

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

import cn from 'classnames';
import React, { useContext, useState } from 'react';
import { TodoContext } from '../store/TodoContext';

export const TodoItem: React.FC<Props> = ({
todo,
showEditId,
setShowEditId,
}) => {
const { changeComplete, handleDeleteTodo, editTitle, setIsDeleted } =
useContext(TodoContext);

const [title, setTitle] = useState(todo.title);

const handleSubmitEditedTodo = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

if (!title.trim().length) {
handleDeleteTodo(todo);
setIsDeleted(true);
} else if (title.trim() === todo.title) {
setShowEditId(null);
} else if (showEditId && title.trim()) {
editTitle(todo, title);
setShowEditId(null);
}
};

const handleEscape = (e: React.KeyboardEvent) => {
if (e.key === 'Escape') {
setTitle(todo.title);
setShowEditId(null);
}
};
const { changeComplete, handleDeleteTodo } = useContext(TodoContext);
const { handleEscape, handleSubmitEditedTodo, title, setTitle } = useTodoItem(
todo,
setShowEditId,
showEditId,
);

return (
<div data-cy="Todo" className={cn('todo', { completed: todo.completed })}>
Expand Down
1 change: 0 additions & 1 deletion src/components/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useContext, useState } from 'react';

// import { TodoItem } from './TodoItem';
import { TodoContext } from '../store/TodoContext';
import { TodoItem } from './TodoItem';

Expand Down
36 changes: 36 additions & 0 deletions src/hooks/useTodoItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { Dispatch, SetStateAction, useContext, useState } from 'react';
import { TodoContext } from '../store/TodoContext';
import { Todo } from '../types/Todo';

export const useTodoItem = (
todo: Todo,
setShowEditId: Dispatch<SetStateAction<number | null>>,
showEditId: boolean,
) => {
const [title, setTitle] = useState(todo.title);

const { editTitle, setIsDeleted, handleDeleteTodo } = useContext(TodoContext);

const handleSubmitEditedTodo = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

if (!title.trim().length) {
handleDeleteTodo(todo);
setIsDeleted(true);
} else if (title.trim() === todo.title) {
setShowEditId(null);
} else if (showEditId && title.trim()) {
editTitle(todo, title);
setShowEditId(null);
}
};

const handleEscape = (e: React.KeyboardEvent) => {
if (e.key === 'Escape') {
setTitle(todo.title);
setShowEditId(null);
}
};

return { handleEscape, handleSubmitEditedTodo, title, setTitle };
};
8 changes: 4 additions & 4 deletions src/store/TodoContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import React, {
useMemo,
useState,
} from 'react';

import { Todo } from '../types/Todo';
import { Field } from '../types/Field';
import {
getLocaleStorageTodos,
setLocaleStorageTodos,
} from '../utils/localStorageTodos';

import { filterByTodos, filteredTodos } from '../service/service';

import { Todo } from '../types/Todo';
import { Field } from '../types/Field';

type TodoContextType = {
todos: Todo[];
field: Field;
Expand Down Expand Up @@ -153,7 +154,6 @@ export const TodoProvider: React.FC<Props> = ({ children }) => {
editField,
changeComplete,
changeCompleteAll,

deleteCompletedTodos,
}),
[
Expand Down

0 comments on commit a164a70

Please sign in to comment.