shareDetailReducer.js 3.02 KB
'use strict';

import InitialState from './shareDetailInitialState';
import Immutable, {List} from "immutable";

const {
    SHARE_DETAIL_REQUEST,
    SHARE_DETAIL_SUCCESS,
    SHARE_DETAIL_FAILURE,
    ADD_FAVORITE_REQUEST,
    ADD_FAVORITE_SUCCESS,
    ADD_FAVORITE_FAILURE,
    FAVORITE_INFO_REQUEST,
    FAVORITE_INFO_FAILURE,

    SHARE_CODE_INFO_REQUEST,
    SHARE_CODE_INFO_SUCCESS,
    SHARE_CODE_INFO_FAILURE,


    PRODUCT_LIST_REQUEST,
    PRODUCT_LIST_SUCCESS,
    PRODUCT_LIST_FAILURE,

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

const initialState = new InitialState;

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

    switch (action.type) {
        case SHARE_DETAIL_REQUEST:
            return state.set('isFetching', true);
        case SHARE_DETAIL_SUCCESS:
            return state.set('isFetching', false)
            .set('productInfo', Immutable.fromJS(action.payload));
        case SHARE_DETAIL_FAILURE:
            return state.set('isFetching', false)
                    .set('error', action.payload);
        case ADD_FAVORITE_REQUEST:
            return state.set('isFetching', true);
        case ADD_FAVORITE_SUCCESS:
            return state.set('isFetching', false)
            .set('isCollect', action.payload);
        case ADD_FAVORITE_FAILURE:
            return state.set('isFetching', false)
            .set('error', action.payload);
        case ADD_FAVORITE_FAILURE:
            return state.set('isFetching', false)
            .set('error', action.payload);
        case ADD_FAVORITE_FAILURE:
            return state.set('isFetching', false)
            .set('error', action.payload);
        case SHARE_CODE_INFO_REQUEST:
            return state.set('isFetching', true);
        case SHARE_CODE_INFO_SUCCESS:
            return state.set('isFetching', false)
            .set('shareCodeInfo', Immutable.fromJS(action.payload));
        case SHARE_CODE_INFO_FAILURE:
            return state.set('isFetching', false)
            .set('error', action.payload);
        case PRODUCT_LIST_REQUEST: {
          return state.setIn(['productList', 'isFetching'], true)
  			         .setIn(['productList', 'error'], null);
        }
        case PRODUCT_LIST_SUCCESS: {
          let {
    				product_list,
    				page,
    				page_total,
    				total,
            endReached,
    			} = action.payload;

          return state.setIn(['productList', 'page'], page)
          .setIn(['productList', 'pagetotal'], page_total)
          .setIn(['productList', 'total'], total)
          .setIn(['productList', 'product_list'], Immutable.fromJS(product_list))
          .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);
        }
    }

    return state;
}