-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducer.js
62 lines (60 loc) · 1.55 KB
/
reducer.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
import storage from "./until/storage.js"
const init = {
todos: storage.get(),
filter: 'all',
filters: {
all: () => true,
active: todo => !todo.completed,
completed: todo => todo.completed,
},
editIndex: 'null',
}
const actions = {
add({ todos }, title) {
if(title) {
todos.push({ title, completed: false })
storage.set(todos)
}
},
toggle({ todos }, index) {
const todo = todos[index]
todo.completed = !todo.completed
storage.set(todos)
},
toggleAll({ todos }, completed) {
todos.forEach(todo => todo.completed = completed)
storage.set(todos)
},
destroy({ todos }, index) {
todos.splice(index, 1)
storage.set(todos)
},
switchFilter(state, filter) {
state.filter = filter
},
clearCompleted(state) {
state.todos = state.todos.filter(state.filters.active)
storage.set(state.todos)
},
startEdit(state, index) {
state.editIndex = index
},
endEdit(state, title) {
if(state.editIndex !== null) {
if(title) {
state.todos[state.editIndex].title = title
storage.set(state.todos)
} else {
this.destroy(state, state.editIndex)
}
state.editIndex = null
}
},
cancelEdit(state) {
state.editIndex = null
}
}
export default function reducer(state = init, action, args) {
actions[action] && actions[action](state, ...args)
return state
}