subjectPostReducer.js
4.57 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
/**
* # 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,
} = 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,
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',postsTitle)
.set('praise',praise)
.set('praiseUsers',Immutable.fromJS(praiseUsers))
.set('publishTimeString',publishTimeString)
.set('shareProductSkn',shareProductSkn)
.set('LZ',LZ)
.set('isContentFetching', false).set('contentError', null);
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;
default:
return initialState;
break;
}
}