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
|
import { fromJS, List, Map } from 'immutable';
import notif from 'ba-api/notifMessage';
import {
FETCH_CONTACT_DATA,
SEARCH_CONTACT,
SHOW_DETAIL_CONTACT,
HIDE_DETAIL,
EDIT_CONTACT,
SUBMIT_CONTACT,
DELETE_CONTACT,
TOGGLE_FAVORITE,
ADD_CONTACT,
CLOSE_CONTACT_FORM,
CLOSE_NOTIF
} from 'ba-actions/actionTypes';
const initialState = {
contactList: List([]),
formValues: Map(),
selectedIndex: 0,
selectedId: '',
keywordValue: '',
avatarInit: '',
openFrm: false,
showMobileDetail: false,
notifMsg: '',
};
let editingIndex = 0;
const initialImmutableState = fromJS(initialState);
export default function reducer(state = initialImmutableState, action = {}) {
switch (action.type) {
case FETCH_CONTACT_DATA:
return state.withMutations((mutableState) => {
const items = fromJS(action.items);
mutableState.set('contactList', items);
});
case SEARCH_CONTACT:
return state.withMutations((mutableState) => {
action.keyword.persist();
const keyword = action.keyword.target.value.toLowerCase();
mutableState.set('keywordValue', keyword);
});
case ADD_CONTACT:
return state.withMutations((mutableState) => {
mutableState
.set('openFrm', true)
.set('formValues', Map())
.set('avatarInit', '');
});
case CLOSE_CONTACT_FORM:
return state.withMutations((mutableState) => {
mutableState
.set('openFrm', false)
.set('formValues', Map())
.set('avatarInit', '')
.set('notifMsg', notif.discard);
});
case EDIT_CONTACT:
return state.withMutations((mutableState) => {
editingIndex = state.get('contactList').indexOf(action.item);
mutableState
.set('openFrm', true)
.set('selectedId', action.item.get('id'))
.set('formValues', action.item)
.set('avatarInit', action.item.get('avatar'));
});
case SUBMIT_CONTACT:
return state.withMutations((mutableState) => {
const initItem = Map(action.newData);
if (state.get('selectedId') === action.newData.get('id')) {
// Update data
const avatar = action.avatar !== '' ? action.avatar : state.get('avatarInit');
const newItem = initItem.update((initUpdated) => (initUpdated.set('avatar', avatar)));
mutableState
.update('contactList', contactList => contactList.setIn(
[editingIndex], newItem
))
.set('notifMsg', notif.updated);
} else {
// Insert data
const avatar = action.avatar !== '' ? action.avatar : '/images/pp_boy.svg';
const id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36);
const newItem = initItem
.update('id', (val = id) => val)
.update('avatar', (val = avatar) => val)
.update('favorited', (val = false) => val);
mutableState
.update('contactList', contactList => contactList.unshift(newItem))
.set('selectedIndex', 0)
.set('notifMsg', notif.saved);
}
mutableState
.set('formValues', null)
.set('avatarInit', '')
.set('openFrm', false);
});
case SHOW_DETAIL_CONTACT:
return state.withMutations((mutableState) => {
const index = state.get('contactList').indexOf(action.item);
mutableState
.set('selectedIndex', index)
.set('showMobileDetail', true);
});
case HIDE_DETAIL:
return state.withMutations((mutableState) => {
mutableState.set('showMobileDetail', false);
});
case DELETE_CONTACT:
return state.withMutations((mutableState) => {
const index = state.get('contactList').indexOf(action.item);
mutableState
.update('contactList', contactList => contactList.splice(index, 1))
.set('notifMsg', notif.removed);
});
case TOGGLE_FAVORITE:
return state.withMutations((mutableState) => {
const index = state.get('contactList').indexOf(action.item);
mutableState.update('contactList', contactList => contactList
.setIn([index, 'favorited'], !state.getIn(['contactList', index, 'favorited']))
);
});
case CLOSE_NOTIF:
return state.withMutations((mutableState) => {
mutableState.set('notifMsg', '');
});
default:
return state;
}
}
|