subjectPostReducer.js
9.9 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
/**
* # subjectPostReducer.js
*
* The reducer for all the actions from the various log states
*/
'use strict';
/**
* ## Imports
*
* InitialState
*/
import InitialState from './subjectPostInitialState';
import Immutable, {List, Record} from 'immutable';
const {
GO_TO_POST,
SUBJECT_CONTENT_REQUEST,
SUBJECT_CONTENT_SUCCESS,
SUBJECT_CONTENT_FAILURE,
SUBJECT_COMMENTS_REQUEST,
SUBJECT_COMMENTS_SUCCESS,
SUBJECT_COMMENTS_FAILURE,
SUBJECT_ASSETS_SELECTED,
SUBJECT_REPLY_REQUEST,
SUBJECT_REPLY_SUCCESS,
SUBJECT_REPLY_FAILURE,
SUBJECT_REPLY_UPDATE,
SUBJECT_DO_NOTHING,
GO_TO_LIKE_LIST,
SUBJECT_LIKE_SUCCESS,
SUBJECT_UNLIKE_SUCCESS,
SUBJECT_LIKE_REQUEST,
SUBJECT_UNLIKE_REQUEST,
SUBJECT_DEL_COMMENT_REQUEST,
SUBJECT_DEL_COMMENT_SUCCESS,
SUBJECT_DEL_COMMENT_FAILURE,
SUBJECT_SHARE_DATA_PREPARED,
SUBJECT_CLEAN,
} = require('../../constants/actionTypes').default;
const initialState = new InitialState;
/**
* ## guideReducer function
* @param {Object} state - initialState
* @param {Object} action - type and payload
*/
export default function postingReducer(state = initialState, action) {
if (!(state instanceof InitialState)) return initialState.merge(state);
switch (action.type) {
case GO_TO_POST: {
let currentSid = state.sid;
let newItems = state.items;
let newSid = currentSid;
// componentWillUnmount未调用,导致clean操作未进行
if (currentSid == -1) {
newItems = List();
}
if (action.payload.tryToClean && currentSid > -1 && !state.cleaned) {
newItems = newItems.pop();
newSid = newSid - 1;
}
let sid = newSid + 1;
// 获取新的初始状态item
let item = initialState.item;
// 设置item的value
item = item.set('id', action.payload.id);
// items新增item
newItems = newItems.push(item);
let nextState = state.set('sid', sid)
.set('items', newItems)
.set('cleaned', false);
return nextState;
}
case SUBJECT_CONTENT_REQUEST: {
let sid = action.payload;
let item = state.items.get(sid);
item = item.set('isContentFetching', true).set('contentError', null);
let nextState = state.setIn(['items', sid], item);
return nextState;
}
break;
case SUBJECT_CONTENT_SUCCESS:{
let {sid,json} = action.payload;
let item = state.items.get(sid);
item = postContentSuccess(item, json);
let nextState = state.setIn(['items', sid], item);
return nextState;
}
break;
case SUBJECT_CONTENT_FAILURE: {
let {sid,error} = action.payload;
let item = state.items.get(sid);
item = item.set('isContentFetching', false).set('contentError', error);
let nextState = state.setIn(['items', sid], item);
return nextState;
}
break;
case SUBJECT_COMMENTS_SUCCESS:{
let {sid, json} = action.payload;
let item = state.items.get(sid);
let {lastedTime, list, endReached, total} = json;
item = item.set('isCommentsFetching', false)
.set('commentsError', null)
.set('commentList', Immutable.fromJS(list))
.set('lastedTime', lastedTime)
.set('commentCount', total)
.set('commentReachEnd', endReached);
let nextState = state.setIn(['items', sid], item);
return nextState;
}
break;
case SUBJECT_COMMENTS_REQUEST:{
let sid = action.payload;
let item = state.items.get(sid);
item = item.set('isCommentsFetching', true).set('commentsError', null);
let nextState = state.setIn(['items', sid], item);
return nextState;
}
break;
case SUBJECT_COMMENTS_FAILURE:
let {sid,error} = action.payload;
let item = state.items.get(sid);
item = item.set('isCommentsFetching', false).set('commentsError', error);
let nextState = state.setIn(['items', sid], item);
return nextState;
break;
case SUBJECT_ASSETS_SELECTED: {
let {sid,assets} = action.payload;
let data = assets||[];
let item = state.items.get(sid);
item = item.set('assets',Immutable.fromJS(data));
let nextState = state.setIn(['items', sid], item);
return nextState;
}
break;
case SUBJECT_REPLY_REQUEST: {
let sid = action.payload;
let item = state.items.get(sid);
item = item.set('isReplying',true).set('replyError',null);
let nextState = state.setIn(['items', sid], item);
return nextState;
}
break;
case SUBJECT_REPLY_SUCCESS:{
let {sid, json, newList} = action.payload;
let item = state.items.get(sid);
item = item.set('assets',Immutable.fromJS([]))
.set('assetsUrlStr','')
.set('assetFinishCount',0)
.set('isReplying',false)
.set('replyError',error)
.set('commentList',Immutable.fromJS(newList));
let nextState = state.setIn(['items', sid], item);
return nextState;
}
break;
case SUBJECT_REPLY_FAILURE: {
let {sid,error} = action.payload;
let item = state.items.get(sid);
item = item.set('isReplying',false).set('replyError',action.payload);
let nextState = state.setIn(['items', sid], item);
return nextState;
}
break;
case SUBJECT_REPLY_UPDATE: {
let {sid, newStr,newCount} = action.payload;
let item = state.items.get(sid);
item = item.set('assetsUrlStr',newStr).set('assetFinishCount',newCount);
let nextState = state.setIn(['items', sid], item);
return nextState;
}
case SUBJECT_LIKE_REQUEST: {
let {sid,json} = action.payload;
let item = state.items.get(sid);
let users = item.praiseUsers.toJS();
let newUsers = [...users,{headIcon:json}];
item = item.set('hasPraise','Y').set('praiseUsers',Immutable.fromJS(newUsers));
let nextState = state.setIn(['items', sid], item);
return nextState;
}
case SUBJECT_UNLIKE_REQUEST: {
let {sid,json} = action.payload;
let item = state.items.get(sid);
let users = item.praiseUsers.toJS();
let newUsers = [];
for (var i = 0; i < users.length; i++) {
let item = users[i];
if (item.headIcon != json) {
newUsers.push(item);
}
}
item = item.set('hasPraise','N').set('praiseUsers',Immutable.fromJS(newUsers));
let nextState = state.setIn(['items', sid], item);
return nextState;
}
break;
case SUBJECT_DEL_COMMENT_REQUEST: {
let {sid, newArray} = action.payload;
let item = state.items.get(sid);
item = item.set('commentList',Immutable.fromJS(newArray));
let nextState = state.setIn(['items', sid], item);
return nextState;
}
break;
case SUBJECT_SHARE_DATA_PREPARED: {
let {sid, json} = action.payload;
let {shareUrl, shareImgUrl} = json
let item = state.items.get(sid);
item = item.set('shareUrl',shareUrl).set('defaultShareImage', shareImgUrl);
let nextState = state.setIn(['items', sid], item);
return nextState;
}
break;
case SUBJECT_CLEAN:{
let sid = action.payload;
if (state.items.size > sid) {
let items = state.items.pop();
let nextState = state.set('sid', sid - 1)
.set('items', items)
.set('cleaned', true);
return nextState;
}
return state;
}
break;
case SUBJECT_DO_NOTHING:
return state;
break;
case GO_TO_LIKE_LIST:
return state;
break;
default:
return state;
break;
}
return state;
}
function postContentSuccess(item, json) {
let {
authorInfo,
blocks,// contentData order templateKey
browse,
createTime,
forumCode,
forumName,
id,
isForumTop,
isHot,
isIndexTop,
postsTitle,
praise,
praiseUsers,//headIcon nickName uid
shareGoods,
revieweState,
hasPraise,
largeImages,
LZ,
} = json;
item = item.set('isContentFetching', false)
.set('contentError', null)
.set('blocks',Immutable.fromJS(blocks))
.set('browse',browse)
.set('createTime',createTime)
.set('forumCode',forumCode)
.set('forumName',forumName)
.set('id',id)
.set('isForumTop',isForumTop)
.set('isHot',isHot)
.set('isIndexTop',isIndexTop)
.set('postsTitle',postsTitle)
.set('praise',praise)
.set('praiseUsers',Immutable.fromJS(praiseUsers.reverse()))
.set('LZ',LZ)
.set('revieweState',revieweState)
.set('largeImages',largeImages)
.set('hasPraise',hasPraise||'N')
.setIn(['shareGoods','productName'],shareGoods.productName)
.setIn(['shareGoods','productUrl'],shareGoods.productUrl)
.setIn(['shareGoods','salesPrice'],shareGoods.salesPrice)
.setIn(['shareGoods','goodsImage'],shareGoods.goodsImage)
.setIn(['shareGoods','productId'],shareGoods.productId)
.setIn(['authorInfo','headIcon'],authorInfo.headIcon)
.setIn(['authorInfo','nickName'],authorInfo.nickName)
.setIn(['authorInfo','uid'],authorInfo.uid);
return item;
}