-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TodoApp.js
160 lines (149 loc) · 2.83 KB
/
TodoApp.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import React from "react"
import uuid from "uuid/v4"
import { useImmerReducer } from "use-immer"
const initialState = {
form: {
done: false,
text: "",
},
todos: [],
}
const actions = state => ({
toggleNextTodo() {
state.form.done = !state.form.done
},
updateNextTodo(text) {
state.form.text = text
},
appendNextTodo() {
if (!state.form.text) {
return
}
state.todos.unshift({
id: uuid(),
...state.form,
})
state.form.text = ""
},
toggleTodoByID(id) {
const todo = state.todos.find(each => each.id === id)
todo.done = !todo.done
},
updateTodoByID(id, text) {
const todo = state.todos.find(each => each.id === id)
todo.text = text
},
deleteTodoByID(id) {
const x = state.todos.findIndex(each => each.id === id)
state.todos.splice(x, 1)
},
})
function TodoAppReducer(state, action) {
switch (action.type) {
case "TOGGLE_NEXT_TODO":
actions(state).toggleNextTodo()
return
case "UPDATE_NEXT_TODO":
actions(state).updateNextTodo(action.text)
return
case "APPEND_NEXT_TODO":
actions(state).appendNextTodo()
return
case "TOGGLE_TODO_BY_ID":
actions(state).toggleTodoByID(action.id)
return
case "UPDATE_TODO_BY_ID":
actions(state).updateTodoByID(action.id, action.text)
return
case "DELETE_TODO_BY_ID":
actions(state).deleteTodoByID(action.id)
return
default:
throw new Error(`TodoAppReducer: action mismatch; action=${action}`)
}
}
const MemoTodoItem = React.memo(({ todo, dispatch }) => (
<div id={todo.id}>
<input
type="checkbox"
checked={todo.done}
onChange={e => {
dispatch({
type: "TOGGLE_TODO_BY_ID",
id: todo.id,
})
}}
/>
<input
type="text"
value={todo.text}
onChange={e => {
dispatch({
type: "UPDATE_TODO_BY_ID",
id: todo.id,
text: e.target.value,
})
}}
/>
<button
onClick={e => {
dispatch({
type: "DELETE_TODO_BY_ID",
id: todo.id,
})
}}
>
Delete
</button>
</div>
))
const TodoApp = () => {
const [state, dispatch] = useImmerReducer(TodoAppReducer, initialState)
return (
<div>
{/* Form */}
<form onSubmit={e => {
e.preventDefault()
dispatch({
type: "APPEND_NEXT_TODO",
})
}}>
<input
type="checkbox"
checked={state.form.done}
onChange={e => {
dispatch({
type: "TOGGLE_NEXT_TODO",
})
}}
/>
<input
type="text"
value={state.form.text}
onChange={e => {
dispatch({
type: "UPDATE_NEXT_TODO",
text: e.target.value,
})
}}
/>
<button type="submit" disabled={!state.form.text}>
Add
</button>
</form>
{/* Todos */}
{state.todos.map(each => (
<MemoTodoItem
key={each.id}
todo={each}
dispatch={dispatch}
/>
))}
{/* DEBUG */}
<pre>
{JSON.stringify(state, null, 2)}
</pre>
</div>
)
}
export default TodoApp