Skip to content

Commit

Permalink
add task next solution
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorKomara committed Nov 12, 2024
1 parent 781fa94 commit 686d85e
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 105 deletions.
33 changes: 33 additions & 0 deletions src/components/TempTodoItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable jsx-a11y/label-has-associated-control */
import classNames from 'classnames';

type Props = {
tempTodo: string;
};

export const TempTodoItem: React.FC<Props> = ({ tempTodo }) => {
return (
<div data-cy="Todo" className="todo">
<label className="todo__status-label">
<input data-cy="TodoStatus" type="checkbox" className="todo__status" />
</label>

<span data-cy="TodoTitle" className="todo__title">
{tempTodo}
</span>

{/* Remove button appears only on hover */}
<button type="button" className="todo__remove" data-cy="TodoDelete">
x
</button>

{/* overlay will cover the todo while it is being deleted or updated */}
<div data-cy="TodoLoader" className="modal overlay is-active">
<div
className={classNames('modal-background', 'has-background-white-ter')}
/>
<div className="loader" />
</div>
</div>
);
};
100 changes: 100 additions & 0 deletions src/components/TodoItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* eslint-disable jsx-a11y/label-has-associated-control */
import classNames from 'classnames';
import { Todo } from '../types/Todo';
import { FormEditingTitle } from './FormEditingTitle';

type Props = {
todo: Todo;
changeTodo: (todo: Todo) => void;
updatingAndDeletingCompletedTodos: Todo[] | [];
setUpdatingAndDeletingCompletedTodos: (
deletingCompletedTodos: Todo[] | [],
) => void;
editingTitle: number;
setEditingTitle: (editingTitle: number) => void;
titleEdit: string;
setTitleEdit: (titleEdit: string) => void;
handleChangeSubmit: (todo: Todo) => void;
deleteTodo: (todo: Todo) => void;
};

export const TodoItem: React.FC<Props> = ({
todo,
changeTodo,
updatingAndDeletingCompletedTodos,
setUpdatingAndDeletingCompletedTodos,
editingTitle,
setEditingTitle,
titleEdit,
setTitleEdit,
handleChangeSubmit,
deleteTodo,
}) => {
const { id, completed, title } = todo;

return (
<div
data-cy="Todo"
className={classNames('todo', { completed: completed })}
>
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
checked={completed}
onClick={() => {
changeTodo({ ...todo, completed: !completed });
setUpdatingAndDeletingCompletedTodos([todo]);
}}
/>
</label>

{editingTitle === id ? (
<FormEditingTitle
todo={todo}
titleEdit={titleEdit}
setTitleEdit={setTitleEdit}
handleChangeSubmit={handleChangeSubmit}
setEditingTitle={setEditingTitle}
/>
) : (
<>
<span
data-cy="TodoTitle"
className="todo__title"
onDoubleClick={() => {
setEditingTitle(id);
setTitleEdit(title);
}}
>
{title}
</span>

<button
type="button"
className="todo__remove"
data-cy="TodoDelete"
onClick={() => deleteTodo(todo)}
>
x
</button>
</>
)}

<div
data-cy="TodoLoader"
className={classNames('modal', 'overlay', {
'is-active': updatingAndDeletingCompletedTodos?.some(
task => task.id === todo.id,
),
})}
>
<div
className={classNames('modal-background', 'has-background-white-ter')}
/>
<div className="loader" />
</div>
</div>
);
};
126 changes: 21 additions & 105 deletions src/components/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable jsx-a11y/label-has-associated-control */
import classNames from 'classnames';
import { Todo } from '../types/Todo';
import { useState } from 'react';
import { FormEditingTitle } from './FormEditingTitle';
import { Todo } from '../types/Todo';
import { TempTodoItem } from './TempTodoItem';
import { TodoItem } from './TodoItem';

type Props = {
visibleTodos: Todo[];
Expand Down Expand Up @@ -54,111 +53,28 @@ export const TodoList: React.FC<Props> = ({
return (
<section className="todoapp__main" data-cy="TodoList">
{visibleTodos.map(todo => {
const { id, completed, title } = todo;

return (
<div
data-cy="Todo"
className={classNames('todo', { completed: completed })}
key={id}
>
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
checked={completed}
onClick={() => {
changeTodo({ ...todo, completed: !completed });
setUpdatingAndDeletingCompletedTodos([todo]);
}}
/>
</label>

{editingTitle === id ? (
<FormEditingTitle
todo={todo}
titleEdit={titleEdit}
setTitleEdit={setTitleEdit}
handleChangeSubmit={handleChangeSubmit}
setEditingTitle={setEditingTitle}
/>
) : (
<>
<span
data-cy="TodoTitle"
className="todo__title"
onDoubleClick={() => {
setEditingTitle(id);
setTitleEdit(title);
}}
>
{title}
</span>

<button
type="button"
className="todo__remove"
data-cy="TodoDelete"
onClick={() => deleteTodo(todo)}
>
x
</button>
</>
)}

{/* overlay will cover the todo while it is being deleted or updated */}
<div
data-cy="TodoLoader"
className={classNames('modal', 'overlay', {
'is-active': updatingAndDeletingCompletedTodos?.some(
task => task.id === todo.id,
),
})}
>
<div
className={classNames(
'modal-background',
'has-background-white-ter',
)}
/>
<div className="loader" />
</div>
</div>
<TodoItem
key={todo.id}
todo={todo}
changeTodo={changeTodo}
updatingAndDeletingCompletedTodos={
updatingAndDeletingCompletedTodos
}
setUpdatingAndDeletingCompletedTodos={
setUpdatingAndDeletingCompletedTodos
}
editingTitle={editingTitle}
setEditingTitle={setEditingTitle}
titleEdit={titleEdit}
setTitleEdit={setTitleEdit}
handleChangeSubmit={handleChangeSubmit}
deleteTodo={deleteTodo}
/>
);
})}

{tempTodo && (
<div data-cy="Todo" className="todo">
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
/>
</label>

<span data-cy="TodoTitle" className="todo__title">
{tempTodo}
</span>

{/* Remove button appears only on hover */}
<button type="button" className="todo__remove" data-cy="TodoDelete">
x
</button>

{/* overlay will cover the todo while it is being deleted or updated */}
<div data-cy="TodoLoader" className="modal overlay is-active">
<div
className={classNames(
'modal-background',
'has-background-white-ter',
)}
/>
<div className="loader" />
</div>
</div>
)}
{tempTodo && <TempTodoItem tempTodo={tempTodo} />}
</section>
);
};

0 comments on commit 686d85e

Please sign in to comment.