-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducer.js
66 lines (62 loc) · 1.34 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
63
64
65
66
import storage from "./ulti/storage.js";
const init = {
todos: storage.get(),
filter: "all",
filters: {
all: () => true,
active: (todo) => !todo.isCompleted,
completed: (todo) => todo.isCompleted,
},
editIndex: null,
};
const actions = {
ADD({ todos }, title) {
if (title) {
todos.push({
title,
isCompleted: false,
});
storage.set(todos);
}
},
TOGGLE_TODO({ todos }, index) {
todos[index].isCompleted = !todos[index].isCompleted;
storage.set(todos);
},
TOGGLE_ALL({ todos }, isCompleted) {
todos.forEach((todo) => (todo.isCompleted = isCompleted));
storage.set(todos);
},
SET_FILTER(state, type) {
state.filter = type;
},
REMOVE({ todos }, index) {
todos.splice(index, 1);
storage.set(todos);
},
REMOVE_COMPLETED(state) {
state.todos = state.todos.filter(state.filters.active);
storage.set(state.todos);
},
EDIT_TODO(state, index) {
state.editIndex = index;
},
UPDATE_TODO(state, title) {
if (state.editIndex !== null) {
if (title) {
state.todos[state.editIndex].title = title;
state.editIndex = null;
storage.set(state.todos);
} else {
this.REMOVE(state, state.editIndex);
}
}
},
CANCEL_EDIT(state) {
state.editIndex = null;
},
};
export default function reducer(state = init, action, args) {
actions[action] && actions[action](state, ...args);
return state;
}