-
Notifications
You must be signed in to change notification settings - Fork 49
/
todos.js
50 lines (40 loc) · 1.1 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
import * as types from 'constants/ActionTypes';
import { List, Map } from 'immutable';
import { combineReducers } from 'redux';
function todoList(state = List(), action) {
switch (action.type) {
case types.ADD_TODO:
return state.push(Map({
id: action.id,
text: action.text,
isCompleted: false,
}));
case types.COMPLETE_TODO:
// map through todos to find matching ID
return state.map(todo => {
if (todo.get('id') === action.id) {
return todo.update('isCompleted', v => !v);
}
return todo;
});
case types.DELETE_TODO:
return state.filter(todo => todo.get('id') !== action.id);
case types.DELETE_ALL_TODOS:
return state.clear();
default:
return state;
}
}
function activeFilter(state = 'all', action) {
switch (action.type) {
case types.CHANGE_FILTER:
console.info('%cFilter changed: ' + action.filter.toUpperCase(), 'color:red; font-weight:bold;');
return action.filter;
default:
return state;
}
}
export default combineReducers({
activeFilter,
todoList,
});