-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
72 lines (57 loc) · 2.02 KB
/
App.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { useState, useEffect } from 'react'
import {TodoProvider} from './context'
import './App.css'
import TodoForm from './components/TodoForm'
import TodoItem from './components/TodoItem'
function App() {
const [todos, setTodos] = useState([])
const addTodo = (todo) => {
setTodos((prev) => [{id: Date.now(), ...todo}, ...prev] )
}
const updateTodo = (id, todo) => {
setTodos((prev) => prev.map((prevTodo) => (prevTodo.id === id ? todo : prevTodo )))
}
const deleteTodo = (id) => {
setTodos((prev) => prev.filter((todo) => todo.id !== id))
}
const toggleComplete = (id) => {
//console.log(id);
setTodos((prev) =>
prev.map((prevTodo) =>
prevTodo.id === id ? { ...prevTodo,
completed: !prevTodo.completed } : prevTodo))
}
useEffect(() => {
const todos = JSON.parse(localStorage.getItem("todos"))
if (todos && todos.length > 0) {
setTodos(todos)
}
}, [])
useEffect(() => {
localStorage.setItem("todos", JSON.stringify(todos))
}, [todos])
return (
<TodoProvider value={{todos, addTodo, updateTodo, deleteTodo, toggleComplete}}>
<div className="bg-[#172842] min-h-screen py-8">
<div className="w-full max-w-2xl mx-auto shadow-md rounded-lg px-4 py-3 text-white">
<h1 className="text-2xl font-bold text-center mb-8 mt-2">Manage Your Todos</h1>
<div className="mb-4">
{/* Todo form goes here */}
<TodoForm />
</div>
<div className="flex flex-wrap gap-y-3">
{/*Loop and Add TodoItem here */}
{todos.map((todo) => (
<div key={todo.id}
className='w-full'
>
<TodoItem todo={todo} />
</div>
))}
</div>
</div>
</div>
</TodoProvider>
)
}
export default App