haggleReducer.js 4.51 KB
'use strict';

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

const {
  PRODUCT_LIST_REQUEST,
  PRODUCT_LIST_SUCCESS,
  PRODUCT_LIST_FAILURE,
  MYHAGGLE_PRODUCT_LIST_REQUEST,
  MYHAGGLE_PRODUCT_LIST_SUCCESS,
  MYHAGGLE_PRODUCT_LIST_FAILURE,
  RESOURCE_INFO_REQUEST,
  RESOURCE_INFO_SUCCESS,
  RESOURCE_INFO_FAILURE,
  SET_SELECTED_CATEGORY,
  SHOW_HASCUT_ALERT,
  REFRESH_PRODUCT_LIST,
} = 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 PRODUCT_LIST_REQUEST: {
          return state.setIn(['productList', 'isFetching'], true)
              .setIn(['productList', 'error'], null);
      }

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

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

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

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

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

      case SET_SELECTED_CATEGORY: {
        return state.set('categoryType', action.payload);

      }

      case REFRESH_PRODUCT_LIST: {
        let {
            index,
        } = action.payload;

        if (index === 0) {
          return state.setIn(['productList', 'page'], 0)
            .setIn(['productList', 'page_total'], 0)
            .setIn(['productList', 'endReached'], false)
            .setIn(['productList', 'error'], null)
            .setIn(['productList', 'isFetching'], false)
            .setIn(['productList', 'isPullToRefresh'], true);
          }else {
            return state.setIn(['myHaggleProductList', 'page'], 0)
              .setIn(['myHaggleProductList', 'page_total'], 0)
              .setIn(['myHaggleProductList', 'endReached'], false)
              .setIn(['myHaggleProductList', 'error'], null)
              .setIn(['myHaggleProductList', 'isFetching'], false)
              .setIn(['myHaggleProductList', 'isPullToRefresh'], true);
          }
        }

        break;

      case SHOW_HASCUT_ALERT: {
        return state.set('isShowAlert', true);
      }

      case  RESOURCE_INFO_REQUEST: {
          return state.setIn(['resourceInfo', 'isFetching'], true)
              .setIn(['resourceInfo', 'error'], action.payload);
      }

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

      case RESOURCE_INFO_FAILURE: {
          return state.setIn(['resourceInfo', 'isFetching'], false)
              .setIn(['resourceInfo', 'error'], null);
      }

    }

      return state;
  }