subjectPostReducer.js
6.27 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
/**
* # 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,
} = 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: {
return state.set('id', action.payload);
}
case SUBJECT_CONTENT_REQUEST: {
let nextState = state.set('isContentFetching', true).set('contentError', null);
return nextState;
}
break;
case SUBJECT_CONTENT_SUCCESS:{
let {
authorInfo,
blocks,// contentData order templateKey
browse,
createTime,
forumCode,
forumName,
id,
isForumTop,
isHot,
isIndexTop,
postsTitle,
praise,
praiseUsers,//headIcon nickName uid
shareGoods,
publishTimeString,
shareProductSkn,
revieweState,
hasPraise,
LZ,
} = action.payload;
let nextState = state.setIn(['authorInfo','headIcon'],authorInfo.headIcon||'')
.setIn(['authorInfo','nickName'],authorInfo.nickName||'')
.setIn(['authorInfo','uid'],authorInfo.uid||'')
.setIn(['authorInfo','url'],authorInfo.url||'')
.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',decodeURI(postsTitle))
.set('praise',praise)
.set('praiseUsers',Immutable.fromJS(praiseUsers))
.set('publishTimeString',publishTimeString)
.set('shareProductSkn',shareProductSkn)
.set('LZ',LZ)
.set('isContentFetching', false)
.set('contentError', null)
.set('revieweState',revieweState)
.set('hasPraise',hasPraise||'N');
if (shareGoods) {
nextState.setIn(['shareGoods','productName'],shareGoods.productName||'')
.setIn(['shareGoods','productUrl'],shareGoods.productUrl||'')
.setIn(['shareGoods','salesPrice'],shareGoods.salesPrice||'')
.setIn(['shareGoods','goodsImage'],shareGoods.goodsImage||'')
}
return nextState;
}
break;
case SUBJECT_CONTENT_FAILURE: {
let nextState = state.set('isContentFetching', false).set('contentError', action.payload);
return nextState;
}
break;
case SUBJECT_COMMENTS_SUCCESS:{
let {lastedTime, total, list, pageSize} = action.payload;
let tailCount = (total%pageSize)>0?1:0;
let totalPages = total/pageSize + tailCount;
let existedAry = state.commentList.toJS();
let nextData = [...existedAry,...list];
let page = state.currentPage;
page++;
let nextState = state.set('commentList',Immutable.fromJS(nextData))
.set('lastedTime',lastedTime)
.set('totalPages',totalPages)
.set('currentPage',page)
.set('commentCount',total);
return nextState;
}
break;
case SUBJECT_COMMENTS_REQUEST:{
let nextState = state.set('isCommentsFetching', true).set('commentsError', null);
return nextState;
}
break;
case SUBJECT_COMMENTS_FAILURE:
let nextState = state.set('isCommentsFetching', false).set('commentsError', action.payload);
return nextState;
break;
case SUBJECT_ASSETS_SELECTED: {
let data = action.payload||[];
let nextState = state.set('assets',Immutable.fromJS(data));
return nextState;
}
break;
case SUBJECT_REPLY_REQUEST: {
let nextState = state.set('isReplying',true).set('replyError',null);
return nextState;
}
break;
case SUBJECT_REPLY_SUCCESS:{
let nextState = state.set('assets',Immutable.fromJS([]))
.set('assetsUrlStr','')
.set('assetFinishCount',0)
.set('isReplying',false)
.set('replyError',error)
return nextState;
}
break;
case SUBJECT_REPLY_FAILURE: {
let nextState = state.set('isReplying',false).set('replyError',action.payload);
return nextState;
}
break;
case SUBJECT_REPLY_UPDATE: {
let {newStr,newCount} = action.payload;
let nextState = state.set('assetsUrlStr',newStr).set('assetFinishCount',newCount);
return nextState;
}
case SUBJECT_LIKE_REQUEST: {
return state.set('hasPraise','Y');
}
case SUBJECT_UNLIKE_REQUEST: {
return state.set('hasPraise','N');;
}
break;
case SUBJECT_DO_NOTHING:
return state;
break;
case GO_TO_LIKE_LIST:
return state;
break;
default:
return initialState;
break;
}
}