groupPurchaseDetailReducer.js 4.33 KB
'use strict';

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

const {
    SET_GROUP_NO,
    SET_ACTIVITY_ID,
    PRODUCT_LIST_REQUEST,
    PRODUCT_LIST_SUCCESS,
    PRODUCT_LIST_FAILURE,
    RESOURCEINFO_REQUEST,
    RESOURCEINFO_SUCCESS,
    RESOURCEINFO_FAILURE,
    ACTIVITY_GROUP_DETAIL_REQUEST,
    ACTIVITY_GROUP_DETAIL_SUCCESS,
    ACTIVITY_GROUP_DETAIL_FAILURE,
    UPDATE_LEFTTIME,
    SHOWSHARVIEW,
    SHOWSNAPSHOOTSHARE,
} = 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 SHOWSHARVIEW: {
            return state.set('showShareView', action.payload);
        }
        case SHOWSNAPSHOOTSHARE: {
            return state.set('showSnapshootShare', action.payload);
        }
        case SET_ACTIVITY_ID: {
            return state.set('activityId', action.payload);
        }
        case SET_GROUP_NO: {
            return state.set('groupNo', action.payload);
        }
        case PRODUCT_LIST_REQUEST: {
          return state.setIn(['productList', 'isFetching'], true)
  			         .setIn(['productList', 'error'], null);
        }
        case PRODUCT_LIST_SUCCESS: {
          let {
    				collageProductVoList,
    				page,
    				page_total,
    				total,
            endReached,
    			} = action.payload;

          return state.setIn(['productList', 'page'], page)
          .setIn(['productList', 'pagetotal'], page_total)
          .setIn(['productList', 'total'], total)
          .setIn(['productList', 'list'], Immutable.fromJS(collageProductVoList))
          .setIn(['productList', 'isFetching'], false)
          .setIn(['productList', 'error'], null)
          .setIn(['productList', 'endReached'], endReached);
        }
        case PRODUCT_LIST_FAILURE: {
          return state.setIn(['productList', 'isFetching'], false)
                .setIn(['productList', 'error'], action.payload);
        }

        case RESOURCEINFO_REQUEST: {
          return state.setIn(['resourceInfo', 'isFetching'], true)
                .setIn(['resourceInfo', 'error'], null);
        }
        case RESOURCEINFO_SUCCESS: {
          return state.setIn(['resourceInfo', 'isFetching'], false)
                .setIn(['resourceInfo', 'resourceList'], Immutable.fromJS(action.payload))
                .setIn(['resourceInfo', 'error'], null);
        }
        case RESOURCEINFO_FAILURE: {
          return state.setIn(['resourceInfo', 'isFetching'], false)
                .setIn(['resourceInfo', 'error'], action.payload);
        }

        case ACTIVITY_GROUP_DETAIL_REQUEST: {
          return state.setIn(['groupDetail', 'isFetching'], true)
                .setIn(['groupDetail', 'error'], null);
        }
        case ACTIVITY_GROUP_DETAIL_SUCCESS: {

          let {
            groupId,
            leftTime,
            isNewCustomer,
            pageGo,
            groupRole,
            lackNum,
            openerJoinItem,
            yourJoinItem,
            membershipItems,
            joinLimit,
    			} = action.payload;

          return state.setIn(['groupDetail', 'isFetching'], false)
                .setIn(['groupDetail', 'error'], null)
                .setIn(['groupDetail', 'groupId'], groupId)
                .setIn(['groupDetail', 'leftTime'], leftTime)
                .setIn(['groupDetail', 'isNewCustomer'], isNewCustomer)
                .setIn(['groupDetail', 'pageGo'], pageGo)
                .setIn(['groupDetail', 'groupRole'], groupRole)
                .setIn(['groupDetail', 'lackNum'], lackNum)
                .setIn(['groupDetail', 'openerJoinItem'], Immutable.fromJS(openerJoinItem))
                .setIn(['groupDetail', 'yourJoinItem'], Immutable.fromJS(yourJoinItem))
                .setIn(['groupDetail', 'membershipItems'], Immutable.fromJS(membershipItems))
                .setIn(['groupDetail', 'joinLimit'], joinLimit);
        }
        case ACTIVITY_GROUP_DETAIL_FAILURE: {
          return state.setIn(['groupDetail', 'isFetching'], false)
                .setIn(['groupDetail', 'error'], action.payload);
        }
        case UPDATE_LEFTTIME: {
          state.setIn(['groupDetail', 'leftTime'], action.payload);
        }
    }

    return state;
}