stockStatsReducer.js
1.4 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
'use strict';
import StockStatsInitialState from './stockStatsInitialState'
import Immutable, {List, Record} from 'immutable';
const {
STOCK_STATS_REQUEST,
STOCK_STATS_SUCCESS,
STOCK_STATS_FAILURE,
} = require('../../constants/actionTypes').default;
const initialState = new StockStatsInitialState;
export default function stockStatsReducer(state = initialState, action) {
if (!(state instanceof StockStatsInitialState)) return initialState.merge(state);
switch (action.type) {
case STOCK_STATS_REQUEST: {
let nextState = state.set('isFetching', true).set('error', null);
return nextState;
}
case STOCK_STATS_SUCCESS: {
const {jsonData} = state;
let origin = jsonData.toJS();
let data = [...origin, ...action.payload.list];
let newPageCount = action.payload.list ? state.pageCount + 1 : state.pageCount;
let nextState = state.set('isFetching',false)
.set('sum', action.payload.sum)
.set('totalAmount', action.payload.totalAmount)
.set('pageCount', newPageCount)
.set('jsonData', List(data));
return nextState;
}
case STOCK_STATS_FAILURE: {
let nextState = state.set('isFetching',false)
.set('error', action.playload);
return nextState;
}
}
return state;
}