diff --git a/src/App.css b/src/App.css deleted file mode 100644 index 74b5e05..0000000 --- a/src/App.css +++ /dev/null @@ -1,38 +0,0 @@ -.App { - text-align: center; -} - -.App-logo { - height: 40vmin; - pointer-events: none; -} - -@media (prefers-reduced-motion: no-preference) { - .App-logo { - animation: App-logo-spin infinite 20s linear; - } -} - -.App-header { - background-color: #282c34; - min-height: 100vh; - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - font-size: calc(10px + 2vmin); - color: white; -} - -.App-link { - color: #61dafb; -} - -@keyframes App-logo-spin { - from { - transform: rotate(0deg); - } - to { - transform: rotate(360deg); - } -} diff --git a/src/App.tsx b/src/App.tsx index bf4135f..ebf4d4c 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,11 +1,14 @@ import React, {useState} from 'react'; -import './App.css'; +import { useSelector, useDispatch } from "react-redux"; + import Todo from "./Todo"; import {TodoItem} from "./types"; +import { ADD_TODO, TOGGLE_TODO, DELETE_TODO } from "./store"; function App() { - const [todos, setTodos] = useState([]); const [text, setText] = useState(""); + const todos = useSelector((state: { todos: TodoItem[] }) => state.todos); + const dispatch = useDispatch(); const handleAddTodo = () => { if (text.trim() === "") return; @@ -15,24 +18,16 @@ function App() { completed: false, }; - setTodos([...todos, newTodo]); + dispatch({ type: ADD_TODO, payload: newTodo}); setText(""); } const handleToggleTodo = (id: number) => { - const updatedTodos = todos.map((todo) => { - if (todo.id === id) { - return {...todo, completed: !todo.completed}; - } - return todo; - }); - - setTodos(updatedTodos); + dispatch({ type: TOGGLE_TODO, payload: id}); } const handleDeleteTodo = (id: number) => { - const updatedTodos = todos.filter((todo) => todo.id !== id); - setTodos(updatedTodos); + dispatch({ type: DELETE_TODO, payload: id}); } return ( diff --git a/src/index.tsx b/src/index.tsx index 032464f..a0ca7df 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,19 +1,12 @@ import React from 'react'; -import ReactDOM from 'react-dom/client'; -import './index.css'; +import ReactDOM from "react-dom"; +import { Provider } from "react-redux"; +import { store } from "./store"; import App from './App'; -import reportWebVitals from './reportWebVitals'; -const root = ReactDOM.createRoot( - document.getElementById('root') as HTMLElement -); -root.render( - - - -); - -// If you want to start measuring performance in your app, pass a function -// to log results (for example: reportWebVitals(console.log)) -// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals -reportWebVitals(); +ReactDOM.render( + + + , + document.getElementById("root") +) diff --git a/src/store.ts b/src/store.ts new file mode 100644 index 0000000..593cae2 --- /dev/null +++ b/src/store.ts @@ -0,0 +1,65 @@ +import { createStore } from "redux"; + +import { TodoItem } from "./types"; +import {stat} from "fs"; + +interface TodoState { + todos: TodoItem[]; +} + +const initialState: TodoState = { + todos: [], +} + +export const ADD_TODO = "ADD_TODO"; +export const TOGGLE_TODO = "TOGGLE_TODO"; +export const DELETE_TODO = "DELETE_TODO"; + +interface AddTodoAction { + type: typeof ADD_TODO; + payload: TodoItem; +} + +interface ToggleTodoAction { + type: typeof TOGGLE_TODO; + payload: number; +} + +interface DeleteTodoAction { + type: typeof DELETE_TODO; + payload: number; +} + +type TodoActionTypes = AddTodoAction | ToggleTodoAction | DeleteTodoAction; + +export function todoReducer( + state = initialState, + action: TodoActionTypes +): TodoState { + switch (action.type) { + case ADD_TODO: + return { + ...state, + todos: [...state.todos, action.payload], + }; + case TOGGLE_TODO: + return { + ...state, + todos: state.todos.map((todo) => { + if (todo.id === action.payload) { + return { ...todo, completed: !todo.completed }; + } + return todo; + }), + }; + case DELETE_TODO: + return { + ...state, + todos: state.todos.filter((todo) => todo.id !== action.payload), + }; + default: + return state; + } +} + +export const store = createStore(todoReducer);