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
9 changes: 9 additions & 0 deletions .hintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": [
"development"
],
"hints": {
"axe/name-role-value": "off",
"axe/forms": "off"
}
}
12 changes: 7 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/scripts": "^2.1.3",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
155 changes: 2 additions & 153 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,157 +1,6 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
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>
);
return <TodoApp />;
};
76 changes: 76 additions & 0 deletions src/components/Footer/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-disable prettier/prettier */
import React, { useContext } from 'react';
import classNames from 'classnames';
import { TodoContext } from '../../context/TodoContext';
import { FILTERS } from '../../types/Filter';

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

const activeTodos = todos.filter(todo => !todo.completed).length;

const handleClearCompleted = () => {
setTodos(todos.filter(todo => !todo.completed));

setTimeout(() => {
(
document.querySelector(
Comment on lines +15 to +17

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates checklist item #12: "Show only NewTodo form if todos array is empty." Here, the footer (which includes filters and clear button) is only rendered when todos.length > 0; you should adjust the layout so that when todos is empty, only the new-todo field is shown, and when todos is not empty, the list and footer are visible as in the reference app.

'[data-cy="NewTodoField"]',
) as HTMLInputElement | null
)?.focus();
}, 0);
};

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">
<a
href="#/"
data-cy="FilterLinkAll"
className={classNames('filter__link', {
selected: filter === FILTERS.all,
})}
onClick={() => setFilter(FILTERS.all)}
>
All
</a>

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

<a
href="#/completed"
data-cy="FilterLinkCompleted"
className={classNames('filter__link', {
selected: filter === FILTERS.completed,
})}
onClick={() => setFilter(FILTERS.completed)}
>
Completed
</a>
</nav>

<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
disabled={!todos.some(todo => todo.completed)}
onClick={handleClearCompleted}
>
Clear completed
</button>
</footer>
);
};
1 change: 1 addition & 0 deletions src/components/Footer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Footer';
24 changes: 24 additions & 0 deletions src/components/TodoApp/TodoApp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint-disable prettier/prettier */
import React, { useContext } from 'react';
import { TodoContext } from '../../context/TodoContext';
import { TodoHeader } from '../TodoHeader';
import { TodoList } from '../TodoList';
import { Footer } from '../Footer';

export const TodoApp: React.FC = () => {
const { todos } = useContext(TodoContext);

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

<div className="todoapp__content">
<TodoHeader />

{todos.length > 0 && <TodoList />}

{todos.length > 0 && <Footer />}
</div>
</div>
);
};
1 change: 1 addition & 0 deletions src/components/TodoApp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TodoApp';
76 changes: 76 additions & 0 deletions src/components/TodoHeader/TodoHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-disable prettier/prettier */
import React, { useContext, useRef, useState } from 'react';
import classNames from 'classnames';
import { TodoContext } from '../../context/TodoContext';

export const TodoHeader: React.FC = () => {
const { todos, setTodos } = useContext(TodoContext);

const [title, setTitle] = useState('');
const inputRef = useRef<HTMLInputElement>(null);

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

const trimmedTitle = title.trim();

if (!trimmedTitle) {
inputRef.current?.focus();

return;
}

setTodos([
...todos,
{
id: +new Date(),
title: trimmedTitle,
completed: false,
},
]);

setTitle('');
inputRef.current?.focus();
};

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

setTodos(
todos.map(todo => ({
...todo,
completed: !allCompleted,
})),
);

inputRef.current?.focus();
};

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

<form onSubmit={handleSubmit}>
<input
ref={inputRef}
data-cy="NewTodoField"
type="text"
className="todoapp__new-todo"
placeholder="What needs to be done?"
autoFocus
value={title}
onChange={event => setTitle(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';
Loading
Loading