/
Todos.js
149 lines (125 loc) · 3.48 KB
/
Todos.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
import React, { Fragment, useReducer, useState } from 'react'
import { useReducerDevtools, useStateDevtools } from '../use-devtools'
export function Todos({ initialCount }) {
const stateReducer = useReducer(reducer, initialState)
const [state, dispatch] = useReducerDevtools(
stateReducer,
initialState,
'TODO'
)
const addTodo = useFormInput('')
function handleCheckboxChange(e) {
const id = parseInt(e.target.dataset.todo, 10)
e.target.checked
? dispatch({ type: 'COMPLETE_TODO', id })
: dispatch({ type: 'UNCOMPLETE_TODO', id })
}
function handleAddClick(e) {
e.preventDefault()
if (addTodo.value.trim() !== '') {
dispatch({ type: 'ADD_TODO', todo: addTodo.value })
}
}
return (
<Fragment>
<List items={state} onChange={handleCheckboxChange} />
<div>
<input placeholder="Add todo" {...addTodo} />
<button onClick={handleAddClick}>Add</button>
</div>
{sumCompleted(state)}
<div>
<button onClick={() => dispatch({ type: 'RESET' })}>Reset</button>
</div>
</Fragment>
)
}
const initialState = [
{ id: 0, content: 'Todo 0', completed: false },
{ id: 1, content: 'Todo 1', completed: false },
{ id: 2, content: 'Todo 2', completed: false },
]
function reducer(state, action) {
switch (action.type) {
case 'RESET':
return initialState
case 'COMPLETE_TODO': {
const item = getItem(state, action.id)
const index = getIndex(state, action.id)
return item ? update(state)({ ...item, completed: true })(index) : state
}
case 'ADD_TODO': {
const item = {
id: parseInt(state.length + 1, 10),
content: action.todo,
completed: false,
}
return [...state, item]
}
case 'LOAD_TODOS':
return state
case 'REMOVE_TODO':
return state
case 'UNCOMPLETE_TODO': {
const item = getItem(state, action.id)
const index = getIndex(state, action.id)
return item ? update(state)({ ...item, completed: false })(index) : state
}
default:
return action.type ? state : action
}
}
function List({ items, onChange }) {
return (
<ul>
{items.map(({ id, content, completed }) => (
<li key={id}>
<label htmlFor={id}>
<input
id={id}
data-todo={id}
type="checkbox"
checked={completed}
onChange={onChange}
/>
{content}
</label>
</li>
))}
</ul>
)
}
const getItem = (state, id) => state.filter(item => item.id === id)[0]
const getIndex = (state, id) => state.findIndex(item => item.id === id)
function sumCompleted(state) {
const sum = state.reduce((cnt, item) => (item.completed ? cnt + 1 : cnt), 0)
return sum === state.length
? 'Awesome, all todos completed!'
: `${sum} of ${state.length} completed`
}
function update(list) {
return item => {
return index => {
return index === 0
? [item, ...list.slice(1, list.length)]
: index === list.length - 1
? [...list.slice(0, list.length - 1), item]
: [...list.slice(0, index), item, ...list.slice(index + 1, list.length)]
}
}
}
function useFormInput(initialValue) {
const inputState = useState(initialValue)
const [value, setValue] = useStateDevtools(
inputState,
initialValue,
'ADD_INPUT'
)
function handleChange(e) {
setValue(e.target.value)
}
return {
value,
onChange: handleChange,
}
}