allianceReducer.js 7.39 KB
'use strict';

import InitialState from './allianceInitialState';
import Immutable from 'immutable';

const {
    SHOW_HELP_DIALOG,
    DISMISS_HELP_DIALOG,

    SHOW_WITHDRAW_DIALOG,
    DISMISS_WITHDRAW_DIALOG,

    SET_ORDER_CODE,

    SETTLEMENT_INFO_REQUEST,
    SETTLEMENT_INFO_SUCCESS,
    SETTLEMENT_INFO_FAILURE,

    ADD_SETTLEMENT_REQUEST,
    ADD_SETTLEMENT_SUCCESS,
    ADD_SETTLEMENT_FAILURE,

    RECENTLY_ORDER_REQUEST,
    RECENTLY_ORDER_SUCCESS,
    RECENTLY_ORDER_FAILURE,

    ORDER_DETAIL_REQUEST,
    ORDER_DETAIL_SUCCESS,
    ORDER_DETAIL_FAILURE,

    SETTLEMENT_RECORD_REQUEST,
    SETTLEMENT_RECORD_SUCCESS,
    SETTLEMENT_RECORD_FAILURE,

    ORDER_LIST_REQUEST,
    ORDER_LIST_SUCCESS,
    ORDER_LIST_FAILURE,

    TOP_LIST_REQUEST,
    TOP_LIST_SUCCESS,
    TOP_LIST_FAILURE,

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

const initialState = new InitialState;

export default function couponReducer(state = initialState, action) {
    if (!(state instanceof InitialState)) {
        return initialState.merge(state);
    }

    switch (action.type) {
        case SHOW_HELP_DIALOG: {
            return state.set('showHelpDialog', true);
        }
        case DISMISS_HELP_DIALOG: {
            return state.set('showHelpDialog', false);
        }
        case SHOW_WITHDRAW_DIALOG: {
            return state.set('showWithdrawalDialog', true);
        }
        case DISMISS_WITHDRAW_DIALOG: {
            return state.set('showWithdrawalDialog', false);
        }

        case SET_ORDER_CODE: {
            return state.set('orderCode', action.payload);
        }

        case SETTLEMENT_INFO_REQUEST: {
            return state.setIn(['settlementInfo', 'isFetching'], true)
                .setIn(['settlementInfo', 'error'], null);
        }

        case SETTLEMENT_INFO_SUCCESS: {
            return state.setIn(['settlementInfo', 'isFetching'], false)
                .setIn(['settlementInfo', 'settlementInfoData'], action.payload)
                .setIn(['settlementInfo', 'error'], null);
        }

        case SETTLEMENT_INFO_FAILURE: {
            return state.setIn(['settlementInfo', 'isFetching'], false)
                .setIn(['settlementInfo', 'error'], action.payload);
        }

        case  ADD_SETTLEMENT_REQUEST: {
            return state.setIn(['addSettlement', 'isFetching'], false)
                .setIn(['addSettlement', 'error'], action.payload);
        }

        case ADD_SETTLEMENT_SUCCESS: {
            return state.setIn(['addSettlement', 'isFetching'], true)
                .setIn(['addSettlement', 'error'], null);
        }

        case ADD_SETTLEMENT_FAILURE: {
            return state.setIn(['addSettlement', 'isFetching'], false)
                .set('showWithdrawalDialog', action.payload.isShow)
                .setIn(['addSettlement', 'error'], null);
        }

        case RECENTLY_ORDER_REQUEST: {
            return state.setIn(['recentlyOrder', 'isFetching'], true)
                .setIn(['recentlyOrder', 'error'], null);
        }

        case RECENTLY_ORDER_SUCCESS: {
            return state.setIn(['recentlyOrder', 'isFetching'], false)
                .setIn(['recentlyOrder', 'recentlyOrder_list'], Immutable.fromJS(action.payload))
                .setIn(['recentlyOrder', 'error'], null);
        }

        case RECENTLY_ORDER_FAILURE: {
            return state.setIn(['recentlyOrder', 'isFetching'], false)
                .setIn(['recentlyOrder', 'error'], action.payload);
        }

        case ORDER_DETAIL_REQUEST: {
            return state.setIn(['orderDetail', 'isFetching'], true)
                .setIn(['orderDetail', 'error'], null);
        }

        case ORDER_DETAIL_SUCCESS: {
            let {
                amountStr,
                orderCode,
                orderStatus,
                orderTimeStr,
                lastOrderAmountStr,
                productList,
            } = action.payload;
            return state.setIn(['orderDetail', 'isFetching'], false)
                .setIn(['orderDetail', 'amountStr'], amountStr)
                .setIn(['orderDetail', 'orderCode'], orderCode)
                .setIn(['orderDetail', 'orderStatus'], orderStatus)
                .setIn(['orderDetail', 'orderTimeStr'], orderTimeStr)
                .setIn(['orderDetail', 'lastOrderAmountStr'], lastOrderAmountStr)
                .setIn(['orderDetail', 'productList'], Immutable.fromJS(productList))
                .setIn(['orderDetail', 'error'], null);
        }

        case ORDER_DETAIL_FAILURE: {
            return state.setIn(['orderDetail', 'isFetching'], false)
                .setIn(['orderDetail', 'error'], action.payload);
        }

        case SETTLEMENT_RECORD_REQUEST: {
            return state.setIn(['settlementRecordList', 'isFetching'], true)
                .setIn(['settlementRecordList', 'error'], null);
        }

        case SETTLEMENT_RECORD_SUCCESS: {
            let {
                page,
                totalPage,
                endReached,
                list,
            } = action.payload;
            return state.setIn(['settlementRecordList', 'isFetching'], false)
                .setIn(['settlementRecordList', 'page'], page)
                .setIn(['settlementRecordList', 'totalPage'], totalPage)
                .setIn(['settlementRecordList', 'endReached'], endReached)
                .setIn(['settlementRecordList', 'list'], Immutable.fromJS(list))
                .setIn(['settlementRecordList', 'error'], null);
        }

        case SETTLEMENT_RECORD_FAILURE: {
            return state.setIn(['settlementRecordList', 'isFetching'], false)
                .setIn(['settlementRecordList', 'error'], action.payload);
        }

        case ORDER_LIST_REQUEST: {
            return state.setIn(['orderList', 'isFetching'], true)
                .setIn(['orderList', 'error'], null);

        }

        case ORDER_LIST_SUCCESS: {
            let {
                page,
                totalPage,
                endReached,
                list,
            } = action.payload;
            return state.setIn(['orderList', 'isFetching'], false)
                .setIn(['orderList', 'page'], page)
                .setIn(['orderList', 'totalPage'], totalPage)
                .setIn(['orderList', 'endReached'], endReached)
                .setIn(['orderList', 'list'], Immutable.fromJS(list))
                .setIn(['orderList', 'error'], null);
        }

        case ORDER_LIST_FAILURE: {
            return state.setIn(['orderList', 'isFetching'], false)
                .setIn(['orderList', 'error'], action.payload);
        }

        case TOP_LIST_REQUEST: {
            return state.setIn(['topList', 'isFetching'], true)
                .setIn(['topList', 'error'], null);
        }

        case TOP_LIST_SUCCESS: {
            let {
                page,
                page_total,
                endReached,
                product_list,
            } = action.payload;
            return state.setIn(['topList', 'isFetching'], false)
                .setIn(['topList', 'page'], page)
                .setIn(['topList', 'page_total'], page_total)
                .setIn(['topList', 'endReached'], endReached)
                .setIn(['topList', 'product_list'], Immutable.fromJS(product_list))
                .setIn(['topList', 'error'], null);
        }

        case TOP_LIST_FAILURE: {
            return state.setIn(['topList', 'isFetching'], false)
                .setIn(['topList', 'error'], action.payload);
        }
    }

    return state;
}