import {
    REQUEST_ACTIVE_ORDERS,
    RECEIVE_ACTIVE_ORDERS,
    REQUEST_HISTORY_ORDERS,
    RECEIVE_HISTORY_ORDERS,
    SET_VIEW_ALL_ORDERS
} from '../../constants/ordersConstants';

const moduleReducers = {};

moduleReducers[REQUEST_ACTIVE_ORDERS] = (state, action) => {
    return Object.assign({}, state, {
        isLoading: action.isLoading
    });
};

moduleReducers[RECEIVE_ACTIVE_ORDERS] = (state, action) => {
    return Object.assign({}, state, {
        activeOrders: action.activeOrders,
        isLoading: action.isLoading
    });
};

moduleReducers[REQUEST_HISTORY_ORDERS] = (state, action) => {
    return Object.assign({}, state, {
        isLoading: action.isLoading
    });
};

moduleReducers[RECEIVE_HISTORY_ORDERS] = (state, action) => {
    return Object.assign({}, state, {
        historyOrders: action.historyOrders
    });
};

moduleReducers[SET_VIEW_ALL_ORDERS] = (state, action) => {
    const oldState = Object.assign({}, state.isViewAllOrdersChecked);
    const newState = Object.assign(oldState, action.isViewAllOrdersChecked);

    return Object.assign({}, state, {
        isViewAllOrdersChecked: newState
    });
};

const ordersReducer = (state = {}, action) => {

    return moduleReducers[action.type] ? moduleReducers[action.type](state, action) : state;
};

export default ordersReducer;
