userReducer.js
10.1 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
'use strict'
import InitialState from './userInitialState';
import Immutable, {List, Record} from 'immutable';
const {
USER_BACKGROUND_TAP,
USER_POSTS_REQUEST,
USER_POSTS_SUCCESS,
USER_POSTS_FAILURE,
USER_LIKE_REQUEST,
USER_LIKE_SUCCESS,
USER_LIKE_FAILURE,
USER_REPLY_REQUEST,
USER_REPLY_SUCCESS,
USER_REPLY_FAILURE,
USER_UNREAD_REQUEST,
USER_UNREAD_SUCCESS,
USER_UNREAD_FAILURE,
USER_GO_TO_SETTING_STATS,
SYNC_USER_REQUEST,
SYNC_USER_SUCCESS,
SYNC_USER_FAILURE,
USER_INFO_REQUEST,
USER_INFO_SUCCESS,
USER_INFO_FAILURE,
SET_UID,
USER_CLEAN,
USER_SET_ACTIVE_TAB,
USER_AVATAR_UPLOADING,
USER_AVATAR_UPLOAD_SUCCESS,
USER_AVATAR_UPLOAD_FAILURE,
USER_BG_UPLOADING,
USER_BG_UPLOAD_SUCCESS,
USER_BG_UPLOAD_FAILURE,
POST_LIKE_REQUEST,
POST_UNLIKE_REQUEST,
SETTING_CLEAN,
SETTING_SAVE_REQUEST,
SETTING_SAVE_SUCCESS,
SETTING_SAVE_FAILURE,
SETTING_MODI_NICKNAME,
SETTING_MODI_SIGNATURE,
USER_DID_LOGOUT,
USER_BG_UPDATE,
USER_AVATAR_UPDATE,
USER_NICKNAME_UPDATE,
GO_TO_MESSAGE_CENTER,
} = require('../../constants/actionTypes').default;
const initialState = new InitialState;
export default function user(state = initialState, action) {
if (!(state instanceof InitialState)) {
return initialState.merge(state);
}
switch (action.type) {
case USER_POSTS_REQUEST: {
let nextState = state.setIn(['posts', 'isFetching'], true)
.set('error', null)
.set('ptr', action.payload);
return nextState;
}
case USER_LIKE_REQUEST: {
let nextState = state.setIn(['like', 'isFetching'], true)
.set('error', null)
.set('ptr', action.payload);
return nextState;
}
case USER_REPLY_REQUEST: {
let nextState = state.setIn(['reply', 'isFetching'], true)
.set('error', null)
.set('ptr', action.payload);
return nextState;
}
case USER_POSTS_SUCCESS: {
// let nextState = state.set
let {lastedTime, list, endReached} = action.payload;
let nextState = state.setIn(['posts', 'isFetching'], false)
.setIn(['posts', 'error'], null)
.setIn(['posts', 'lastedTime'], lastedTime)
.setIn(['posts', 'endReached'], endReached)
.setIn(['posts', 'list'], Immutable.fromJS(list))
.set('ptr', false);
return nextState;
}
case USER_LIKE_SUCCESS: {
// let nextState = state.set
let {lastedTime, list, endReached} = action.payload;
let nextState = state.setIn(['like', 'isFetching'], false)
.setIn(['like', 'error'], null)
.setIn(['like', 'lastedTime'], lastedTime)
.setIn(['like', 'endReached'], endReached)
.setIn(['like', 'list'], Immutable.fromJS(list))
.set('ptr', false);
return nextState;
}
case USER_REPLY_SUCCESS: {
// let nextState = state.set
let {lastedTime, list, endReached} = action.payload;
let nextState = state.setIn(['reply', 'isFetching'], false)
.setIn(['reply', 'error'], null)
.setIn(['reply', 'lastedTime'], lastedTime)
.setIn(['reply', 'endReached'], endReached)
.setIn(['reply', 'list'], Immutable.fromJS(list))
.set('ptr', false);
return nextState;
}
case USER_POSTS_FAILURE: {
let nextState = state.setIn(['posts', 'isFetching'], false)
.set('error', action.payload)
.set('ptr', false);
return nextState;
}
case USER_LIKE_FAILURE: {
let nextState = state.setIn(['like', 'isFetching'], false)
.set('error', action.payload)
.set('ptr', false);
return nextState;
}
case USER_REPLY_FAILURE: {
let nextState = state.setIn(['reply', 'isFetching'], false)
.set('error', action.payload)
.set('ptr', false);
return nextState;
}
case USER_GO_TO_SETTING_STATS:
return state;
case SYNC_USER_REQUEST: {
let nextState = state.setIn(['profile', 'isFetching'], true)
.setIn(['profile', 'error'], null);
return nextState;
}
case SYNC_USER_SUCCESS: {
let {avatar, backgroundImage, nickName, realName, gender, sign, age, birthday, height, weight, msgCount} = action.payload;
let nextState = state.setIn(['profile', 'isFetching'], false)
.setIn(['profile', 'error'], null)
.setIn(['profile', 'avatar'], avatar)
.setIn(['profile', 'backgroundImage'], backgroundImage)
.setIn(['profile', 'nickName'], nickName)
.setIn(['profile', 'realName'], realName)
.setIn(['profile', 'gender'], gender)
.setIn(['profile', 'sign'], sign)
.setIn(['profile', 'age'], age)
.setIn(['profile', 'birthday'], birthday)
.setIn(['profile', 'height'], height)
.setIn(['profile', 'weight'], weight)
.setIn(['profile', 'msgCount'], msgCount)
.setIn(['setting', 'nickName'], nickName)
.setIn(['setting', 'signature'], sign);
return nextState;
}
case SYNC_USER_FAILURE: {
let nextState = state.setIn(['profile', 'isFetching'], false)
.setIn(['profile', 'error'], action.payload);
return nextState;
}
case SET_UID:
return state.setIn(['profile', 'uid'], action.payload);
case USER_SET_ACTIVE_TAB:
return state.set('activeTab', action.payload);
case USER_CLEAN: {
let nextState = initialState.set('profile', state.profile);
return nextState;
}
case USER_UNREAD_SUCCESS:
return state.setIn(['profile', 'msgCount'], action.payload);
case USER_AVATAR_UPLOADING:{
let nextState = state.set('avatarUploading',true).set('avatarUploadError', null);
return nextState;
}
case USER_AVATAR_UPLOAD_SUCCESS:{
let nextState = state.set('avatarUploading', false).set('avatarUploadError', null);
return nextState;
}
case USER_AVATAR_UPLOAD_FAILURE: {
let nextState = state.set('avatarUploading', false).set('avatarUploadError', action.payload);
return nextState;
}
case USER_BG_UPLOADING: {
let nextState = state.set('bgUploading', true).set('bgUploadError', null);
return nextState;
}
case USER_BG_UPLOAD_SUCCESS: {
let nextState = state.set('bgUploading', false).set('bgUploadError', null);
return nextState;
}
case USER_BG_UPLOAD_FAILURE: {
let nextState = state.set('bgUploading', false).set('bgUploadError', action.payload);
return nextState;
}
case POST_LIKE_REQUEST: {
return userCenterNewLikeState(state, action.payload, true);
}
case POST_UNLIKE_REQUEST: {
return userCenterNewLikeState(state, action.payload, false);
}
case SETTING_CLEAN: {
return state.setIn(['setting', 'nickName'], state.profile.nickName)
.setIn(['setting','signature'], state.profile.sign);
}
case SETTING_SAVE_REQUEST:
return state.setIn(['setting', 'isFetching'], true)
.setIn(['setting', 'error'], null)
.setIn(['setting','success'], false);
case SETTING_SAVE_SUCCESS: {
return state.setIn(['setting', 'isFetching'], false)
.setIn(['setting', 'error'], null)
.setIn(['profile','nickName'], state.setting.nickName)
.setIn(['profile','sign'], state.setting.signature)
.setIn(['setting','success'], true);
}
case SETTING_SAVE_FAILURE:
return state.setIn(['setting', 'isFetching'], false)
.setIn(['setting', 'error'], action.payload)
.setIn(['setting','success'], false);
case SETTING_MODI_NICKNAME:
return state.setIn(['setting', 'nickName'], action.payload);
case SETTING_MODI_SIGNATURE:
return state.setIn(['setting', 'signature'], action.payload);
case USER_DID_LOGOUT:
return initialState;
case USER_AVATAR_UPDATE:
return state.setIn(['profile', 'avatar'], action.payload);
case USER_BG_UPDATE:
return state.setIn(['profile', 'backgroundImage'], action.payload);
case USER_NICKNAME_UPDATE:
return state.setIn(['profile', 'nickName'], action.payload)
.setIn(['setting', 'nickName'], action.payload);
}
return state;
}
function userCenterNewLikeState(state, postId, like) {
let likeState = (list, postId, like) => {
let index = list.findIndex((item) => {
return item.get('id') == postId;
})
if (index == -1) {
return list;
}
let likeCount = 0;
if (like) {
likeCount = list.get(index).get('likeCount') + 1;
} else {
likeCount = list.get(index).get('likeCount') - 1;
likeCount = likeCount < 0 ? 0 : likeCount;
}
let nextList = list.setIn([index, 'isLike'], like)
.setIn([index, 'likeCount'], likeCount);
return nextList;
};
// let nextState = state;
let postsList = likeState(state.posts.list, postId, like);
let likeList = likeState(state.like.list, postId, like);
let nextState = state.setIn(['posts', 'list'], postsList)
.setIn(['like', 'list'], likeList);
return nextState;
// state.items.map((item, i) => {
// let hotList = likeState(item.hot.list, postId, like);
// let newList = likeState(item.new.list, postId, like);
//
// item = item.setIn(['hot', 'list'], hotList)
// .setIn(['new', 'list'], newList);
// nextState = nextState.setIn(['items', i], item);
// });
//
// return nextState;
}