Skip to content
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
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ Implement a simple [TODO app](https://mate-academy.github.io/react_todo-app/) th

![todoapp](./description/todoapp.gif)

1. Learn the markup in `App.tsx`.
2. Show only a field to create a new todo if there are no todos yet.
3. Use React Context to manage todos.
4. Each todo should have an `id` (you can use `+new Date()`), a `title`, and a `completed` status (`false` by default).
5. Save `todos` to `localStorage` using `JSON.stringify` after each change.
6. Display the number of not completed todos in `TodoApp`.
7. Implement filtering by status (`All`/`Active`/`Completed`).
8. Add the ability to delete a todo using the `x` button.
9. Implement the `clearCompleted` button (disabled if there are no completed todos).
10. Implement individual todo status toggling.
11. Implement the `toggleAll` checkbox (checked only when all todos are completed).
12. Enable inline editing for the `TodoItem`:
1. - Learn the markup in `App.tsx`.
2. - Show only a field to create a new todo if there are no todos yet.
3. - Use React Context to manage todos.
4. - Each todo should have an `id` (you can use `+new Date()`), a `title`, and a `completed` status (`false` by default).
5. - Save `todos` to `localStorage` using `JSON.stringify` after each change.
6. - Display the number of not completed todos in `TodoApp`.
7. - Implement filtering by status (`All`/`Active`/`Completed`).
8. - Add the ability to delete a todo using the `x` button.
9. - Implement the `clearCompleted` button (disabled if there are no completed todos).
10. - Implement individual todo status toggling.
11. - Implement the `toggleAll` checkbox (checked only when all todos are completed).
12. - Enable inline editing for the `TodoItem`:
- Double-clicking on the todo title shows a text field instead of the title and `deleteButton`.
- Form submission saves changes (press `Enter` to save).
- Trim the saved text.
- Delete the todo if the title is empty.
- Save changes `onBlur`.
- Pressing `Escape` cancels editing (use `onKeyUp` and check if `event.key === 'Escape'`).
+ Pressing `Escape` cancels editing (use `onKeyUp` and check if `event.key === 'Escape'`).

![todoedit](./description/edittodo.gif)

Expand All @@ -33,4 +33,4 @@ Implement a simple [TODO app](https://mate-academy.github.io/react_todo-app/) th
- Implement a solution following the [React task guidelines](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Open another terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your GitHub username in the [DEMO LINK](https://<your_account>.github.io/react_todo-app/) and add it to the PR description.
- Replace `<your_account>` with your GitHub username in the [DEMO LINK](https://RVDotsenko.github.io/react_todo-app/) and add it to the PR description.
292 changes: 161 additions & 131 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,156 +1,186 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import React, { useContext, useEffect, useRef, useState } from 'react';
import cn from 'classnames';
import { DispatchContext, StateContext } from './context/TodoContext';
import { TodoComponent } from './components/Todo';

export const App: React.FC = () => {
const dispatch = useContext(DispatchContext);
const { todos, filteredTodos, todosToComplete, selectedFilter } =
useContext(StateContext);

const [newTodoTitle, setNewTodoTitle] = useState('');

const inputRef = useRef<HTMLInputElement>(null);
const isHydrated = useRef(false);

/* Hydrate todos from local storage */
useEffect(() => {
const localTodos = localStorage.getItem('todos');

if (localTodos) {
dispatch({
type: 'HYDRATE_TODOS',
payload: JSON.parse(localTodos),
});
}

isHydrated.current = true;
}, [dispatch]);

/* Save todos to local storage */
useEffect(() => {
if (!isHydrated.current) {
return;
}

localStorage.setItem('todos', JSON.stringify(todos));
}, [todos]);

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

const trimmedTitle = newTodoTitle.trim();

if (trimmedTitle.length > 0) {
dispatch({ type: 'ADD_TODO', payload: { title: trimmedTitle } });
setNewTodoTitle('');

if (inputRef.current) {
inputRef.current.focus();
}
}
};

const handleGlobalClick = (e: React.MouseEvent<HTMLDivElement>) => {
const target = e.target as HTMLElement;

if (
target.closest('[data-cy="TodoDelete"]') ||
target.closest('[data-cy="ClearCompletedButton"]')
) {
inputRef.current?.focus();
}
};

return (
<div className="todoapp">
<div className="todoapp" onClick={e => handleGlobalClick(e)}>
<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>
{todos.length !== 0 && (
<button
type="button"
className={cn('todoapp__toggle-all', {
active: todosToComplete === 0 && todos.length !== 0,
})}
data-cy="ToggleAllButton"
onClick={() =>
dispatch({
type: 'CHECK_AS_COMPLETED',
payload: {
completed: todosToComplete === 0,
},
})
}
/>
)}

<form
onSubmit={event => handleSubmit(event)}
onBlur={() => handleSubmit()}
>
<input
ref={inputRef}
data-cy="NewTodoField"
type="text"
className="todoapp__new-todo"
placeholder="What needs to be done?"
value={newTodoTitle}
onChange={event => setNewTodoTitle(event.target.value)}
autoFocus
/>
</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>
{filteredTodos.map(todo => (
<TodoComponent key={todo.id} todo={todo} />
))}
</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>
{todos.length !== 0 && (
<footer className="todoapp__footer" data-cy="Footer">
<span className="todo-count" data-cy="TodosCounter">
{`${todosToComplete} items left`}
</span>

<a
href="#/completed"
className="filter__link"
data-cy="FilterLinkCompleted"
<nav className="filter" data-cy="Filter">
<a
href="#/"
className={cn('filter__link', {
selected: selectedFilter === 'all',
})}
data-cy="FilterLinkAll"
onClick={() => {
dispatch({
type: 'SET_SELECTED_FILTER',
payload: { selectedFilter: 'all' },
});
}}
>
All
</a>

<a
href="#/active"
className={cn('filter__link', {
selected: selectedFilter === 'active',
})}
data-cy="FilterLinkActive"
onClick={() => {
dispatch({
type: 'SET_SELECTED_FILTER',
payload: { selectedFilter: 'active' },
});
}}
>
Active
</a>

<a
href="#/completed"
className={cn('filter__link', {
selected: selectedFilter === 'completed',
})}
data-cy="FilterLinkCompleted"
onClick={() => {
dispatch({
type: 'SET_SELECTED_FILTER',
payload: {
selectedFilter: 'completed',
},
});
}}
>
Completed
</a>
</nav>

<button
type="button"
className={cn('todoapp__clear-completed', {
'is-invisible': todosToComplete === todos.length,
})}
disabled={todosToComplete === todos.length}
data-cy="ClearCompletedButton"
onClick={() => dispatch({ type: 'CLEAR_COMPLETED' })}
>
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>
Clear completed
</button>
</footer>
)}
</div>
</div>
);
Expand Down
Loading
Loading