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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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://qlaudy.github.io/react_todo-app/) and add it to the PR description.
194 changes: 57 additions & 137 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,156 +1,76 @@
/* eslint-disable jsx-a11y/label-has-associated-control */
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import React, { useContext, useRef, useState } from 'react';
import { TodoContext } from './context/TodoContext';
import { Status } from './types/Status';
import { Header } from './components/Header';
import { Footer } from './components/Footer';
import { TodoList } from './components/TodoList';

export const App: React.FC = () => {
return (
<div className="todoapp">
<h1 className="todoapp__title">todos</h1>
const [query, setQuery] = useState('');
const newTodoField = useRef<HTMLInputElement>(null);

<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"
/>
const { todos, addTodo, filter, deleteTodo, clearCompleted } =
useContext(TodoContext);

{/* 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>
const handleSubmit = (event: React.FormEvent) => {
event.preventDefault();
addTodo(query);
setQuery('');
};

<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>
const handleDeleteTodo = (id: number) => {
deleteTodo(id);
newTodoField.current?.focus();
};

<span data-cy="TodoTitle" className="todo__title">
Completed Todo
</span>
const visibleTodos = todos.filter(todo => {
if (filter === Status.Active) {
return !todo.completed;
}

{/* Remove button appears only on hover */}
<button type="button" className="todo__remove" data-cy="TodoDelete">
×
</button>
</div>
if (filter === Status.Completed) {
return todo.completed;
}

{/* 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>
return true;
});

<span data-cy="TodoTitle" className="todo__title">
Not Completed Todo
</span>
const handleClearCompleted = () => {
clearCompleted();
newTodoField.current?.focus();
};

<button type="button" className="todo__remove" data-cy="TodoDelete">
×
</button>
</div>
const activeTodosCount = todos.filter(todo => !todo.completed).length;

{/* 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>
return (
<div className="todoapp">
<h1 className="todoapp__title">todos</h1>

<button type="button" className="todo__remove" data-cy="TodoDelete">
×
</button>
</div>
</section>
<div className="todoapp__content">
<Header
handleSubmit={handleSubmit}
query={query}
setQuery={setQuery}
inputRef={newTodoField}
/>

{todos.length > 0 && (
<TodoList
visibleTodos={visibleTodos}
onDeleteTodo={handleDeleteTodo}
/>
)}

{/* 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>
{todos.length > 0 && (
<Footer
activeTodosCount={activeTodosCount}
handleClearCompleted={handleClearCompleted}
/>
)}
</div>
</div>
);
Expand Down
80 changes: 80 additions & 0 deletions src/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import classNames from 'classnames';
import { TodoContext } from '../context/TodoContext';
import React, { useContext } from 'react';
import { Status } from '../types/Status';

interface Props {
activeTodosCount: number;
handleClearCompleted: () => void;
}

export const Footer: React.FC<Props> = ({
activeTodosCount,
handleClearCompleted,
}) => {
const { todos, filter, setFilter } = useContext(TodoContext);

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

{/* Active link should have the 'selected' class */}
<nav className="filter" data-cy="Filter">
<a
href="#/"
className={classNames('filter__link', {
selected: filter === Status.All,
})}
data-cy="FilterLinkAll"
onClick={event => {
event.preventDefault();
setFilter(Status.All);
}}
>
All
</a>

<a
href="#/active"
className={classNames('filter__link', {
selected: filter === Status.Active,
})}
data-cy="FilterLinkActive"
onClick={event => {
event.preventDefault();
setFilter(Status.Active);
}}
>
Active
</a>

<a
href="#/completed"
className={classNames('filter__link', {
selected: filter === Status.Completed,
})}
data-cy="FilterLinkCompleted"
onClick={event => {
event.preventDefault();
setFilter(Status.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"
disabled={!todos.some(todo => todo.completed)}
onClick={handleClearCompleted}
>
Clear completed
</button>
</footer>
);
};
50 changes: 50 additions & 0 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import classNames from 'classnames';
import { TodoContext } from '../context/TodoContext';
import React, { useContext } from 'react';

interface Props {
handleSubmit: (event: React.FormEvent) => void;
query: string;
setQuery: (value: string) => void;
inputRef: React.RefObject<HTMLInputElement>;
}

export const Header: React.FC<Props> = ({
handleSubmit,
query,
setQuery,
inputRef,
}) => {
const { todos, toggleAll } = useContext(TodoContext);

return (
<header className="todoapp__header">
{/* this button should have `active` class only if all todos are completed */}

{todos.length > 0 && (
<button
type="button"
className={classNames('todoapp__toggle-all', {
active: todos.every(todo => todo.completed),
})}
data-cy="ToggleAllButton"
onClick={toggleAll}
/>
)}

{/* Add a todo on form submit */}
<form onSubmit={handleSubmit}>
<input
ref={inputRef}
data-cy="NewTodoField"
type="text"
className="todoapp__new-todo"
placeholder="What needs to be done?"
value={query}
onChange={event => setQuery(event.target.value)}
autoFocus
/>
</form>
</header>
);
};
Loading
Loading