Skip to content
Open

Develop #1278

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
bdc5eea
add type definitions from previous versions
mpodaniev Feb 20, 2026
dac8b81
Show only a field to create a new todo
mpodaniev Feb 20, 2026
b49d47b
add TodoContext with provider for managing todos
mpodaniev Feb 20, 2026
fdb2b19
Show only a field to create a new todo
mpodaniev Feb 20, 2026
d3d7534
wrap App with TodoProvider in index.tsx
mpodaniev Feb 20, 2026
a6aeede
remove unused userId field from Todo type definition
mpodaniev Feb 20, 2026
4c6aba7
remove unnecessary blank lines in component files
mpodaniev Feb 20, 2026
6b1c0c3
add form submission logic and input handling in Header component
mpodaniev Feb 20, 2026
f76e729
persist todos to localStorage using useEffect
mpodaniev Feb 20, 2026
bd6fbde
render todos from context in TodoList component
mpodaniev Feb 20, 2026
7147589
display dynamic count of remaining todos in Footer component
mpodaniev Feb 20, 2026
69b25ad
add filtering logic and UI enhancements for todo list views
mpodaniev Feb 21, 2026
2a3c632
add removeTodo functionality to TodoContext and integrate it with Tod…
mpodaniev Feb 21, 2026
aa1b95c
add clearCompleted functionality to TodoContext and integrate it with…
mpodaniev Feb 21, 2026
1c310f5
add toggleTodo functionality to TodoContext and integrate it with Tod…
mpodaniev Feb 21, 2026
c1e459f
add toggleAll functionality to TodoContext and integrate it with Head…
mpodaniev Feb 21, 2026
8b8073e
add updateTodoTitle functionality to TodoContext and enable title edi…
mpodaniev Feb 22, 2026
9cdd440
add input focus management with useRef in TodoContext and integrate i…
mpodaniev Feb 22, 2026
59a3aa8
remove unnecessary input focus logic from TodoItem and refine inputTe…
mpodaniev Feb 22, 2026
5a8d9c9
update DEMO LINK in README.md with GitHub username
mpodaniev Feb 22, 2026
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://redfield-mp.github.io/react_todo-app/) and add it to the PR description.
155 changes: 11 additions & 144 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,156 +1,23 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import React, { useContext } from 'react';
import { Header } from './components/Header';
import { TodoList } from './components/TodoList';
import { Footer } from './components/Footer';
import { TodoContext } from './context/TodoContext';

export const App: React.FC = () => {
const { todos } = useContext(TodoContext);
const hasTodos = todos.length > 0;

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>

<Header />
{hasTodos && <TodoList />}
{/* 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>
{hasTodos && <Footer />}
</div>
</div>
);
Expand Down
70 changes: 70 additions & 0 deletions src/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { useContext } from 'react';
import { TodoContext } from '../context/TodoContext';
import classNames from 'classnames';

export const Footer = () => {
const { todos, filter, setFilter, clearCompleted } = useContext(TodoContext);
const notCompletedTodosCount = todos.filter(todo => !todo.completed).length;

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

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

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

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

export const Header = () => {
const [inputText, setInputText] = useState('');
const { todos, setTodos, toggleAll, inputRef, focusInput } =
useContext(TodoContext);

const allCompleted = todos.every(todo => todo.completed);

const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const trimmedInputText = inputText.trim();

if (!trimmedInputText) {
return;
}

const newTodo: Todo = {
id: Date.now(),
title: trimmedInputText,
completed: false,
};

setTodos(prevTodos => [...prevTodos, newTodo]);

setInputText('');
focusInput();
};

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: allCompleted,
})}
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={inputText}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
setInputText(e.target.value);
}}
autoFocus
onKeyUp={e => {
if (e.key === 'Escape') {
setInputText('');
focusInput();
}
}}
/>
</form>
</header>
);
};
Loading