plustarReducer.js
2.21 KB
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
import * as types from '../constants/ActionTypes';
let initialState = {
payload: {
left: {
loading: false,
head: [],
foot: [],
data: [],
segment: null,
},
right: {
loading: false,
head: [],
foot: [],
data: [],
segment: null,
},
selectedIndex: 0,
genderType: 0,
},
error: false,
};
export default function plustar(state=initialState, action) {
switch(action.type) {
case types.REQUEST_PLASTAR_DATA: {
return Object.assign({}, state, {
payload: loadingPayload(state.payload, action, true)
});
}
case types.RECEIVE_PLASTAR_DATA: {
if(action.error) {
return Object.assign({}, state, {
payload: loadingPayload(state.payload, action, false),
error: action.error,
});
}
return Object.assign({}, state, {
payload: dataPayload(state.payload, action)
});
}
case types.SWITCH_SEGMENT: {
let payload = Object.assign({}, state.payload, {
selectedIndex: action.payload.selectedIndex,
});
return Object.assign({}, state, {
payload: payload
});
}
case types.SWITCH_GENDER: {
let payload = Object.assign({}, state.payload, {
genderType: action.payload.genderType,
});
return Object.assign({}, state, {
payload: payload
});
}
default:
return state;
}
}
function loadingPayload(payload, action, loading) {
let newPayload = Object.assign({}, payload, {
[action.payload.selectedItem]: Object.assign({}, payload[action.payload.selectedItem], {
loading,
}),
});
return newPayload;
}
function dataPayload(payload, action) {
let newPayload = Object.assign({}, payload, {
[action.payload.selectedItem]: action.payload[action.payload.selectedItem],
});
return newPayload;
}