-
Notifications
You must be signed in to change notification settings - Fork 56
/
todos.jsx
155 lines (154 loc) · 4.65 KB
/
todos.jsx
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
import {
ADD_TODO,
DELETE_TODO,
CHANGE_TODO_TO_DOING,
CHANGE_DOING_TO_DONE,
CHANGE_DOING_TO_TODO,
CAHNGE_DONE_TO_DOING,
SEARCH,
} from '../actions';
let todos;
(() => {
if (localStorage.todos) {
todos = JSON.parse(localStorage.todos);
} else {
todos = [];
}
})();
const todolist = (state = todos, action) => {
switch (action.type) {
/*
* 添加新的事项
* 并进行本地化存储
* 使用ES6展开运算符链接新事项和旧事项
* JSON.stringify进行对象深拷贝
*/
case ADD_TODO:
return [
...state, {
todo: action.text,
istodo: true,
doing: false,
done: false
}
];
/*
* 将todo转为doing状态,注意action.index的类型转换
*/
case CHANGE_TODO_TO_DOING:
localStorage.setItem('todos', JSON.stringify([
...state.slice(0, action.index),
{
todo:state[action.index].todo,
istodo: false,
doing: true,
done: false
},
...state.slice(parseInt(action.index) + 1)
]));
return [
...state.slice(0, action.index),
{
todo:state[action.index].todo,
istodo: false,
doing: true,
done: false
},
...state.slice(parseInt(action.index) + 1)
];
/*
* 将doing转为done状态
*/
case CHANGE_DOING_TO_DONE:
localStorage.setItem('todos', JSON.stringify([
...state.slice(0, action.index),
{
todo:state[action.index].todo,
istodo: false,
doing: false,
done: true
},
...state.slice(parseInt(action.index) + 1)
]));
return [
...state.slice(0, action.index),
{
todo:state[action.index].todo,
istodo: false,
doing: false,
done: true
},
...state.slice(parseInt(action.index) + 1)
];
/*
* 将done转为doing状态
*/
case CAHNGE_DONE_TO_DOING:
localStorage.setItem('todos', JSON.stringify([
...state.slice(0, action.index),
{
todo:state[action.index].todo,
istodo: false,
doing: true,
done: false
},
...state.slice(parseInt(action.index) + 1)
]));
return [
...state.slice(0, action.index),
{
todo:state[action.index].todo,
istodo: false,
doing: true,
done: false
},
...state.slice(parseInt(action.index) + 1)
];
/*
* 将doing转为todo状态
*/
case CHANGE_DOING_TO_TODO:
localStorage.setItem('todos', JSON.stringify([
...state.slice(0, action.index),
{
todo:state[action.index].todo,
istodo: true,
doing: false,
done: false
},
...state.slice(parseInt(action.index) + 1)
]));
return [
...state.slice(0, action.index),
{
todo:state[action.index].todo,
istodo: true,
doing: false,
done: false
},
...state.slice(parseInt(action.index) + 1)
];
/*
* 删除某个事项
*/
case DELETE_TODO:
localStorage.setItem('todos', JSON.stringify([
...state.slice(0, action.index),
...state.slice(parseInt(action.index) + 1)
]));
return [
...state.slice(0, action.index),
...state.slice(parseInt(action.index) + 1)
];
/*
* 搜索
*/
case SEARCH:
let text = action.text;
let reg = eval("/"+text+"/gi");
return state.filter(item=> item.todo.match(reg));
default:
return state;
}
}
export default todolist;