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://jk-npc.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.

206 changes: 75 additions & 131 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,156 +1,100 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import React, { useState } from 'react';
import { useTodos } from './context/TodoContext';
import { TodoItem } from './components/TodoItem';
import { TodoFilter } from './components/TodoFilter';

type FilterType = 'all' | 'active' | 'completed';

export const App: React.FC = () => {
const { todos, inputRef, addTodo, toggleAll, clearCompleted } = useTodos();
const [filter, setFilter] = useState<FilterType>('all');
const [newTitle, setNewTitle] = useState('');

const filteredTodos = todos.filter(todo => {
if (filter === 'active') {
return !todo.completed;
}

if (filter === 'completed') {
return todo.completed;
}

return true;
});

const activeTodosCount = todos.filter(todo => !todo.completed).length;
const hasCompleted = todos.some(todo => todo.completed);
const allCompleted = todos.length > 0 && todos.every(todo => todo.completed);

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

if (!newTitle.trim()) {
return;
}

addTodo(newTitle.trim());
setNewTitle('');
inputRef.current?.focus();
};

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>
{todos.length > 0 && (
<button
type="button"
className={`todoapp__toggle-all ${allCompleted ? 'active' : ''}`}
data-cy="ToggleAllButton"
onClick={toggleAll}
/>
)}

<form onSubmit={handleSubmit}>
<input
ref={inputRef}
data-cy="NewTodoField"
type="text"
className="todoapp__new-todo"
placeholder="What needs to be done?"
value={newTitle}
onChange={e => setNewTitle(e.target.value)}
autoFocus
/>
</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
{todos.length > 0 && (
<section className="todoapp__main" data-cy="TodoList">
{filteredTodos.map(todo => (
<TodoItem key={todo.id} todo={todo} />
))}
</section>
)}

{todos.length > 0 && (
<footer className="todoapp__footer" data-cy="Footer">
<span className="todo-count" data-cy="TodosCounter">
{activeTodosCount} items left
</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>
<TodoFilter filter={filter} onChange={setFilter} />

<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"
<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
onClick={clearCompleted}
disabled={!hasCompleted}
>
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>
Clear completed
</button>
</footer>
)}
</div>
</div>
);
Expand Down
38 changes: 38 additions & 0 deletions src/components/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
type FilterType = 'all' | 'active' | 'completed';

type Props = {
filter: FilterType;
onChange: (filter: FilterType) => void;
};

const FILTERS = [
{ type: 'all', label: 'All', href: '#/', dataCy: 'FilterLinkAll' },
{
type: 'active',
label: 'Active',
href: '#/active',
dataCy: 'FilterLinkActive',
},
{
type: 'completed',
label: 'Completed',
href: '#/completed',
dataCy: 'FilterLinkCompleted',
},
] as const;

export const TodoFilter = ({ filter, onChange }: Props) => (
<nav className="filter" data-cy="Filter">
{FILTERS.map(({ type, label, href, dataCy }) => (
<a
key={type}
href={href}
className={`filter__link ${filter === type ? 'selected' : ''}`}
data-cy={dataCy}
onClick={() => onChange(type)}
>
{label}
</a>
))}
</nav>
);
87 changes: 87 additions & 0 deletions src/components/TodoItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React, { useState, useRef, useEffect } from 'react';
import { Todo } from '../types/Todo';
import { useTodos } from '../context/TodoContext';

export const TodoItem = ({ todo }: { todo: Todo }) => {
const { deleteTodo, toggleTodo, updateTodo } = useTodos();
const [editing, setEditing] = useState(false);
const [editTitle, setEditTitle] = useState(todo.title);
const inputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
if (editing) {
inputRef.current?.focus();
}
}, [editing]);

const handleSubmit = () => {
updateTodo(todo.id, editTitle);
setEditing(false);
};

const handleKeyUp = (e: React.KeyboardEvent) => {
if (e.key === 'Escape') {
setEditTitle(todo.title);
setEditing(false);
}
};

const handleFormSubmit = (e: React.FormEvent) => {
e.preventDefault();
handleSubmit();
};

return (
<div
data-cy="Todo"
className={` todo ${todo.completed ? 'completed' : ''}`}
>
{/* eslint-disable-next-line jsx-a11y/label-has-associated-control */}
<label className="todo__status-label" htmlFor={`todo-${todo.id}`}>
<input
id={`todo-${todo.id}`}
data-cy="TodoStatus"
type="checkbox"
className="todo__status"
checked={todo.completed}
onChange={() => toggleTodo(todo.id)}
/>
</label>

{editing ? (
<form onSubmit={handleFormSubmit}>
<input
ref={inputRef}
data-cy="TodoTitleField"
type="text"
className="todo__title-field"
placeholder="Empty todo will be deleted"
value={editTitle}
onChange={e => setEditTitle(e.target.value)}
onBlur={handleSubmit}
onKeyUp={handleKeyUp}
/>
</form>
) : (
<>
<span
data-cy="TodoTitle"
className="todo__title"
onDoubleClick={() => setEditing(true)}
>
{todo.title}
</span>

<button
type="button"
className="todo__remove"
data-cy="TodoDelete"
onClick={() => deleteTodo(todo.id)}
>
×
</button>
</>
)}
</div>
);
};
Loading
Loading