outletReducer.js 7.56 KB
'use strict';

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

const {
	SET_ACTIVITY_FLITER,
	SET_FLITER,
	SHOW_TOAST,
	HIDE_TOAST,
	GET_CATEGORY_REQUEST,
	GET_CATEGORY_SUCCESS,
	GET_CATEGORY_FAILURE,

	GET_OUTLETHOMERESOURCE_REQUEST,
	GET_OUTLETHOMERESOURCE_SUCCESS,
	GET_OUTLETHOMERESOURCE_FAILURE,

	GET_PRODUCT_REQUEST,
	GET_PRODUCT_SUCCESS,
	GET_PRODUCT_FAILURE,

	GET_OUTLET_ACTIVITY_LIST_REQUEST,
	GET_OUTLET_ACTIVITY_LIST_SUCCESS,
	GET_OUTLET_ACTIVITY_LIST_FAILURE,

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

const initialState = new InitialState;

export default function outletReducer(state=initialState, action) {
    switch(action.type) {

		case GET_CATEGORY_REQUEST: {
			return state.setIn(['categoryList', 'isFetching'], true);
		}
		case GET_CATEGORY_SUCCESS: {
			return state.setIn(['categoryList', 'list'], Immutable.fromJS(action.payload))
			.setIn(['categoryList', 'isFetching'], false);
		}
		case GET_CATEGORY_FAILURE: {
			return state.setIn(['categoryList', 'isFetching'], false)
			.setIn(['categoryList', 'error'], action.payload);
		}
		case GET_OUTLETHOMERESOURCE_REQUEST: {

			let categoryList = state.categoryList.toJS();
			let content_code1 = action.payload.content_code;
		    categoryList.list.map((item, i) => {
			   	 let content_code0 = item.content_code;
			   	 if (content_code1 == content_code0) {
			   		 item.isFetching = true;
			   		 item.error = '';
					 item.ptr = action.payload.ptr;
			   	 }
		    })
			return state.setIn(['categoryList', 'list'], Immutable.fromJS(categoryList.list))
		}
		case GET_OUTLETHOMERESOURCE_SUCCESS: {
			let categoryNavigationList;

			if (action.payload.json.list) {
				action.payload.json.list.map((item, i) => {
	                 let template_name = item.template_name;
	                 if (template_name == 'categoryNavigation') {
	                     categoryNavigationList = item.data;
	                 }
	            });
			}

			let categoryList = state.categoryList.toJS();
			let content_code1 = action.payload.content_code;
		    categoryList.list.map((item, i) => {
			   	 let content_code0 = item.content_code;
			   	 if (content_code1 == content_code0) {
			   		 item.honeResource = action.payload.json;
					 item.categoryNavigationList = categoryNavigationList;
			   		 item.isFetching = false;
			   		 item.error = '';
					 item.ptr = false;
					 item.Navfliter = 0;
			   	 }
		    })
			return state.setIn(['categoryList', 'list'], Immutable.fromJS(categoryList.list))
		}
		case GET_OUTLETHOMERESOURCE_FAILURE: {
			let categoryList = state.categoryList.toJS();
			let content_code1 = action.payload.content_code;
			let error = action.payload.error;
		    categoryList.list.map((item, i) => {
			   	 let content_code0 = item.content_code;
			   	 if (content_code1 == content_code0) {
			   		 item.isFetching = false;
			   		 item.error = error;
					 item.ptr = false;
			   	 }
		    })
			return state.setIn(['categoryList', 'list'], Immutable.fromJS(categoryList.list))
		}
		case GET_PRODUCT_REQUEST: {
			let categoryList = state.categoryList.toJS();
			let content_code1 = action.payload.content_code;
			let categoryNavigationItem = action.payload.categoryNavigationItem;

			categoryList.list.map((item, i) => {
				 let content_code0 = item.content_code;
				 if (content_code1 == content_code0) {
					 item.categoryNavigationList.map((item0, i) => {
						 let categoryName = item0.categoryName;
						 if (categoryNavigationItem.categoryName == categoryName) {
							 item0.isFetching = true;
					   		 item0.error = null;
						 }
					});
				 }
			});

			return state.setIn(['categoryList', 'list'], Immutable.fromJS(categoryList.list))
		}
		case GET_PRODUCT_SUCCESS: {
			let categoryList = state.categoryList.toJS();
			let content_code1 = action.payload.content_code;
			let categoryNavigationItem = action.payload.categoryNavigationItem;

			categoryList.list.map((item, i) => {
				 let content_code0 = item.content_code;
				 if (content_code1 == content_code0) {
					 item.categoryNavigationList.map((item0, i) => {
		 				 let categoryName = item0.categoryName;
		 				 if (categoryNavigationItem.categoryName == categoryName) {
		 					 item0.list = action.payload.json.list;
							 item0.isFetching = false;
							 item0.currentPage = action.payload.json.currentPage;
							 item0.endReached = action.payload.json.endReached;
							 item0.pageCount = action.payload.json.pageCount;
							 item0.total = action.payload.json.total;
		 				 }
		 			});
				 }
			});

			return state.setIn(['categoryList', 'list'], Immutable.fromJS(categoryList.list))
		}
		case GET_PRODUCT_FAILURE: {
			let categoryList = state.categoryList.toJS();
			let content_code1 = action.payload.content_code;
			let categoryNavigationItem = action.payload.categoryNavigationItem;
			let error = action.payload.error;

			categoryList.list.map((item, i) => {
				 let content_code0 = item.content_code;
				 if (content_code1 == content_code0) {
					 item.categoryNavigationList.map((item0, i) => {
						 let categoryName = item0.categoryName;
						 if (categoryNavigationItem.categoryName == categoryName) {
							 item0.isFetching = false;
							 item0.error = error;
						 }
					});
				 }
			});

			return state.setIn(['categoryList', 'list'], Immutable.fromJS(categoryList.list))
		}
		case GET_OUTLET_ACTIVITY_LIST_REQUEST: {
			let categoryList = state.categoryList.toJS();
			let content_code1 = action.payload;
			categoryList.list.map((item, i) => {
				 let content_code0 = item.content_code;
				 if (content_code1 == content_code0) {
					 item.activityFetching = true;
				 }
			})
			return state.setIn(['categoryList', 'list'], Immutable.fromJS(categoryList.list))
		}
		case GET_OUTLET_ACTIVITY_LIST_SUCCESS: {
			let categoryList = state.categoryList.toJS();
			let content_code1 = action.payload.content_code;
			categoryList.list.map((item, i) => {
				 let content_code0 = item.content_code;
				 if (content_code1 == content_code0) {
					 item.activityList = action.payload.json;
					 item.activityMore = false;
					 item.activityFetching = false;
				 }
			})
			return state.setIn(['categoryList', 'list'], Immutable.fromJS(categoryList.list))
		}
		case GET_OUTLET_ACTIVITY_LIST_FAILURE: {
			let categoryList = state.categoryList.toJS();
			let content_code1 = action.payload.content_code;
			categoryList.list.map((item, i) => {
				 let content_code0 = item.content_code;
				 if (content_code1 == content_code0) {
					 item.activityFetching = false;
					 item.error = action.payload.error;
				 }
			})
			return state.setIn(['categoryList', 'list'], Immutable.fromJS(categoryList.list))
		}
		case SET_ACTIVITY_FLITER: {
			let categoryList = state.categoryList.toJS();
			let content_code1 = action.payload.content_code;
			categoryList.list.map((item, i) => {
				 let content_code0 = item.content_code;
				 if (content_code1 == content_code0) {
					 item.activityMore = action.payload.activityMore;
				 }
			})
			return state.setIn(['categoryList', 'list'], Immutable.fromJS(categoryList.list))
		}
		case SET_FLITER: {
			let categoryList = state.categoryList.toJS();
			let content_code1 = action.payload.content_code;
			categoryList.list.map((item, i) => {
				 let content_code0 = item.content_code;
				 if (content_code1 == content_code0) {
					 item.Navfliter = action.payload.value;
				 }
			})
			return state.setIn(['categoryList', 'list'], Immutable.fromJS(categoryList.list))
		}
		case SHOW_TOAST:
			return state.set('isShowToast', true).set('toastMessage', action.payload)
		case HIDE_TOAST:
			return state.set('isShowToast', false).set('toastMessage', '')
    }

    return state;
}