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
157 changes: 10 additions & 147 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,156 +1,19 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
import { useTodoState } from './hooks/useTodoState';
import { Header } from './components/Header';
import { TodoList } from './components/TodoList';
import { Footer } from './components/Footer';

export const App = () => {
const { hasTodos } = useTodoState();

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>
<Header />
<TodoList />
{hasTodos && <Footer />}
</div>
</div>
);
Expand Down
36 changes: 36 additions & 0 deletions src/components/CreateTodoForm/CreateTodoForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useState, FormEvent } from 'react';
import { useTodoActions } from '../../hooks/useTodoActions';
import { useInputRef } from '../../hooks/useInputRef';

export const CreateTodoForm = () => {
const [title, setTitle] = useState('');
const { addTodo } = useTodoActions();
const inputRef = useInputRef();

const handleSubmit = (event: FormEvent) => {
event.preventDefault();
const normalizedTitle = title.trim();

if (!normalizedTitle) {
return;
}

addTodo(normalizedTitle);
setTitle('');
};

return (
<form onSubmit={handleSubmit}>
<input
ref={inputRef}
autoFocus
data-cy="NewTodoField"
type="text"
className="todoapp__new-todo"
placeholder="What needs to be done?"
value={title}
onChange={event => setTitle(event.target.value)}
/>
</form>
);
};
1 change: 1 addition & 0 deletions src/components/CreateTodoForm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './CreateTodoForm';
45 changes: 45 additions & 0 deletions src/components/EditTodoForm/EditTodoForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { FormEvent, KeyboardEvent, useState } from 'react';

interface Props {
title: string;
onChange: (title: string) => void;
onClose: () => void;
}

export const EditTodoForm = ({ title, onChange, onClose }: Props) => {
const [newTitle, setNewTitle] = useState(title);

const handleEscKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
if (event.key === 'Escape') {
onClose();
}
};

const handleChangeTitle = () => {
const normalizedTitle = newTitle.trim();

onChange(normalizedTitle);
onClose();
};

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

return (
<form onSubmit={handleSubmit}>
<input
autoFocus
data-cy="TodoTitleField"
type="text"
className="todo__title-field"
placeholder="Empty todo will be deleted"
value={newTitle}
onChange={event => setNewTitle(event.target.value)}
onBlur={handleChangeTitle}
onKeyDown={handleEscKeyDown}
/>
</form>
);
};
1 change: 1 addition & 0 deletions src/components/EditTodoForm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './EditTodoForm';
71 changes: 71 additions & 0 deletions src/components/Footer/Footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import classNames from 'classnames';
import { FilterOption } from '../../types/FilterOption';
import { useTodoActions } from '../../hooks/useTodoActions';
import { useTodoState } from '../../hooks/useTodoState';
import { useFocusInput } from '../../hooks/useFocusInput';

export const Footer = () => {
const { filter, activeTodosCount, completedTodosCount } = useTodoState();
const { setFilter, removeCompleted } = useTodoActions();
const focusInput = useFocusInput();

const hasCompletedTodos = completedTodosCount > 0;

const handleRemoveCompleted = () => {
removeCompleted();
focusInput();
};

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

<nav className="filter" data-cy="Filter">
<a
href="#/"
className={classNames('filter__link', {
selected: filter === FilterOption.ALL,
})}
data-cy="FilterLinkAll"
onClick={() => setFilter(FilterOption.ALL)}
>
All
</a>

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

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

<button
type="button"
className="todoapp__clear-completed"
data-cy="ClearCompletedButton"
disabled={!hasCompletedTodos}
onClick={handleRemoveCompleted}
>
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';
34 changes: 34 additions & 0 deletions src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import classNames from 'classnames';
import { useTodoState } from '../../hooks/useTodoState';
import { useTodoActions } from '../../hooks/useTodoActions';
import { useFocusInput } from '../../hooks/useFocusInput';
import { CreateTodoForm } from '../CreateTodoForm';

export const Header = () => {
const { hasTodos, allTodosCompleted } = useTodoState();
const { toggleAll } = useTodoActions();

const focusInput = useFocusInput();

const handleToggleAll = () => {
toggleAll();
focusInput();
};

return (
<header className="todoapp__header">
{hasTodos && (
<button
type="button"
className={classNames('todoapp__toggle-all', {
active: allTodosCompleted,
})}
data-cy="ToggleAllButton"
onClick={handleToggleAll}
/>
)}

<CreateTodoForm />
</header>
);
};
1 change: 1 addition & 0 deletions src/components/Header/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Header';
14 changes: 14 additions & 0 deletions src/components/TodoList/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useTodoState } from '../../hooks/useTodoState';
import { TodoListItem } from '../TodoListItem';

export const TodoList = () => {
const { filteredTodos } = useTodoState();

return (
<section className="todoapp__main" data-cy="TodoList">
{filteredTodos.map(todo => (
<TodoListItem key={todo.id} todo={todo} />
))}
</section>
);
};
1 change: 1 addition & 0 deletions src/components/TodoList/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TodoList';
Loading
Loading