listReducer.js
2.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
89
90
91
92
93
94
95
'use strict';
import InitialState from './listInitialState';
import Immutable, {Map} from 'immutable';
const {
GET_PRODUCT_LIST_REQUEST,
GET_PRODUCT_LIST_SUCCESS,
GET_PRODUCT_LIST_FAILURE,
FETCH_BANNER_REQUEST,
FETCH_BANNER_SUCCESS,
FETCH_BANNER_FAILURE,
SET_SELECTED_ITEM,
LIKE_REQUEST,
LIKE_SUCCESS,
DETAIL_LIKE_CHANGED,
} = require('../../constants/actionTypes').default;
const initialState = new InitialState;
export default function listReducer(state=initialState, action) {
switch(action.type) {
case LIKE_REQUEST:{
let newState = state.set('liking', true);
return newState;
}
break;
case LIKE_SUCCESS: {
let {currentProduct, index} = action.payload;
let newState = state.set('liking', false).setIn(['products', 'list', index], currentProduct);
return newState;
}
break;
case DETAIL_LIKE_CHANGED: {
let {
likeStatus,
likeCount,
} = action.payload;
let index = state.get('selected_index');
index = index - 1;//这个selected_index当初是给埋点用的,埋点从1开始,而数组从0开始,故减一。
return state.setIn(['products', 'list', index, 'is_praised'], likeStatus)
.setIn(['products', 'list', index, 'total_praised'], likeCount);
}
case SET_SELECTED_ITEM: {
let {selected_index, selected_skn} = action.payload;
let newState = state.set('selected_skn', selected_skn).set('selected_index',selected_index);
return newState;
}
break;
case GET_PRODUCT_LIST_REQUEST: {
let newState = state.setIn(['products', 'isFetching'], true)
.setIn(['products', 'ptr'], action.payload);
return newState;
}
case GET_PRODUCT_LIST_SUCCESS: {
let {
list,
currentPage,
pageCount,
total,
endReached,
reload,
} = action.payload;
let newState = state.setIn(['products', 'isFetching'], false)
.setIn(['products', 'ptr'], reload)
.setIn(['products', 'error'], null)
.setIn(['products', 'list'], Immutable.fromJS(list))
.setIn(['products', 'currentPage'], currentPage)
.setIn(['products', 'pageCount'], pageCount)
.setIn(['products', 'total'], total)
.setIn(['products', 'endReached'], endReached);
return newState;
}
case GET_PRODUCT_LIST_FAILURE: {
return state.setIn(['products', 'isFetching'], false)
.setIn(['products', 'error'], action.payload)
.setIn(['products', 'ptr'], false);
}
case FETCH_BANNER_REQUEST: {
return state.setIn(['banner', 'isFetching'], true);
}
case FETCH_BANNER_SUCCESS: {
return state.setIn(['banner', 'isFetching'], false)
.setIn(['banner', 'data'], Immutable.fromJS(action.payload));
}
case FETCH_BANNER_FAILURE: {
return state.setIn(['banner', 'isFetching'], false)
.setIn(['banner', 'error'], action.payload);
}
}
return state;
}