sectionReducer.js
5.81 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
/**
* # guideReducer.js
*
* The reducer for all the actions from the various log states
*/
'use strict';
/**
* ## Imports
*
* InitialState
*/
import InitialState from './sectionInitialState';
import Immutable, {Map} from 'immutable';
const {
SECTION_HEADER_REQUEST,
SECTION_HEADER_SUCCESS,
SECTION_HEADER_FAILURE,
SECTION_NEW_POST_REQUEST,
SECTION_NEW_POST_SUCCESS,
SECTION_NEW_POST_FAILURE,
SECTION_HOT_POST_REQUEST,
SECTION_HOT_POST_SUCCESS,
SECTION_HOT_POST_FAILURE,
SECTION_SET_ACTIVE_TAB,
SECTION_CLEAN,
GO_TO_SECTION,
POST_LIKE_REQUEST,
POST_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 sectionReducer(state = initialState, action) {
if (!(state instanceof InitialState)) return initialState.merge(state);
switch (action.type) {
case GO_TO_SECTION: {
let sid = state.sid + 1;
// 获取新的初始状态item
let item = initialState.item;
// 设置item的value
item = item.set('id', action.payload.id)
.set('name', action.payload.name);
// items新增item
let newItems = state.items.push(item);
let nextState = state.set('sid', sid)
.set('items', newItems);
return nextState;
}
case SECTION_HEADER_REQUEST: {
let sid = action.payload;
let nextState = state.setIn(['items', sid, 'isFetching'], true)
.setIn(['items', sid, 'error'], null)
.setIn(['items', sid, 'ptr'], true);
return nextState;
}
case SECTION_HEADER_SUCCESS: {
let {sid, json} = action.payload;
let {notice, header} = json;
let item = state.items.get(sid);
item = item.set('isFetching', false)
.set('error', null)
.set('ptr', false)
.setIn(['header', 'uri'], header.uri)
.setIn(['header', 'title'], header.title)
.setIn(['header', 'post'], header.post)
.setIn(['header', 'comment'], header.comment)
.setIn(['header', 'like'], header.like)
.setIn(['notice', 'duration'], notice.duration)
.setIn(['notice', 'open'], notice.open)
.setIn(['notice', 'list'], Immutable.fromJS(notice.list));
let nextState = state.setIn(['items', sid], item);
return nextState;
}
case SECTION_HEADER_FAILURE: {
let {sid, error} = action.payload;
let nextState = state.setIn(['items', sid, 'isFetching'], false)
.setIn(['items', sid, 'error'], error)
.setIn(['items', sid, 'ptr'], false);
return nextState;
}
case SECTION_HOT_POST_REQUEST: {
let {sid, ptr} = action.payload;
let nextState = state.setIn(['items', sid, 'hot', 'isFetching'], true)
.setIn(['items', sid, 'hot', 'error'], null);
return nextState;
}
case SECTION_HOT_POST_SUCCESS: {
let {sid, json} = action.payload;
let {lastedTime, list, endReached} = json;
let item = state.items.get(sid);
item = item.setIn(['hot', 'isFetching'], false)
.setIn(['hot', 'error'], null)
.setIn(['hot', 'lastedTime'], lastedTime)
.setIn(['hot', 'endReached'], endReached)
.setIn(['hot', 'list'], Immutable.fromJS(list));
let nextState = state.setIn(['items', sid], item);
return nextState;
}
case SECTION_HOT_POST_FAILURE: {
let {sid, error} = action.payload;
let nextState = state.setIn(['items', sid, 'hot', 'isFetching'], false)
.setIn(['items', sid, 'hot', 'error'], error);
return nextState;
}
case SECTION_NEW_POST_REQUEST: {
let {sid, ptr} = action.payload;
let nextState = state.setIn(['items', sid, 'new', 'isFetching'], true)
.setIn(['items', sid, 'new', 'error'], null);
return nextState;
}
case SECTION_NEW_POST_SUCCESS: {
let {sid, json} = action.payload;
let {lastedTime, list, endReached} = json;
let item = state.items.get(sid);
item = item.setIn(['new', 'isFetching'], false)
.setIn(['new', 'error'], null)
.setIn(['new', 'lastedTime'], lastedTime)
.setIn(['new', 'endReached'], endReached)
.setIn(['new', 'list'], Immutable.fromJS(list));
let nextState = state.setIn(['items', sid], item);
return nextState;
}
case SECTION_NEW_POST_FAILURE: {
let {sid, error} = action.payload;
let nextState = state.setIn(['items', sid, 'new', 'isFetching'], false)
.setIn(['items', sid, 'new', 'error'], error);
return nextState;
}
case SECTION_SET_ACTIVE_TAB: {
let {sid, tab} = action.payload;
let item = state.items.get(sid);
item = item.set('activeTab', tab);
let nextState = state.setIn(['items', sid], item);
return nextState;
}
case POST_LIKE_REQUEST: {
return sectionNewLikeState(state, action.payload, true);
}
case POST_UNLIKE_REQUEST: {
return sectionNewLikeState(state, action.payload, false);
}
case SECTION_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);
return nextState;
}
}
}
return state;
}
function sectionNewLikeState(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;
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;
}