haggleReducer.js 6.33 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,
    PRODUCT_UFO_LIST_SUCCESS,
    PRODUCT_UFO_LIST_REQUEST,
    PRODUCT_UFO_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 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 PRODUCT_UFO_LIST_REQUEST: {
            return state.setIn(['productUFOList', 'isFetching'], true)
                .setIn(['productUFOList', 'error'], null);
        }

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

        case PRODUCT_UFO_LIST_FAILURE: {
            return state.setIn(['productUFOList', 'isFetching'], false)
                .setIn(['productUFOList', 'error'], action.payload)
                .setIn(['productUFOList', '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)
                    .setIn(['productUFOList', 'page'], 0)
                    .setIn(['productUFOList', 'page_total'], 0)
                    .setIn(['productUFOList', 'endReached'], false)
                    .setIn(['productUFOList', 'error'], null)
                    .setIn(['productUFOList', 'isFetching'], false)
                    .setIn(['productUFOList', '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;
}