Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
getchards committed Aug 24, 2024
1 parent ccbbcbd commit 585c315
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/components/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ type Props = {
};

export const TodoItem: React.FC<Props> = ({ todo }) => {
const { completed, id, title } = todo;
const { toggleTodo, deleteTodo, updateTodo } = useTodos();
const [isEditing, setIsEditing] = useState(false);
const [editedTitle, setEditedTitle] = useState(todo.title);
const [editedTitle, setEditedTitle] = useState(title);

const inputElement = useRef<HTMLInputElement>(null);

const handleTodoStatus = () => {
toggleTodo(todo.id);
toggleTodo(id);
};

const handleDeleteTodo = () => {
Expand All @@ -32,9 +33,9 @@ export const TodoItem: React.FC<Props> = ({ todo }) => {

const handleEditSubmit = () => {
if (editedTitle.trim() === '') {
deleteTodo(todo.id);
deleteTodo(id);
} else {
updateTodo(todo.id, { title: editedTitle.trim() });
updateTodo(id, { title: editedTitle.trim() });
}

setIsEditing(false);
Expand All @@ -44,7 +45,7 @@ export const TodoItem: React.FC<Props> = ({ todo }) => {
if (event.key === 'Enter') {
handleEditSubmit();
} else if (event.key === 'Escape') {
setEditedTitle(todo.title);
setEditedTitle(title);
setIsEditing(false);
}
};
Expand All @@ -58,15 +59,15 @@ export const TodoItem: React.FC<Props> = ({ todo }) => {
return (
<div
data-cy="Todo"
className={classNames('todo', { completed: todo.completed })}
className={classNames('todo', { completed: completed })}
>
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
onClick={handleTodoStatus}
checked={todo.completed}
checked={completed}
/>
</label>

Expand All @@ -86,7 +87,7 @@ export const TodoItem: React.FC<Props> = ({ todo }) => {
className="todo__title"
onDoubleClick={startEdit}
>
{todo.title}
{title}
</span>
)}
{!isEditing && (
Expand Down

0 comments on commit 585c315

Please sign in to comment.