diff --git a/README.md b/README.md index 47a1add059..95b5d340d8 100644 --- a/README.md +++ b/README.md @@ -47,4 +47,4 @@ Implement the ability to edit a todo title on double click: - Implement a solution following the [React task guideline](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). -- Replace `` with your Github username in the [DEMO LINK](https://.github.io/react_todo-app-with-api/) and add it to the PR description. +- Replace `` with your Github username in the [DEMO LINK](https://VitaliyHoroshko.github.io/react_todo-app-with-api/) and add it to the PR description. diff --git a/src/App.tsx b/src/App.tsx index 81e011f432..a399fe5bf7 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,26 +1,272 @@ -/* eslint-disable max-len */ +/* eslint-disable jsx-a11y/label-has-associated-control */ /* eslint-disable jsx-a11y/control-has-associated-label */ -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { UserWarning } from './UserWarning'; +import { getTodos, USER_ID } from './api/todos'; +import { Todo } from './types/Todo'; +import { client } from './utils/fetchClient'; -const USER_ID = 0; +import Header from './components/Header'; +import TodoList from './components/TodoList'; +import Footer from './components/Footer'; +import ErrorNotification from './components/ErrorNotification'; +import { ErrorMessage } from './types/ErrorMessage'; +import { Filter } from './types/Filter'; export const App: React.FC = () => { + const [todos, setTodos] = useState([]); + const [error, setError] = useState(''); + const [filter, setFilter] = useState(Filter.ALL); + const [loading, setLoading] = useState(false); + const [tempTodo, setTempTodo] = useState(null); + const [updatingIds, setUpdatingIds] = useState([]); + const [newTitle, setNewTitle] = useState(''); + const [editingId, setEditingId] = useState(null); + + const newTodoRef = React.useRef(null); + + useEffect(() => { + if (!USER_ID) { + return; + } + + setError(''); + setLoading(true); + getTodos() + .then(todosFromServer => { + setTimeout(() => setTodos(todosFromServer), 150); + }) + .catch(() => setError(ErrorMessage.LOAD_TODOS)) + .finally(() => setLoading(false)); + }, []); + + useEffect(() => { + if (!loading) { + newTodoRef.current?.focus(); + } + }, [loading]); + + useEffect(() => { + if (error) { + const timer = setTimeout(() => setError(''), 3000); + + return () => clearTimeout(timer); + } + }, [error]); + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + const trimmed = newTitle.trim(); + + if (!trimmed) { + setError(ErrorMessage.EMPTY_TITLE); + + return; + } + + const optimisticTodo: Todo = { + id: 0, + title: trimmed, + completed: false, + userId: USER_ID, + }; + + setTempTodo(optimisticTodo); + setLoading(true); + + try { + const addedTodo = await client.post('/todos', { + title: trimmed, + userId: USER_ID, + completed: false, + }); + + setTodos(prev => [...prev, addedTodo]); + setTempTodo(null); + setNewTitle(''); + newTodoRef.current?.focus(); + } catch { + setError(ErrorMessage.ADD_TODO); + setTempTodo(null); + newTodoRef.current?.focus(); + } finally { + setLoading(false); + } + }; + + const deleteTodo = async (id: number) => { + setUpdatingIds(prev => [...prev, id]); + try { + await client.delete(`/todos/${id}`); + setTodos(prev => prev.filter(t => t.id !== id)); + + newTodoRef.current?.focus(); + } catch { + setError(ErrorMessage.DELETE_TODO); + } finally { + setUpdatingIds(prev => prev.filter(i => i !== id)); + } + }; + + const toggleTodo = async (id: number) => { + const activeTodo = todos.find(todo => todo.id === id); + + if (!activeTodo) { + return; + } + + const newStatus = !activeTodo.completed; + + setUpdatingIds(prev => [...prev, id]); + + try { + const updatedTodo = await client.patch(`/todos/${id}`, { + completed: newStatus, + }); + + setTodos(prevTodos => + prevTodos.map(todo => { + return todo.id === id ? updatedTodo : todo; + }), + ); + } catch { + setError(ErrorMessage.UPDATE_TODO); + } finally { + setUpdatingIds(prev => prev.filter(i => i !== id)); + } + }; + + const updateTodo = async (id: number, title: string) => { + setUpdatingIds(prev => [...prev, id]); + + try { + const updatedTodo = await client.patch(`/todos/${id}`, { title }); + + setTodos(prev => prev.map(todo => (todo.id === id ? updatedTodo : todo))); + setEditingId(null); + } catch { + setError(ErrorMessage.UPDATE_TODO); + } finally { + setUpdatingIds(prev => prev.filter(i => i !== id)); + } + }; + + const toggleAll = async () => { + const newCompleted = !todos.every(todo => todo.completed); + const filtered = todos.filter(todo => todo.completed !== newCompleted); + const idsToUpdate = filtered.map(todo => todo.id); + + setUpdatingIds(prev => [...prev, ...idsToUpdate]); + + try { + const promises = filtered.map(todo => { + return client.patch(`/todos/${todo.id}`, { + completed: newCompleted, + }); + }); + + const updatedTodos = await Promise.all(promises); + + setTodos(prevTodos => + prevTodos.map(todo => { + const updated = updatedTodos.find( + (u: { id: number }) => u.id === todo.id, + ); + + return updated || todo; + }), + ); + } catch { + setError(ErrorMessage.TOGGLE_ALL); + } finally { + setUpdatingIds(prev => prev.filter(id => !idsToUpdate.includes(id))); + } + }; + + const clearCompleted = async () => { + const completedTodos = todos.filter(t => t.completed); + + if (completedTodos.length === 0) { + return; + } + + const ids = completedTodos.map(t => t.id); + + setUpdatingIds(ids); + + const successIds: number[] = []; + let hasError = false; + + await Promise.allSettled( + ids.map(async id => { + try { + await client.delete(`/todos/${id}`); + successIds.push(id); + } catch { + hasError = true; + } + }), + ); + + setTodos(prev => prev.filter(t => !successIds.includes(t.id))); + setUpdatingIds([]); + + if (!hasError) { + newTodoRef.current?.focus(); + } else { + setError(ErrorMessage.DELETE_TODO); + } + }; + + const allCompleted = todos.length > 0 && todos.every(todo => todo.completed); + if (!USER_ID) { return ; } return ( -
-

- Copy all you need from the prev task: -
- - React Todo App - Add and Delete - -

- -

Styles are already copied

-
+
+

todos

+ +
+
+ + {(loading || todos.length > 0) && ( + <> + + +
!t.completed).length} + filter={filter} + setFilter={setFilter} + hasCompleted={todos.some(t => t.completed)} + onClearCompleted={clearCompleted} + /> + + )} +
+ + setError('')} /> +
); }; diff --git a/src/api/todos.ts b/src/api/todos.ts new file mode 100644 index 0000000000..7b5c4eda5f --- /dev/null +++ b/src/api/todos.ts @@ -0,0 +1,8 @@ +import { Todo } from '../types/Todo'; +import { client } from '../utils/fetchClient'; + +export const USER_ID = 3995; + +export const getTodos = async (): Promise => { + return client.get(`/todos?userId=${USER_ID}`); +}; diff --git a/src/components/ErrorNotification.tsx b/src/components/ErrorNotification.tsx new file mode 100644 index 0000000000..fefceb5c04 --- /dev/null +++ b/src/components/ErrorNotification.tsx @@ -0,0 +1,32 @@ +import React from 'react'; +import classNames from 'classnames'; + +type ErrorNotificationProps = { + error: string; + onClose: () => void; +}; + +export const ErrorNotification: React.FC = ({ + error, + onClose, +}) => { + return ( +
+
+ ); +}; + +export default ErrorNotification; diff --git a/src/components/Footer.tsx b/src/components/Footer.tsx new file mode 100644 index 0000000000..810374a49e --- /dev/null +++ b/src/components/Footer.tsx @@ -0,0 +1,61 @@ +import React from 'react'; +import classNames from 'classnames'; +import { Filter } from '../types/Filter'; + +type FooterProps = { + activeCount: number; + filter: Filter; + setFilter: (filter: Filter) => void; + hasCompleted: boolean; + onClearCompleted: () => void; +}; + +const filters = [ + { value: Filter.ALL, label: 'All', href: '#/' }, + { value: Filter.ACTIVE, label: 'Active', href: '#/active' }, + { value: Filter.COMPLETED, label: 'Completed', href: '#/completed' }, +]; + +export const Footer: React.FC = ({ + activeCount, + filter, + setFilter, + hasCompleted, + onClearCompleted, +}) => { + return ( + + ); +}; + +export default Footer; diff --git a/src/components/Header.tsx b/src/components/Header.tsx new file mode 100644 index 0000000000..ebbac9dcfb --- /dev/null +++ b/src/components/Header.tsx @@ -0,0 +1,55 @@ +import React from 'react'; +import classNames from 'classnames'; + +type HeaderProps = { + newTitle: string; + setNewTitle: (value: string) => void; + onSubmit: (e: React.FormEvent) => void; + isLoading: boolean; + inputRef: React.RefObject; + todosLength: number; + allCompleted: boolean; + onToggleAll: () => void; +}; + +export const Header: React.FC = ({ + newTitle, + setNewTitle, + onSubmit, + isLoading, + inputRef, + todosLength, + allCompleted, + onToggleAll, +}) => { + return ( +
+ {todosLength > 0 && ( +
+ ); +}; + +export default Header; diff --git a/src/components/TodoItem.tsx b/src/components/TodoItem.tsx new file mode 100644 index 0000000000..add227f264 --- /dev/null +++ b/src/components/TodoItem.tsx @@ -0,0 +1,125 @@ +/* eslint-disable jsx-a11y/label-has-associated-control */ +import React from 'react'; +import classNames from 'classnames'; +import { Todo } from '../types/Todo'; + +type TodoItemProps = { + todo: Todo; + isUpdating: boolean; + isEditing: boolean; + onEdit: (id: number | null) => void; + onUpdate: (title: string) => void; + onToggle: () => void; + onDelete: () => void; +}; + +export const TodoItem: React.FC = ({ + todo, + isUpdating, + isEditing, + onEdit, + onUpdate, + onToggle, + onDelete, +}) => { + const [newTitle, setNewTitle] = React.useState(todo.title); + const handleSubmit = (event?: React.FormEvent) => { + event?.preventDefault(); + + if (isUpdating) { + return; + } + + const trimmedTitle = newTitle.trim(); + + if (trimmedTitle === todo.title) { + onEdit(null); + + return; + } + + if (!trimmedTitle) { + onDelete(); + + return; + } + + onUpdate(trimmedTitle); + }; + + const handleKeyUp = (e: React.KeyboardEvent) => { + if (e.key === 'Enter') { + handleSubmit(); + } + + if (e.key === 'Escape') { + setNewTitle(todo.title); + onEdit(null); + } + }; + + return ( +
+ + + {!isEditing ? ( + onEdit(todo.id)} + > + {todo.title} + + ) : ( + setNewTitle(e.target.value)} + autoFocus + disabled={isUpdating} + onBlur={handleSubmit} + onKeyUp={handleKeyUp} + /> + )} + + {!isEditing && ( + + )} + +
+
+
+
+
+ ); +}; + +export default TodoItem; diff --git a/src/components/TodoList.tsx b/src/components/TodoList.tsx new file mode 100644 index 0000000000..e1a2732df0 --- /dev/null +++ b/src/components/TodoList.tsx @@ -0,0 +1,87 @@ +/* eslint-disable jsx-a11y/label-has-associated-control */ +import React from 'react'; +import { Todo } from '../types/Todo'; +import TodoItem from './TodoItem'; +import { Filter } from '../types/Filter'; + +type TodoListProps = { + todos: Todo[]; + filter: Filter; + tempTodo: Todo | null; + updatingIds: number[]; + onToggle: (id: number) => void; + onDelete: (id: number) => void; + isLoading: boolean; + editingId: number | null; + onEdit: (id: number | null) => void; + onUpdate: (id: number, title: string) => void; +}; + +export const TodoList: React.FC = ({ + todos, + filter, + tempTodo, + updatingIds, + onToggle, + onDelete, + editingId, + onEdit, + onUpdate, +}) => { + const visibleTodos = todos.filter(todo => { + switch (filter) { + case Filter.ACTIVE: + return !todo.completed; + case Filter.COMPLETED: + return todo.completed; + default: + return true; + } + }); + + return ( +
+ {visibleTodos.map(todo => ( + onToggle(todo.id)} + onDelete={() => onDelete(todo.id)} + isEditing={todo.id === editingId} + onEdit={onEdit} + onUpdate={(title: string) => onUpdate(todo.id, title)} + /> + ))} + + {tempTodo && ( +
+ + + + {tempTodo.title} + + + + +
+
+
+
+
+ )} +
+ ); +}; + +export default TodoList; diff --git a/src/types/ErrorMessage.ts b/src/types/ErrorMessage.ts new file mode 100644 index 0000000000..414defe058 --- /dev/null +++ b/src/types/ErrorMessage.ts @@ -0,0 +1,8 @@ +export enum ErrorMessage { + LOAD_TODOS = 'Unable to load todos', + ADD_TODO = 'Unable to add a todo', + DELETE_TODO = 'Unable to delete a todo', + UPDATE_TODO = 'Unable to update a todo', + TOGGLE_ALL = 'Unable to toggle all todos', + EMPTY_TITLE = 'Title should not be empty', +} diff --git a/src/types/Filter.ts b/src/types/Filter.ts new file mode 100644 index 0000000000..6dad7340f0 --- /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 0000000000..780afae02d --- /dev/null +++ b/src/types/Todo.ts @@ -0,0 +1,6 @@ +export interface Todo { + id: number; + title: string; + completed: boolean; + userId: number; +} diff --git a/src/utils/fetchClient.ts b/src/utils/fetchClient.ts new file mode 100644 index 0000000000..708ac4c17b --- /dev/null +++ b/src/utils/fetchClient.ts @@ -0,0 +1,46 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +const BASE_URL = 'https://mate.academy/students-api'; + +// returns a promise resolved after a given delay +function wait(delay: number) { + return new Promise(resolve => { + setTimeout(resolve, delay); + }); +} + +// To have autocompletion and avoid mistypes +type RequestMethod = 'GET' | 'POST' | 'PATCH' | 'DELETE'; + +function request( + url: string, + method: RequestMethod = 'GET', + data: any = null, // we can send any data to the server +): Promise { + const options: RequestInit = { method }; + + if (data) { + // We add body and Content-Type only for the requests with data + options.body = JSON.stringify(data); + options.headers = { + 'Content-Type': 'application/json; charset=UTF-8', + }; + } + + // DON'T change the delay it is required for tests + return wait(100) + .then(() => fetch(BASE_URL + url, options)) + .then(response => { + if (!response.ok) { + throw new Error(); + } + + return response.json(); + }); +} + +export const client = { + get: (url: string) => request(url), + post: (url: string, data: any) => request(url, 'POST', data), + patch: (url: string, data: any) => request(url, 'PATCH', data), + delete: (url: string) => request(url, 'DELETE'), +};