orderListReducer.js 1.65 KB
'use strict';

import InitialState from './orderListInitialState';
import Immutable, {Map} from 'immutable';

const {
    SET_ERROR,
    SET_TIP_MESSAGE,

    SELECT_ORDER_TYPE_SUCCESS,
    GET_ORDER_LIST_REQUEST,
    GET_ORDER_LIST_FAILURE,
    GET_ORDER_LIST_SUCCESS,

} = require('../../constants/actionTypes').default;

const initialState = new InitialState;

export default function appReducer(state = initialState, action) {

    switch (action.type) {

        case SET_ERROR:
            return state.set('error',action.payload);
        case SET_TIP_MESSAGE:
            return state.set('tipMessage', action.payload);
        case SELECT_ORDER_TYPE_SUCCESS:
            return state.set('currentOrderType', action.payload);
        case GET_ORDER_LIST_REQUEST:
            return state.set('isFetching', true)
                        .set('error', null);
        case GET_ORDER_LIST_FAILURE:
            return state.set('isFetching', false)
                        .set('error', action.payload);
        case GET_ORDER_LIST_SUCCESS:
            let json = action.payload;
            return state.set('isFetching', false)                        
                        .set('error', null)
                        .set('currentOrderList', Immutable.fromJS(json.list))
                        .set('currentOrderListType', json.orderType)
                        .set('currentPage', json.currentPage)
                        .set('pageCount', json.pageCount)
                        .set('total', json.total)
                        .set('endReached', json.endReached)
                        .setIn(["orderList", json.orderType], Immutable.fromJS(json));
    }

    return state;
}