-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToDoList.js
70 lines (61 loc) · 2.5 KB
/
ToDoList.js
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
import { useState ,useEffect} from "react";
export default function ToDoList() {
function getStoredTodos() {
let data=localStorage.getItem("todos")
let json=JSON.parse(data)
if(json){
return json
}
return[]
}
const [todos, setTodos] = useState(getStoredTodos());
useEffect(()=>{
localStorage.setItem("todos", JSON.stringify(todos))
},[todos])
function handleSubmit(event) {
event.preventDefault();
let task = event.target.task.value;
if (!task) {
alert("Please provide a valid task!");
return;
}
setTodos([...todos, { task: task, completed: false }]);
event.target.reset();
}
function changeTaskStatus(index) {
let newTodos = [...todos];
newTodos[index].completed = !newTodos[index].completed;
setTodos(newTodos);
}
function deleteTask(index){
let newTodos=[...todos]
newTodos.splice(index, 1)
setTodos(newTodos);
}
return (
<div className="container my-5">
<div className="mx-auto rounded border p-4" style={{ width: "600px", backgroundColor: "#08618d" }}>
<h2 className="text-white text-center mb-5">Organize Your Day !!</h2>
<form className="d-flex" onSubmit={handleSubmit}>
<input className="form-control me-2" placeholder="New Task" name="task" />
<button className="btn btn-outline-light" type="submit">Add</button>
</form>
{todos.map((todo, index) => (
<div key={index} className="rounded mt-4 p-2 d-flex" style={{ backgroundColor: todo.completed ? "#87FC68" : "LightGray" }}>
<div className="me-auto">{todo.task}</div>
<div>
<i
className={"h5 me-2 " + (todo.completed ? "bi bi-check2-square" : "bi bi-square")}
style={{ cursor: "pointer" }}
onClick={() => changeTaskStatus(index)}
></i>
<i className="bi bi-trash3 h5"
style={{ cursor: "pointer" }}
onClick={() => deleteTask(index)}></i>
</div>
</div>
))}
</div>
</div>
);
}