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 #1051

Open
wants to merge 5 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
63 changes: 27 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
# React ToDo App

Implement a simple [TODO app](https://mate-academy.github.io/react_todo-app/) that functions as described below.

> If you are unsure about how a feature should work, open the real TodoApp and observe its behavior.

![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`:
- 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'`).

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

## Instructions

- Install the Prettier Extension and use these [VSCode settings](https://mate-academy.github.io/fe-program/tools/vscode/settings.json) to enable format on save.
- 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.
# TODO App

### Description

- Implemented a simple TODOs App

### Stack

- HTML (BEM)
- CSS (Bulma)
- JS
- Typescript
- React
- ReactDOM

### Tools

- ESlint
- Prettier
- Cypress
- Mochawesome
- Babel

### Demo links

- [Demo](https://AndriiZakharenko.github.io/todo-app/)

155 changes: 5 additions & 150 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,157 +1,12 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import { TodoProvider } from './Components/TodoContext';
import { TodoApp } from './Components/TodoApp';

export const App: React.FC = () => {
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>
</div>
</div>
<TodoProvider>
<TodoApp />
</TodoProvider>
);
};
66 changes: 66 additions & 0 deletions src/Components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { useContext } from 'react';
import cn from 'classnames';
import { ActionNames, FilterBy, TodoContext } from './TodoContext';

export const Footer: React.FC = () => {
const { todos, dispatch, handleFilterBy, filteredBy, originalTodos } =
useContext(TodoContext);

const activeTodosCount = originalTodos.filter(
todo => todo.completed === false,
).length;
const isDisabled = todos.some(todo => todo.completed);

const handleFilter = (
event: React.MouseEvent<HTMLAnchorElement>,
type: FilterBy,
) => {
event.preventDefault();

handleFilterBy(type);
};

return (
<>
{originalTodos.length > 0 && (
<footer className="todoapp__footer" data-cy="Footer">
<span className="todo-count" data-cy="TodosCounter">
{`${activeTodosCount} items left`}
</span>

<nav className="filter" data-cy="Filter">
{Object.values(FilterBy).map(filter => (
<a
key={filter}
href={`#/${filter === FilterBy.All ? '' : filter.toLowerCase()}`}
className={cn('filter__link', {
selected: filteredBy === filter,
})}
data-cy={`FilterLink${filter}`}
onClick={(event: React.MouseEvent<HTMLAnchorElement>) =>
handleFilter(event, filter)
}
>
{filter}
</a>
))}
</nav>

<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
disabled={!isDisabled}
onClick={() =>
dispatch({
type: ActionNames.ClearCompleted,
})
}
>
Clear completed
</button>
</footer>
)}
</>
);
};
87 changes: 87 additions & 0 deletions src/Components/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React, { useContext, useEffect, useRef, useState } from 'react';
import cn from 'classnames';
import { ActionNames, TodoContext } from './TodoContext';
import { Todo } from '../types/Todo';

export const Header: React.FC = ({}) => {
const { todos, dispatch, originalTodos } = useContext(TodoContext);
const [value, setValue] = useState('');

const handleOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
event.preventDefault();
setValue(event.target.value);
};

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

if (value.trim() === '') {
return;
}

const newTodo: Todo = {
id: 100,
completed: false,
title: value.trim(),
};

setValue('');

dispatch({ type: ActionNames.Add, payload: newTodo });
};

const isCompleted =
todos.length && todos.some(todo => todo.completed === false);

const isAllCompleted = originalTodos.every(todo => todo.completed);

const inputRef = useRef<HTMLInputElement>(null);

const focusInput = () => {
if (inputRef.current) {
inputRef.current.focus();
}
};

useEffect(() => {
if (isAllCompleted) {
focusInput();
}
}, [isAllCompleted]);

useEffect(() => {
focusInput();
}, [todos.length]);

return (
<header className="todoapp__header">
{originalTodos.length > 0 && (
<button
type="button"
data-cy="ToggleAllButton"
onClick={() =>
dispatch({ type: ActionNames.ToggleAllCompleted, payload: todos })
}
className={cn('todoapp__toggle-all', {
active: !isCompleted,
})}
/>
)}

<form onSubmit={handleKeyDown}>
<input
data-cy="NewTodoField"
type="text"
className={cn('todoapp__new-todo', {
active: isAllCompleted,
})}
placeholder="What needs to be done?"
autoFocus
value={value}
ref={inputRef}
onChange={handleOnChange}
/>
</form>
</header>
);
};
Loading
Loading