Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add task solution #1086

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 10 additions & 146 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,156 +1,20 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import React, { useContext } from 'react';
import { TodoList } from './components/TodoList';
import { TodoHeader } from './components/TodoHeader';
import { TodoFooter } from './components/TodoFooter';
import { TodosContext } from './context';

export const App: React.FC = () => {
const { todos } = useContext(TodosContext);

return (
<div className="todoapp">
<h1 className="todoapp__title">todos</h1>

<div className="todoapp__content">
<header className="todoapp__header">
{/* this button should have `active` class only if all todos are completed */}
<button
type="button"
className="todoapp__toggle-all active"
data-cy="ToggleAllButton"
/>

{/* Add a todo on form submit */}
<form>
<input
data-cy="NewTodoField"
type="text"
className="todoapp__new-todo"
placeholder="What needs to be done?"
/>
</form>
</header>

<section className="todoapp__main" data-cy="TodoList">
{/* This is a completed todo */}
<div data-cy="Todo" className="todo completed">
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
checked
/>
</label>

<span data-cy="TodoTitle" className="todo__title">
Completed Todo
</span>

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

{/* This todo is an active todo */}
<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">
Not Completed Todo
</span>

<button type="button" className="todo__remove" data-cy="TodoDelete">
×
</button>
</div>

{/* This todo is being edited */}
<div data-cy="Todo" className="todo">
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
/>
</label>

{/* This form is shown instead of the title and remove button */}
<form>
<input
data-cy="TodoTitleField"
type="text"
className="todo__title-field"
placeholder="Empty todo will be deleted"
value="Todo is being edited now"
/>
</form>
</div>

{/* This todo is in loadind state */}
<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">
Todo is being saved now
</span>

<button type="button" className="todo__remove" data-cy="TodoDelete">
×
</button>
</div>
</section>

{/* Hide the footer if there are no todos */}
<footer className="todoapp__footer" data-cy="Footer">
<span className="todo-count" data-cy="TodosCounter">
3 items left
</span>

{/* Active link should have the 'selected' class */}
<nav className="filter" data-cy="Filter">
<a
href="#/"
className="filter__link selected"
data-cy="FilterLinkAll"
>
All
</a>

<a
href="#/active"
className="filter__link"
data-cy="FilterLinkActive"
>
Active
</a>

<a
href="#/completed"
className="filter__link"
data-cy="FilterLinkCompleted"
>
Completed
</a>
</nav>

{/* this button should be disabled if there are no completed todos */}
<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
>
Clear completed
</button>
</footer>
<TodoHeader />
<TodoList />
{todos.length > 0 && <TodoFooter />}
</div>
</div>
);
Expand Down
57 changes: 57 additions & 0 deletions src/components/TodoFooter/TodoFooter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React, { useContext } from 'react';
import { TodosContext } from '../../context';
import { Filter } from '../../enums/Enums';
import classNames from 'classnames';

export const TodoFooter: React.FC = () => {
const { todos, activeTodos, filter, setFilter, deleteHandler } =
useContext(TodosContext);

const deleteCompleted = () => {
const idsToDelete: number[] = [];

todos.forEach(todo => {
const { completed, id } = todo;

if (completed) {
idsToDelete.push(id);
}
});

deleteHandler(idsToDelete);
};

return (
<footer className="todoapp__footer" data-cy="Footer">
<span className="todo-count" data-cy="TodosCounter">
{activeTodos} items left
</span>

<nav className="filter" data-cy="Filter">
{Object.values(Filter).map((value, i) => (
<a
href="#/"
key={value + i}
className={classNames('filter__link', {
selected: filter === value,
})}
data-cy={`FilterLink${value}`}
onClick={() => setFilter(value)}
>
{value}
</a>
))}
</nav>

<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
disabled={activeTodos === todos.length}
onClick={deleteCompleted}
>
Clear completed
</button>
</footer>
);
};
1 change: 1 addition & 0 deletions src/components/TodoFooter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TodoFooter';
59 changes: 59 additions & 0 deletions src/components/TodoHeader/TodoHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, { useContext } from 'react';
import { TodosContext } from '../../context';
import classNames from 'classnames';
import { myLocalStorage } from '../../localStorage';
import { Names } from '../../enums/Enums';

export const TodoHeader: React.FC = () => {
const {
todos,
activeTodos,
value,
headerInputRef,
setTodos,
setValue,
onSubmit,
} = useContext(TodosContext);

const toogleAll = () => {
const updatedTodos = todos.map(todo => {
const { completed } = todo;

if (activeTodos === 0 && completed) {
return { ...todo, completed: !completed };
}

return { ...todo, completed: true };
});

setTodos(updatedTodos);
myLocalStorage.setItem(Names.todos, JSON.stringify(updatedTodos));
};

return (
<header className="todoapp__header">
{todos.length > 0 && (
<button
type="button"
className={classNames('todoapp__toggle-all', {
active: activeTodos === 0 && activeTodos !== todos.length,
})}
data-cy="ToggleAllButton"
onClick={toogleAll}
/>
)}

<form onSubmit={onSubmit}>
<input
ref={headerInputRef}
data-cy="NewTodoField"
type="text"
className="todoapp__new-todo"
placeholder="What needs to be done?"
value={value}
onChange={event => setValue(event.target.value)}
/>
</form>
</header>
);
};
1 change: 1 addition & 0 deletions src/components/TodoHeader/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TodoHeader';
86 changes: 86 additions & 0 deletions src/components/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* eslint-disable jsx-a11y/label-has-associated-control */
import classNames from 'classnames';
import React from 'react';

interface Props {
id: number;
title: string;
editableTitle: string;
isTodoChecked: boolean;
editableTodoById: number;
todoInputRef: React.MutableRefObject<HTMLInputElement | null>;
setEditableTitle: (value: string) => void;
editHandler: (id: number, title: string) => void;
deleteHandler: (ids: number[]) => void;
toogleHandler: (id: number) => void;
onSubmit: (event: React.FormEvent, id: number) => void;
}

export const TodoItem: React.FC<Props> = React.memo(
({
id,
title,
editableTitle,
editableTodoById,
isTodoChecked,
todoInputRef,
setEditableTitle,
editHandler,
deleteHandler,
toogleHandler,
onSubmit,
}) => {
return (
<div
data-cy="Todo"
className={classNames('todo', { completed: isTodoChecked })}
>
<label className="todo__status-label">
<input
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
onChange={() => toogleHandler(id)}
checked={isTodoChecked}
/>
</label>

{editableTodoById === id ? (
<form onSubmit={event => onSubmit(event, id)}>
<input
ref={todoInputRef}
onBlur={event => onSubmit(event, id)}
data-cy="TodoTitleField"
type="text"
className="todo__title-field"
placeholder="Empty todo will be deleted"
value={editableTitle}
onChange={event => setEditableTitle(event.target.value)}
/>
</form>
) : (
<span
data-cy="TodoTitle"
className="todo__title"
onDoubleClick={() => editHandler(id, title)}
>
{title || 'Todo is being saved now'}
</span>
)}

{editableTodoById !== id && (
<button
type="button"
className="todo__remove"
data-cy="TodoDelete"
onClick={() => deleteHandler([id])}
>
×
</button>
)}
</div>
);
},
);

TodoItem.displayName = 'TodoItem';
Empty file.
Loading
Loading