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
|
import { fromJS, List, Map } from 'immutable';
import notif from 'ba-api/notifMessage';
import {
FETCH_DATA_FORM,
ADD_NEW,
CLOSE_FORM,
SUBMIT_DATA,
REMOVE_ROW_FORM,
EDIT_ROW_FORM,
CLOSE_NOTIF
} from 'ba-actions/actionTypes';
const initialState = {
dataTable: List([]),
formValues: Map(),
editingId: '',
showFrm: false,
notifMsg: '',
};
const initialItem = (keyTemplate, anchor) => {
const [...rawKey] = keyTemplate.keys();
const staticKey = {};
for (let i = 0; i < rawKey.length; i += 1) {
if (rawKey[i] !== 'id') {
const itemIndex = anchor.findIndex(a => a.name === rawKey[i]);
if (itemIndex == -1) continue
staticKey[rawKey[i]] = anchor[itemIndex].initialValue;
}
}
return Map(staticKey);
};
const initialItemNew = (anchor) => {
const [...rawKey] = anchor.map((e) => e.name);
const staticKey = {};
for (let i = 0; i < rawKey.length; i += 1) {
if (rawKey[i] !== 'id') {
staticKey[rawKey[i]] = anchor[i].initialValue;
}
}
return Map(staticKey);
};
let editingIndex = 0;
const initialImmutableState = fromJS(initialState);
export default function reducer(state = initialImmutableState, action = {}) {
const { branch } = action;
switch (action.type) {
case `${branch}/${FETCH_DATA_FORM}`:
return state.withMutations((mutableState) => {
const items = fromJS(action.items);
mutableState.set('dataTable', items);
});
case `${branch}/${ADD_NEW}`:
return state.withMutations((mutableState) => {
const raw = state.get('dataTable').last();
const initial = initialItemNew(action.anchor);
mutableState.set('formValues', initial);
mutableState.set('showFrm', true);
});
case `${branch}/${SUBMIT_DATA}`:
return state.withMutations((mutableState) => {
if (state.get('editingId') === action.newData.get('id')) {
// Update data
mutableState
.update('dataTable', dataTable => dataTable.setIn([editingIndex], action.newData))
.set('notifMsg', notif.updated);
} else {
// Insert data
const id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36);
const initItem = Map(action.newData);
const newItem = initItem.update('id', (val = id) => val);
mutableState
.update('dataTable', dataTable => dataTable.unshift(newItem))
.set('notifMsg', notif.saved);
}
mutableState.set('showFrm', false);
mutableState.set('formValues', Map());
});
case `${branch}/${CLOSE_FORM}`:
return state.withMutations((mutableState) => {
mutableState
.set('formValues', Map())
.set('showFrm', false);
});
case `${branch}/${REMOVE_ROW_FORM}`:
return state.withMutations((mutableState) => {
const index = state.get('dataTable').indexOf(action.item);
mutableState
.update('dataTable', dataTable => dataTable.splice(index, 1))
.set('notifMsg', notif.removed);
});
case `${branch}/${EDIT_ROW_FORM}`:
return state.withMutations((mutableState) => {
editingIndex = state.get('dataTable').indexOf(action.item);
mutableState
.set('formValues', action.item)
.set('editingId', action.item.get('id'))
.set('showFrm', true);
});
case `${branch}/${CLOSE_NOTIF}`:
return state.withMutations((mutableState) => {
mutableState.set('notifMsg', '');
});
default:
return state;
}
}
|