couponReducer.js 2.79 KB
'use strict';

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

const {
	SET_FLITER,

	UNUSE_COUPON_LIST_REQUEST,
	UNUSE_COUPON_LIST_SUCCESS,
	UNUSE_COUPON_LIST_FAILURE,

	USED_COUPON_LIST_REQUEST,
	USED_COUPON_LIST_SUCCESS,
	USED_COUPON_LIST_FAILURE,

	SHOW_USED_COUPON_DETAIL,
	SHOW_UNUSED_COUPON_DETAIL,

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

const initialState = new InitialState;

export default function detailReducer(state=initialState, action) {
    switch(action.type) {
    	case UNUSE_COUPON_LIST_REQUEST: {
			return state.setIn(['unuseCoupon', 'isFetching'], true)
				.setIn(['unuseCoupon', 'error'], null);
    	}
		case UNUSE_COUPON_LIST_SUCCESS: {
			return state.setIn(['unuseCoupon', 'isFetching'], false)
				.setIn(['unuseCoupon', 'currentPage'], action.payload.currentPage)
				.setIn(['unuseCoupon', 'pageSize'], action.payload.pageSize)
				.setIn(['unuseCoupon', 'total'], action.payload.total)
				.setIn(['unuseCoupon', 'endReached'], action.payload.endReached)
				.setIn(['unuseCoupon', 'couponList'], Immutable.fromJS(action.payload.couponList))
				.setIn(['unuseCoupon', 'error'], null);
		}
		case UNUSE_COUPON_LIST_FAILURE: {
			return state.setIn(['unuseCoupon', 'isFetching'], false)
				.setIn(['unuseCoupon', 'error'], action.payload);
		}
		case USED_COUPON_LIST_REQUEST: {
			return state.setIn(['usedCoupon', 'isFetching'], true)
				.setIn(['usedCoupon', 'error'], null);
    	}
		case USED_COUPON_LIST_SUCCESS: {
			return state.setIn(['usedCoupon', 'isFetching'], false)
				.setIn(['usedCoupon', 'currentPage'], action.payload.currentPage)
				.setIn(['usedCoupon', 'pageSize'], action.payload.pageSize)
				.setIn(['usedCoupon', 'total'], action.payload.total)
				.setIn(['usedCoupon', 'endReached'], action.payload.endReached)
				.setIn(['usedCoupon', 'couponList'], Immutable.fromJS(action.payload.couponList))
				.setIn(['usedCoupon', 'error'], null);
		}
		case USED_COUPON_LIST_FAILURE: {
			return state.setIn(['usedCoupon', 'isFetching'], false)
				.setIn(['usedCoupon', 'error'], action.payload);
		}
		case SET_FLITER: {
			return state.set('fliter', action.payload);
		}
		case RESET_DATA: {
			return state.setIn(['unuseCoupon', 'isFetching'], false)
				.setIn(['unuseCoupon', 'currentPage'], 0)
				.setIn(['unuseCoupon', 'pageSize'], 10)
				.setIn(['unuseCoupon', 'total'], 0)
				.setIn(['unuseCoupon', 'endReached'], false)
				.setIn(['unuseCoupon', 'couponList'], Immutable.fromJS([]))
				.setIn(['unuseCoupon', 'error'], null);
		}
		case SHOW_USED_COUPON_DETAIL: {
			return state.setIn(['usedCoupon', 'couponList'], Immutable.fromJS(action.payload))
		}
		case SHOW_UNUSED_COUPON_DETAIL: {
			return state.setIn(['unuseCoupon', 'couponList'], Immutable.fromJS(action.payload))
		}

		break;
    }
    return state;
}