likeListReducer.js
2.66 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
'use strict';
/**
* ## Imports
*
* InitialState
*/
import InitialState from './likeListInitialState';
import Immutable, {List, Record} from 'immutable';
const {
GO_TO_LIKE_LIST,
LIKE_LIST_REQUEST,
LIKE_LIST_SUCCESS,
LIKE_LIST_FAILURE,
LIKE_LIST_CLEAN,
} = require('../../constants/actionTypes').default;
const initialState = new InitialState;
export default function likeList(state = initialState, action) {
if (!(state instanceof InitialState)) {
return initialState.merge(state);
}
switch (action.type) {
case GO_TO_LIKE_LIST: {
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('postId', action.payload.postId);
// items新增item
newItems = newItems.push(item);
let nextState = state.set('sid', sid)
.set('items', newItems)
.set('cleaned', false);
return nextState;
}
case LIKE_LIST_REQUEST: {
let sid = action.payload;
let nextState = state.setIn(['items', sid, 'isFetching'], true)
.setIn(['items', sid, 'error'], null);
return nextState;
}
case LIKE_LIST_SUCCESS: {
let {sid, json} = action.payload;
let {lastedTime, list, endReached, total} = json;
let item = state.items.get(sid);
item = item.set('isFetching', false)
.set('error', null)
.set('lastedTime', lastedTime)
.set('list', Immutable.fromJS(list))
.set('endReached', endReached)
.set('total', total);
let nextState = state.setIn(['items', sid], item);
return nextState;
}
case LIKE_LIST_FAILURE: {
let {sid, error} = action.payload;
let nextState = state.setIn(['items', sid, 'isFetching'], false)
.setIn(['items', sid, 'error'], error);
return nextState;
}
case LIKE_LIST_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;
}
}
return state;
}