requestStatsReducer.js 2.37 KB
'use strict';

import RequestStatsInitialState from './requestStatsInitialState'
import Immutable, {List, Record} from 'immutable';

const {
    REQUEST_STATS_REQUEST,
    REQUEST_STATS_SUCCESS,
    REQUEST_STATS_FAILURE,
    CHOOSE_REQUEST_START_DATE,
    CHOOSE_REQUEST_END_DATE,
    SHOW_REQUEST_DATEPICKER_IOS,
    SELECT_REQUEST_DATE_IOS,
} = require('../../constants/actionTypes').default;

const initialState = new RequestStatsInitialState;

export default function requestStatsReducer(state = initialState, action) {
    if (!(state instanceof RequestStatsInitialState)) return initialState.merge(state);

    switch (action.type) {
        case REQUEST_STATS_REQUEST: {
            let nextState = state.set('isFetching', true).set('error', null);
            return nextState;
        }
        case REQUEST_STATS_SUCCESS: {
            // const {jsonData} = state;
            // let origin = jsonData.toJS();
            let data = [...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('jsonData', List(data));
                // .set('pageCount', newPageCount)
            return nextState;
        }
        case REQUEST_STATS_FAILURE: {
            let nextState = state.set('isFetching', false)
                .set('error', action.playload);
            return nextState;
        }
        case CHOOSE_REQUEST_START_DATE: {
            let nextState = state.set('startDay', action.payload)
                .set('isShowDatePickerIOS', false);
            return nextState;
        }
        case CHOOSE_REQUEST_END_DATE: {
            let nextState = state.set('endDay', action.payload)
                .set('isShowDatePickerIOS', false);
            return nextState;
        }
        case SHOW_REQUEST_DATEPICKER_IOS: {
            let nextState = state.set('isShowDatePickerIOS', action.isShowDatePickerIOS)
                .set('isStartDate', action.isStartDate);
            return nextState;
        }
        case SELECT_REQUEST_DATE_IOS: {
            let nextState = action.isStartDate ? state.set('startDay', action.selectedDate) : state.set('endDay', action.selectedDate);
            return nextState;
        }


    }

    return state;
}