From d069e73f93804c1ba4329506c2809c03eedc6cbd Mon Sep 17 00:00:00 2001 From: Virineya Marchuk Date: Sun, 23 Feb 2025 11:45:49 +0200 Subject: [PATCH] solution --- README.md | 2 +- src/App.tsx | 155 +++--------------------------------- src/components/Footer.tsx | 51 ++++++++++++ src/components/Header.tsx | 72 +++++++++++++++++ src/components/Store.tsx | 122 ++++++++++++++++++++++++++++ src/components/TodoItem.tsx | 141 ++++++++++++++++++++++++++++++++ src/components/TodoList.tsx | 28 +++++++ src/index.tsx | 7 +- src/services.ts | 9 +++ src/styles/index.scss | 4 + src/types/Filter.ts | 5 ++ src/types/Todo.ts | 5 ++ 12 files changed, 454 insertions(+), 147 deletions(-) create mode 100644 src/components/Footer.tsx create mode 100644 src/components/Header.tsx create mode 100644 src/components/Store.tsx create mode 100644 src/components/TodoItem.tsx create mode 100644 src/components/TodoList.tsx create mode 100644 src/services.ts create mode 100644 src/types/Filter.ts create mode 100644 src/types/Todo.ts diff --git a/README.md b/README.md index 903c876f9..80feaf053 100644 --- a/README.md +++ b/README.md @@ -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 `` with your GitHub username in the [DEMO LINK](https://.github.io/react_todo-app/) and add it to the PR description. +- Replace `` with your GitHub username in the [DEMO LINK](https://vtroni.github.io/react_todo-app/) and add it to the PR description. diff --git a/src/App.tsx b/src/App.tsx index a399287bd..e0ee9e8f5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,156 +1,21 @@ /* eslint-disable jsx-a11y/control-has-associated-label */ -import React from 'react'; +import React, { useContext } from 'react'; +import { StateContext } from './components/Store'; +import { Header } from './components/Header'; +import { TodoList } from './components/TodoList'; +import { Footer } from './components/Footer'; export const App: React.FC = () => { + const { todos } = useContext(StateContext); + return (

todos

-
- {/* this button should have `active` class only if all todos are completed */} -
- -
- {/* This is a completed todo */} -
- - - - Completed Todo - - - {/* Remove button appears only on hover */} - -
- - {/* This todo is an active todo */} -
- - - - Not Completed Todo - - - -
- - {/* This todo is being edited */} -
- - - {/* This form is shown instead of the title and remove button */} -
- -
-
- - {/* This todo is in loadind state */} -
- - - - Todo is being saved now - - - -
-
- - {/* Hide the footer if there are no todos */} -
- - 3 items left - - - {/* Active link should have the 'selected' class */} - - - {/* this button should be disabled if there are no completed todos */} - -
+
+ + {!!todos.length &&
}
); diff --git a/src/components/Footer.tsx b/src/components/Footer.tsx new file mode 100644 index 000000000..21aee6cda --- /dev/null +++ b/src/components/Footer.tsx @@ -0,0 +1,51 @@ +import { useContext } from 'react'; +import cn from 'classnames'; +import { DispatchContext, StateContext } from './Store'; +import { Filter } from '../types/Filter'; +import { activeTodos, completedTodos } from '../services'; + +export const Footer = () => { + const { todos, filter } = useContext(StateContext); + const dispatch = useContext(DispatchContext); + + return ( + + ); +}; diff --git a/src/components/Header.tsx b/src/components/Header.tsx new file mode 100644 index 000000000..de7331cc3 --- /dev/null +++ b/src/components/Header.tsx @@ -0,0 +1,72 @@ +import React, { FormEvent, useContext, useEffect, useRef } from 'react'; +import { DispatchContext, StateContext } from './Store'; +import { Todo } from '../types/Todo'; +import classNames from 'classnames'; +import { completedTodos } from '../services'; + +export const Header = () => { + const { todos, newTitle } = useContext(StateContext); + const dispatch = useContext(DispatchContext); + + const inputRef = useRef(null); + + useEffect(() => { + if (inputRef.current) { + inputRef.current.focus(); + } + }); + + const setNewTitle = (e: React.ChangeEvent) => { + dispatch({ type: 'setNewTitle', payload: e.target.value }); + }; + + const addTodo = (e: FormEvent) => { + e.preventDefault(); + + if (!newTitle.trim()) { + return; + } + + const newTodo: Todo = { + id: +new Date(), + title: newTitle.trim(), + completed: false, + }; + + dispatch({ type: 'addTodo', payload: newTodo }); + dispatch({ type: 'setNewTitle', payload: '' }); + }; + + const validation = todos.every(todo => todo.completed === true); + + const toggleAll = () => { + dispatch({ type: 'toggleAll', payload: !validation }); + }; + + return ( +
+ {todos.length > 0 && ( +
+ ); +}; diff --git a/src/components/Store.tsx b/src/components/Store.tsx new file mode 100644 index 000000000..9c13a6ec4 --- /dev/null +++ b/src/components/Store.tsx @@ -0,0 +1,122 @@ +import React, { useEffect, useReducer } from 'react'; +import { Filter } from '../types/Filter'; +import { Todo } from '../types/Todo'; + +type State = { + todos: Todo[]; + newTitle: string; + status: Filter; + filter: Filter; +}; + +const getTododsFromLocalStorage = (): Todo[] => { + const todos = localStorage.getItem('todos'); + + return todos ? JSON.parse(todos) : []; +}; + +const initialTodos: Todo[] = getTododsFromLocalStorage(); + +const initialState: State = { + todos: initialTodos, + newTitle: '', + status: Filter.ALL, + filter: Filter.ALL, +}; + +type Action = + | { type: 'addTodo'; payload: Todo } + | { type: 'deleteTodo'; payload: number } + | { type: 'updateTodo'; payload: Todo } + | { type: 'setNewTitle'; payload: string } + | { type: 'toggleAll'; payload: boolean } + | { type: 'setStatus'; payload: Filter } + | { type: 'setNewStatus'; payload: Todo } + | { type: 'setFilterByStatus'; payload: Filter } + | { type: 'clearAllCompleted' }; + +function 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 + ? { ...todo, title: action.payload.title } + : todo, + ), + }; + case 'setNewTitle': + return { + ...state, + newTitle: action.payload, + }; + case 'toggleAll': + return { + ...state, + todos: state.todos.map(todo => ({ + ...todo, + completed: action.payload, + })), + }; + case 'setStatus': + return { + ...state, + status: action.payload, + }; + case 'setNewStatus': + return { + ...state, + todos: state.todos.map(todo => + todo.id === action.payload.id + ? { ...todo, completed: !todo.completed } + : todo, + ), + }; + case 'setFilterByStatus': + return { + ...state, + filter: action.payload, + }; + case 'clearAllCompleted': + return { + ...state, + todos: state.todos.filter(todo => !todo.completed), + }; + default: + return state; + } +} + +export const StateContext = React.createContext(initialState); +export const DispatchContext = React.createContext>( + () => {}, +); + +type Props = { + children: React.ReactNode; +}; + +export const GlobalStateProvider: 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/TodoItem.tsx b/src/components/TodoItem.tsx new file mode 100644 index 000000000..fc1706aba --- /dev/null +++ b/src/components/TodoItem.tsx @@ -0,0 +1,141 @@ +import React, { useContext, useEffect, useRef, useState } from 'react'; +import { Todo } from '../types/Todo'; +import { DispatchContext } from './Store'; +import classNames from 'classnames'; + +type Props = { + todo: Todo; +}; + +export const TodoItem: React.FC = ({ todo }) => { + const dispatch = useContext(DispatchContext); + + const handleChangeStatusTodo = ( + event: React.ChangeEvent, + ) => { + dispatch({ + type: 'setNewStatus', + payload: { ...todo, completed: event.target.checked }, + }); + }; + + const [isEditingTodo, setIsEditingTodo] = useState(false); + const [updatedTitleTodo, setUpdatedTitleTodo] = useState(todo.title); + + const updatedInput = useRef(null); + + useEffect(() => { + if (isEditingTodo && updatedInput.current) { + updatedInput.current?.focus(); + } + }, [isEditingTodo]); + + useEffect(() => setIsEditingTodo(false), [todo]); + + const { id, title, completed } = todo; + + const handleDeleteTodo = (todoId: number) => { + dispatch({ type: 'deleteTodo', payload: todoId }); + }; + + const handleSubmit = () => { + const newTitle = updatedTitleTodo.trim(); + + if (newTitle === title) { + setIsEditingTodo(false); + + return; + } + + if (!newTitle) { + dispatch({ type: 'deleteTodo', payload: todo.id }); + + return; + } + + setUpdatedTitleTodo(newTitle); + + dispatch({ type: 'updateTodo', payload: { ...todo, title: newTitle } }); + }; + + const handleBlur = () => { + handleSubmit(); + }; + + const handleKeyEvent = (event: React.KeyboardEvent) => { + if (event.key === 'Escape') { + setUpdatedTitleTodo(todo.title); + setIsEditingTodo(false); + } + }; + + const handleChange = (event: React.ChangeEvent) => { + setUpdatedTitleTodo(event.target.value); + }; + + const handleDoubleClick = () => { + if (!isEditingTodo) { + setIsEditingTodo(true); + } + }; + + return ( +
+ + + {isEditingTodo ? ( +
{ + event.preventDefault(); + handleSubmit(); + setIsEditingTodo(false); + }} + > + +
+ ) : ( + <> + + {updatedTitleTodo} + + + + + )} +
+ ); +}; diff --git a/src/components/TodoList.tsx b/src/components/TodoList.tsx new file mode 100644 index 000000000..2bd5b6053 --- /dev/null +++ b/src/components/TodoList.tsx @@ -0,0 +1,28 @@ +import { useContext, useMemo } from 'react'; +import { StateContext } from './Store'; +import { Filter } from '../types/Filter'; +import { TodoItem } from './TodoItem'; + +export const TodoList = () => { + const { todos, filter } = useContext(StateContext); + + const preparedTodos = useMemo(() => { + if (filter === Filter.Active) { + return todos.filter(todo => !todo.completed); + } + + if (filter === Filter.Completed) { + return todos.filter(todo => todo.completed); + } + + return todos; + }, [todos, filter]); + + return ( +
+ {preparedTodos.map(todo => ( + + ))} +
+ ); +}; diff --git a/src/index.tsx b/src/index.tsx index b2c38a17a..d06ee7fb2 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 { GlobalStateProvider } from './components/Store'; const container = document.getElementById('root') as HTMLDivElement; -createRoot(container).render(); +createRoot(container).render( + + + , +); diff --git a/src/services.ts b/src/services.ts new file mode 100644 index 000000000..4954017b8 --- /dev/null +++ b/src/services.ts @@ -0,0 +1,9 @@ +import { Todo } from './types/Todo'; + +export const activeTodos = (todos: Todo[]) => { + return todos.filter((todo: Todo) => !todo.completed); +}; + +export const completedTodos = (todos: Todo[]) => { + return todos.filter((todo: Todo) => todo.completed); +}; diff --git a/src/styles/index.scss b/src/styles/index.scss index d8d324941..48121cbde 100644 --- a/src/styles/index.scss +++ b/src/styles/index.scss @@ -1,3 +1,7 @@ +* { + box-sizing: border-box; +} + iframe { display: none; } diff --git a/src/types/Filter.ts b/src/types/Filter.ts new file mode 100644 index 000000000..83f2e6a73 --- /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..d94ea1bff --- /dev/null +++ b/src/types/Todo.ts @@ -0,0 +1,5 @@ +export type Todo = { + id: number; + title: string; + completed: boolean; +};