userReducer.js
3.64 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
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
156
157
158
159
/**
* # guideReducer.js
*
* The reducer for all the actions from the various log states
*/
'use strict';
/**
* ## Imports
*
* InitialState
*/
import InitialState from './userInitialState';
import Immutable, {List, Record} from 'immutable';
const {
DELETE_TOKEN_REQUEST,
DELETE_TOKEN_SUCCESS,
GET_STORED_USER_INFO,
ACCOUNT_CHANGE,
ACCOUNT_CLEAN,
PWD_CHANGE,
PWD_DISPLAY,
PWD_MODIFY,
PWD_MODIFY_OLD,
PWD_MODIFY_NEW,
PWD_MODIFY_REPEAT,
PWD_MODIFY_REQUEST,
PWD_MODIFY_SUCCESS,
PWD_MODIFY_FAILURE,
LOGIN_REQUEST,
LOGIN_SUCCESS,
LOGIN_FAILURE,
LOGOUT_REQUEST,
LOGOUT_SUCCESS,
LOGOUT_FAILURE,
SET_SESSION_TOKEN,
CHECK_UPDATE_SUCCESS,
CHECK_UPDATE_FAILURE,
LOGOUT,
} = require('../../constants/actionTypes').default;
const initialState = new InitialState;
/**
* ## guideReducer function
* @param {Object} state - initialState
* @param {Object} action - type and payload
*/
export default function userReducer(state = initialState, action) {
if (!(state instanceof InitialState)) return initialState.merge(state);
switch (action.type) {
case ACCOUNT_CHANGE:{
let nextState = state.setIn(['profile', 'account'], action.payload);
return nextState;
}
case ACCOUNT_CLEAN:{
let nextState = state.setIn(['profile', 'account'], '');
return nextState;
}
case PWD_DISPLAY:{
let nextState = state.set('isShowPwd', action.payload);
return nextState;
}
case PWD_CHANGE:{
let nextState = state.setIn(['profile', 'password'], action.payload);
return nextState;
}
case PWD_MODIFY_OLD:{
let nextState = state.setIn(['password', 'old'], action.payload);
return nextState;
}
case PWD_MODIFY_NEW:{
let nextState = state.setIn(['password', 'new'], action.payload);
return nextState;
}
case PWD_MODIFY_REPEAT:{
let nextState = state.setIn(['password', 'repeat'], action.payload);
return nextState;
}
case PWD_MODIFY_SUCCESS:{
let nextState = state.set('isFetching', false)
.set('error', null)
.setIn(['password', 'old'], '')
.setIn(['password', 'new'], '')
.setIn(['password', 'repeat'], '');
return nextState;
}
case LOGOUT:{
let nextState = state.setIn(['profile', 'pid'], '')
.setIn(['profile', 'account'], '')
.setIn(['profile', 'pid'], '')
.setIn(['profile', 'password'], '')
.setIn(['profile', 'sessionKey'], '');
return nextState;
}
case LOGIN_REQUEST:
case PWD_MODIFY_REQUEST: {
let nextState = state.set('isFetching', true)
.set('error', null);
return nextState;
}
case GET_STORED_USER_INFO:
case LOGIN_SUCCESS: {
const {account, pid, session_key, shops} = action.payload;
let brandsList = Immutable.fromJS(shops) || List();
let nextState = state.set('isFetching', false)
.set('error', null)
.setIn(['profile', 'account'], account)
.setIn(['profile', 'pid'], pid)
.setIn(['profile', 'sessionKey'], session_key)
.set('brands', brandsList);
return nextState;
}
case LOGOUT_SUCCESS:
return state.set('isFetching', false)
.set('error', null);
case LOGOUT_FAILURE:
case LOGIN_FAILURE:
case PWD_MODIFY_FAILURE:
return state.set('isFetching', false)
.set('error', action.payload);
case DELETE_TOKEN_REQUEST:
case DELETE_TOKEN_SUCCESS:
return state;
case CHECK_UPDATE_SUCCESS: {
let isLast = action.payload.lastVersion;
if (isLast || !action.payload.url) {
return state.set('isShowUpdate', false);
} else {
return state.set('isShowUpdate', true)
.set('updateUrl', action.payload.url);
}
}
case CHECK_UPDATE_FAILURE:
return state;
}
/**
* ## Default
*/
return state;
}