listReducer.js
1.98 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
'use strict';
import InitialState from './listInitialState';
import Immutable, {Map} from 'immutable';
const {
SET_LIST_TYPE,
SET_AUTHOR_ID,
SET_TAG,
GET_ARTICLE_LIST_REQUEST,
GET_ARTICLE_LIST_SUCCESS,
GET_ARTICLE_LIST_FAILURE,
LIKE_ARTICLE,
} = require('../../constants/actionTypes').default;
const initialState = new InitialState;
export default function listReducer(state=initialState, action) {
switch(action.type) {
case SET_LIST_TYPE: {
return state.set('type', action.payload);
}
case SET_AUTHOR_ID: {
return state.set('authorId', action.payload);
}
case SET_TAG: {
return state.set('tag', action.payload);
}
case GET_ARTICLE_LIST_REQUEST: {
return state.setIn(['articles', 'isFetching'], true)
.setIn(['articles', 'error'], null)
.setIn(['articles', 'ptr'], action.payload);
}
case GET_ARTICLE_LIST_SUCCESS: {
let {
list,
currentPage,
pageCount,
total,
endReached,
} = action.payload;
let newState = state.setIn(['articles', 'isFetching'], false)
.setIn(['articles', 'error'], null)
.setIn(['articles', 'list'], Immutable.fromJS(list))
.setIn(['articles', 'currentPage'], currentPage)
.setIn(['articles', 'pageCount'], pageCount)
.setIn(['articles', 'total'], total)
.setIn(['articles', 'endReached'], endReached);
return newState;
}
case LIKE_ARTICLE: {
let {
article_id,
isLike,
} = action.payload;
let originAry = state.get('articles').get('list').toJS();
for (var i = 0; i < originAry.length; i++) {
if (originAry[i].id == article_id) {
console.log('......find....');
originAry[i].isPraise = isLike?'Y':'N';
}
}
let newState = state.setIn(['articles', 'list'], Immutable.fromJS(originAry));
return newState;
}
break;
case GET_ARTICLE_LIST_FAILURE: {
return state.setIn(['articles', 'isFetching'], false)
.setIn(['articles', 'error'], action.payload)
}
}
return state;
}