-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
133 lines (122 loc) · 3.86 KB
/
index.html
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
href="https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="style.css" />
<title>React Todo</title>
</head>
<body>
<div id="root"></div>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script
src="https://unpkg.com/react@16/umd/react.development.js"
crossorigin
></script>
<script
src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"
crossorigin
></script>
<script type="text/babel">
const categoryToLabel = {
pending: "Pending",
completed: "Completed",
};
function App() {
const [pendingTodos, setPendingTodos] = React.useState([]);
const [completedTodos, setCompletedTodos] = React.useState([]);
const [currentTodo, setCurrentTodo] = React.useState("");
function deleteTodo(todoIndex, status) {
const targetList =
status === "pending" ? pendingTodos : completedTodos;
const setter =
status === "pending" ? setPendingTodos : setCompletedTodos;
const filteredList = targetList.filter(
(pendingTodo, index) => index != todoIndex
);
setter(filteredList);
}
function movePendingTodo(todo, index) {
deleteTodo(index, "pending");
setCompletedTodos([...completedTodos, todo]);
}
function onSubmit(e) {
e.preventDefault();
if (currentTodo.trim() !== "") {
setPendingTodos([...pendingTodos, currentTodo]);
setCurrentTodo("");
}
}
return (
<div className="todo">
<h1 className="h1">todolist</h1>
<div className="input">
<form type="submit" onSubmit={onSubmit}>
<input
type="text"
name="name"
placeholder="Add todo..."
value={currentTodo}
onChange={(e) => setCurrentTodo(e.target.value)}
autoComplete="off"
/>
</form>
</div>
<CategoryToDisplay
category="pending"
todos={pendingTodos}
movePendingTodo={movePendingTodo}
deleteTodo={deleteTodo}
/>
<CategoryToDisplay
category="completed"
todos={completedTodos}
deleteTodo={deleteTodo}
/>
</div>
);
}
function CategoryToDisplay({
category,
todos,
movePendingTodo,
deleteTodo,
}) {
return (
<div className="category">
<div className="subheader">
{categoryToLabel[category]} <hr></hr>
</div>
{todos.map((todo, index) => (
<div key={index} className="subCategory">
<div className="targetTodo">
<p>{todo} </p>
</div>
<div className="buttons">
{category === "completed" ? null : (
<button onClick={() => movePendingTodo(todo, index)}>
✅
</button>
)}
<button onClick={() => deleteTodo(index, category)}>
❌
</button>
</div>
<hr />
</div>
))}
</div>
);
}
ReactDOM.render(<App />, document.getElementById("root"));
</script>
</body>
</html>