Skip to content

Commit

Permalink
add task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
modeltoIT committed Oct 7, 2024
1 parent f1536f7 commit c097f66
Show file tree
Hide file tree
Showing 13 changed files with 386 additions and 151 deletions.
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://modeltoIT.github.io/react_todo-app/) and add it to the PR description.
9 changes: 5 additions & 4 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.8.5",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
151 changes: 10 additions & 141 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,156 +1,25 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import React, { useContext } from 'react';
import { TodoList } from './components/TodoList';
import { Footer } from './components/Footer';
import { TodoContext } from './components/TodoContext';
import { Header } from './components/Header';

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

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

export const Footer = () => {
const { todos, filterBy, setFilterBy, setTodos } = useContext(TodoContext);

const activeTodosLength = todos.filter(t => !t.completed).length;
const isDisabledBtn = !(todos.length - activeTodosLength);
const title =
activeTodosLength === 1 ? '1 item left' : `${activeTodosLength} items left`;

const filterTodos = (status: TodoFilter) => {
switch (status) {
case 'Active':
setFilterBy('Active');
break;
case 'Completed':
setFilterBy('Completed');
break;
default:
setFilterBy('All');
}
};

const deleteCompleted = () => {
setTodos(todos.filter(t => !t.completed));
};

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

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

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

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

{/* this button should be disabled if there are no completed todos */}

<button
type="button"
className="todoapp__clear-completed"
style={{ visibility: `${isDisabledBtn ? 'hidden' : 'visible'}` }}
data-cy="ClearCompletedButton"
onClick={deleteCompleted}
disabled={isDisabledBtn}
>
Clear completed
</button>
</footer>
);
};
61 changes: 61 additions & 0 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React, { useContext, useEffect, useRef, useState } from 'react';
import { TodoContext } from './TodoContext';
import classNames from 'classnames';

export const Header = () => {
const { todos, setTodos } = useContext(TodoContext);
const [value, setValue] = useState('');
const mainInput = useRef<HTMLInputElement>(null);
const isSomeActive = todos.some(t => !t.completed) || !todos.length;

useEffect(() => {
if (mainInput.current) {
mainInput.current.focus();
}
}, [todos]);

const submitHandler: React.FormEventHandler<HTMLFormElement> = e => {
e.preventDefault();

setTodos([
...todos,
{ id: Date.now(), title: value.trim(), completed: false },
]);
setValue('');
};

const toggleAll = () => {
if (isSomeActive) {
setTodos(todos.map(t => ({ ...t, completed: true })));
} else {
setTodos(todos.map(t => ({ ...t, completed: false })));
}
};

return (
<header className="todoapp__header">
{!!todos.length && (
<button
type="button"
className={classNames('todoapp__toggle-all', {
active: !isSomeActive,
})}
data-cy="ToggleAllButton"
onClick={toggleAll}
/>
)}

<form onSubmit={submitHandler}>
<input
data-cy="NewTodoField"
type="text"
className="todoapp__new-todo"
placeholder="What needs to be done?"
value={value}
ref={mainInput}
onChange={e => setValue(e.target.value)}
/>
</form>
</header>
);
};
Loading

0 comments on commit c097f66

Please sign in to comment.