diff --git a/src/App.tsx b/src/App.tsx
index a399287bd..dfcf79d27 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -1,5 +1,8 @@
/* eslint-disable jsx-a11y/control-has-associated-label */
import React from 'react';
+import { Header } from './components/Header';
+import { TodoList } from './components/TodoList';
+import { Footer } from './components/Footer';
export const App: React.FC = () => {
return (
@@ -7,150 +10,11 @@ export const App: React.FC = () => {
todos
-
- {/* this button should have `active` class only if all todos are completed */}
-
+
- {/* Add a todo on form submit */}
-
-
+
-
-
- {/* Hide the footer if there are no todos */}
-
-
- 3 items left
-
-
- {/* Active link should have the 'selected' class */}
-
-
- All
-
-
-
- Active
-
-
-
- Completed
-
-
-
- {/* this button should be disabled if there are no completed todos */}
-
- Clear completed
-
-
+
);
diff --git a/src/Provider.tsx b/src/Provider.tsx
new file mode 100644
index 000000000..a7925fc9e
--- /dev/null
+++ b/src/Provider.tsx
@@ -0,0 +1,83 @@
+import React, { useEffect, useReducer } from 'react';
+import { Todo } from './types/Todo';
+import { Filter } from './types/Filter';
+
+export interface State {
+ todos: Todo[];
+ filter: Filter;
+}
+
+type Props = {
+ children: React.ReactNode;
+};
+
+type Action =
+ | { type: 'addTodo'; payload: Todo }
+ | { type: 'deleteTodo'; payload: number }
+ | { type: 'updateTodo'; payload: Todo }
+ | { type: 'filterTodo'; payload: Filter }
+ | { type: 'deleteCompletedTodos'; payload: number[] };
+
+const reducer = (state: State, action: Action): State => {
+ switch (action.type) {
+ case 'addTodo':
+ return {
+ ...state,
+ todos: [...state.todos, action.payload],
+ };
+
+ case 'deleteTodo':
+ return {
+ ...state,
+ todos: state.todos.filter(todo => todo.id !== action.payload),
+ };
+
+ case 'updateTodo':
+ return {
+ ...state,
+ todos: state.todos.map(todo =>
+ todo.id === action.payload.id ? action.payload : todo,
+ ),
+ };
+
+ case 'filterTodo':
+ return {
+ ...state,
+ filter: action.payload,
+ };
+
+ case 'deleteCompletedTodos':
+ return {
+ ...state,
+ todos: state.todos.filter(todo => !action.payload.includes(todo.id)),
+ };
+
+ default:
+ return state;
+ }
+};
+
+const loadedTodos = localStorage.getItem('todos');
+const initialState: State = {
+ todos: loadedTodos ? JSON.parse(loadedTodos) : [],
+ filter: Filter.All,
+};
+
+export const StateContext = React.createContext(initialState);
+export const DispatchContext = React.createContext>(
+ () => {},
+);
+
+export const Provider: React.FC = ({ children }) => {
+ const [state, dispatch] = useReducer(reducer, initialState);
+
+ useEffect(() => {
+ localStorage.setItem('todos', JSON.stringify(state.todos));
+ }, [state.todos]);
+
+ return (
+
+ {children}
+
+ );
+};
diff --git a/src/components/Footer.tsx b/src/components/Footer.tsx
new file mode 100644
index 000000000..e0804ed67
--- /dev/null
+++ b/src/components/Footer.tsx
@@ -0,0 +1,61 @@
+import { useContext } from 'react';
+import { DispatchContext, StateContext } from '../Provider';
+import { Filter } from '../types/Filter';
+import classNames from 'classnames';
+
+export const Footer: React.FC = () => {
+ const { todos, filter } = useContext(StateContext);
+ const dispatch = useContext(DispatchContext);
+ const activeTodosCount = todos.filter(todo => !todo.completed).length;
+ const hasCompletedTodos = todos.some(todo => todo.completed);
+
+ const deleteCompleted = () => {
+ const completedIds = todos
+ .filter(todo => todo.completed)
+ .map(todo => todo.id);
+
+ dispatch({ type: 'deleteCompletedTodos', payload: completedIds });
+ };
+
+ if (!todos.length) {
+ return null;
+ }
+
+ return (
+ <>
+
+ >
+ );
+};
diff --git a/src/components/Header.tsx b/src/components/Header.tsx
new file mode 100644
index 000000000..f40ada492
--- /dev/null
+++ b/src/components/Header.tsx
@@ -0,0 +1,89 @@
+import { useContext, useEffect, useMemo, useRef, useState } from 'react';
+import { DispatchContext, StateContext } from '../Provider';
+import { Todo } from '../types/Todo';
+import classNames from 'classnames';
+
+export const Header: React.FC = () => {
+ const [title, setTitle] = useState('');
+ const titleField = useRef(null);
+ const { todos } = useContext(StateContext);
+ const dispatch = useContext(DispatchContext);
+
+ useEffect(() => {
+ if (titleField.current) {
+ titleField.current.focus();
+ }
+ }, [todos.length]);
+
+ const handleSubmit = (e: React.FormEvent) => {
+ e.preventDefault();
+
+ if (!title.trim()) {
+ return;
+ }
+
+ const newTodo: Todo = {
+ id: Date.now(),
+ title: title.trim(),
+ completed: false,
+ };
+
+ dispatch({ type: 'addTodo', payload: newTodo });
+ setTitle('');
+ };
+
+ const handleTitleChange = (e: React.ChangeEvent) => {
+ setTitle(e.target.value);
+ };
+
+ const allTodosCompleted = useMemo(() => {
+ return todos.filter(todo => todo.completed).length === todos.length;
+ }, [todos]);
+
+ const handleToggleAllButtonClick = () => {
+ let todosToChange = [];
+
+ if (allTodosCompleted) {
+ todosToChange = [...todos];
+ } else {
+ todosToChange = todos.filter(todo => !todo.completed);
+ }
+
+ todosToChange.forEach(todo => {
+ const { id, title: todoTitle, completed } = todo;
+
+ dispatch({
+ type: 'updateTodo',
+ payload: { id, title: todoTitle, completed: !completed },
+ });
+ });
+ };
+
+ return (
+
+ {!!todos.length && (
+
+ )}
+
+
+
+ );
+};
diff --git a/src/components/TodoItem.tsx b/src/components/TodoItem.tsx
new file mode 100644
index 000000000..d16999525
--- /dev/null
+++ b/src/components/TodoItem.tsx
@@ -0,0 +1,109 @@
+import classNames from 'classnames';
+import { Todo } from '../types/Todo';
+import { useContext, useState } from 'react';
+import { DispatchContext } from '../Provider';
+
+type Props = {
+ todo: Todo;
+};
+
+/* eslint-disable jsx-a11y/label-has-associated-control */
+export const TodoItem: React.FC = ({ todo }) => {
+ const { id, title, completed } = todo;
+ const [isEditing, setIsEditing] = useState(false);
+ const [editedTitle, setEditedTitle] = useState(title);
+ const dispatch = useContext(DispatchContext);
+
+ const handleEditedTitleChange = (e: React.ChangeEvent) => {
+ setEditedTitle(e.target.value);
+ };
+
+ const saveChanges = () => {
+ const trimmedEditedTitle = editedTitle.trim();
+
+ if (!trimmedEditedTitle) {
+ dispatch({ type: 'deleteTodo', payload: id });
+ setIsEditing(false);
+
+ return;
+ }
+
+ dispatch({
+ type: 'updateTodo',
+ payload: { id, title: trimmedEditedTitle, completed },
+ });
+ setIsEditing(false);
+ };
+
+ const handleEditSubmit = (e: React.FormEvent) => {
+ e.preventDefault();
+ saveChanges();
+ };
+
+ const handleEscapeKeyUp = (e: React.KeyboardEvent) => {
+ if (e.key === 'Escape') {
+ setIsEditing(false);
+ }
+ };
+
+ return (
+ <>
+ {/* This is a completed todo */}
+
+
+
+ dispatch({
+ type: 'updateTodo',
+ payload: { id, title, completed: !completed },
+ })
+ }
+ />
+
+
+ {isEditing ? (
+
+ ) : (
+ <>
+ setIsEditing(true)}
+ >
+ {title}
+
+
+ dispatch({ type: 'deleteTodo', payload: id })}
+ >
+ ×
+
+ >
+ )}
+
+ >
+ );
+};
diff --git a/src/components/TodoList.tsx b/src/components/TodoList.tsx
new file mode 100644
index 000000000..3d1281ef0
--- /dev/null
+++ b/src/components/TodoList.tsx
@@ -0,0 +1,30 @@
+import { useContext } from 'react';
+import { TodoItem } from './TodoItem';
+import { StateContext } from '../Provider';
+import { Filter } from '../types/Filter';
+
+export const TodoList: React.FC = () => {
+ const { todos, filter } = useContext(StateContext);
+
+ const filteredTodos = todos.filter(todo => {
+ if (filter === Filter.Completed) {
+ return todo.completed;
+ } else if (filter === Filter.Active) {
+ return !todo.completed;
+ }
+
+ return true;
+ });
+
+ if (!filteredTodos.length) {
+ return null;
+ }
+
+ return (
+
+ {filteredTodos.map(todo => (
+
+ ))}
+
+ );
+};
diff --git a/src/index.tsx b/src/index.tsx
index b2c38a17a..ec5036d18 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -3,7 +3,12 @@ import { createRoot } from 'react-dom/client';
import './styles/index.scss';
import { App } from './App';
+import { Provider } from './Provider';
const container = document.getElementById('root') as HTMLDivElement;
-createRoot(container).render( );
+createRoot(container).render(
+
+
+ ,
+);
diff --git a/src/styles/index.scss b/src/styles/index.scss
index d8d324941..55cac966f 100644
--- a/src/styles/index.scss
+++ b/src/styles/index.scss
@@ -1,3 +1,7 @@
+@use './todoapp';
+@use './todo-list';
+@use './filters';
+
iframe {
display: none;
}
@@ -20,6 +24,3 @@ body {
pointer-events: none;
}
-@import './todoapp';
-@import './todo-list';
-@import './filters';
diff --git a/src/styles/todo-list.scss b/src/styles/todo-list.scss
index 4576af434..cfb34ec2f 100644
--- a/src/styles/todo-list.scss
+++ b/src/styles/todo-list.scss
@@ -71,6 +71,7 @@
}
&__title-field {
+ box-sizing: border-box;
width: 100%;
padding: 11px 14px;
diff --git a/src/styles/todoapp.scss b/src/styles/todoapp.scss
index e289a9458..29383a1e2 100644
--- a/src/styles/todoapp.scss
+++ b/src/styles/todoapp.scss
@@ -56,6 +56,7 @@
}
&__new-todo {
+ box-sizing: border-box;
width: 100%;
padding: 16px 16px 16px 60px;
diff --git a/src/types/Filter.ts b/src/types/Filter.ts
new file mode 100644
index 000000000..66887875b
--- /dev/null
+++ b/src/types/Filter.ts
@@ -0,0 +1,5 @@
+export enum Filter {
+ All = 'All',
+ Active = 'Active',
+ Completed = 'Completed',
+}
diff --git a/src/types/Todo.ts b/src/types/Todo.ts
new file mode 100644
index 000000000..f9e06b381
--- /dev/null
+++ b/src/types/Todo.ts
@@ -0,0 +1,5 @@
+export interface Todo {
+ id: number;
+ title: string;
+ completed: boolean;
+}