-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
116 lines (102 loc) · 4.29 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { useState, useEffect } from 'react';
import Navbar from './components/Navbar';
import { FaEdit } from "react-icons/fa";
import { AiFillDelete } from "react-icons/ai";
import { v4 as uuidv4 } from 'uuid';
import Footer from './components/Footer';
import { ThemeProvider, useTheme } from './ThemeContext';
function App() {
const [todo, setTodo] = useState("");
const [todos, setTodos] = useState([]);
const [showFinished, setShowFinished] = useState(true);
// const [darkMode, setDarkMode] = useState(false);
const { darkMode } = useTheme()
useEffect(() => {
let todoString = localStorage.getItem("todos");
if (todoString) {
let todos = JSON.parse(localStorage.getItem("todos"));
setTodos(todos);
}
}, []);
const saveToLS = () => {
localStorage.setItem("todos", JSON.stringify(todos));
};
const toggleFinished = () => {
setShowFinished(!showFinished);
};
const toggleDarkMode = () => {
setDarkMode(!darkMode);
};
const handleEdit = (id) => {
let t = todos.filter(i => i.id === id);
setTodo(t[0].todo);
let newTodos = todos.filter(item => item.id !== id);
setTodos(newTodos);
saveToLS();
};
const handleDelete = (id) => {
let newTodos = todos.filter(item => item.id !== id);
setTodos(newTodos);
saveToLS();
};
const handleAdd = () => {
setTodos([...todos, { id: uuidv4(), todo, isCompleted: false }]);
setTodo("");
saveToLS();
};
const handleChange = (e) => {
setTodo(e.target.value);
};
const handleCheckbox = (e) => {
let id = e.target.name;
let index = todos.findIndex(item => item.id === id);
let newTodos = [...todos];
newTodos[index].isCompleted = !newTodos[index].isCompleted;
setTodos(newTodos);
saveToLS();
};
return (
<div className={`${darkMode ? 'bg-gray-500 text-white' : 'bg-violet-200 text-black'} min-h-screen flex flex-col`}>
<Navbar darkMode={darkMode} toggleDarkMode={toggleDarkMode} />
<div className={`flex-grow mx-3 md:container md:mx-auto my-5 rounded-xl p-5 shadow-2xl min-h-[80vh] md:w-[35%] ${darkMode ? 'bg-gray-800 shadow-xl' : 'bg-violet-100 shadow-2xl'}`}>
<h1 className='font-bold text-center text-3xl'>eTask - Manage your todos at one place</h1>
<div className="addTodo my-5 flex flex-col gap-4">
<h2 className='text-2xl font-bold'>Add a Todo</h2>
<div className="flex">
<input onChange={handleChange} value={todo} type="text" className='w-full rounded-full px-5 py-1 text-black' />
<button onClick={handleAdd} disabled={todo.length <= 3} className='bg-violet-800 mx-2 rounded-full hover:bg-violet-950 disabled:bg-violet-500 p-4 py-2 text-sm font-bold text-white'>Save</button>
</div>
</div>
<input className='my-4' id='show' onChange={toggleFinished} type="checkbox" checked={showFinished} />
<label className='mx-2' htmlFor="show">Show Finished</label>
<div className='h-[1px] bg-black opacity-15 w-[90%] mx-auto my-2'></div>
<h2 className='text-2xl font-bold'>Your Todos</h2>
<div className="todos">
{todos.length === 0 && <div className='m-5'>No Todos to display</div>}
{todos.map(item => {
return (showFinished || !item.isCompleted) && (
<div key={item.id} className="todo flex my-3 justify-between">
<div className='flex gap-5'>
<input name={item.id} onChange={handleCheckbox} type="checkbox" checked={item.isCompleted} />
<div className={item.isCompleted ? "line-through" : ""}>{item.todo}</div>
</div>
<div className="buttons flex h-full">
<button onClick={() => handleEdit(item.id)} className='bg-violet-800 hover:bg-violet-950 p-2 py-1 text-sm font-bold text-white rounded-md mx-1'><FaEdit /></button>
<button onClick={() => { handleDelete(item.id) }} className='bg-violet-800 hover:bg-violet-950 p-2 py-1 text-sm font-bold text-white rounded-md mx-1'><AiFillDelete /></button>
</div>
</div>
)
})}
</div>
</div>
<Footer darkMode={darkMode} />
</div>
);
}
export default App;
// const AppWrapper = () => (
// <ThemeProvider>
// <App />
// </ThemeProvider>
// );
// export default AppWrapper;