outletActions.js 5.86 KB
'use strict';

import ReactNative from 'react-native';
import OutletService from '../../services/OutletService';
import * as _ from 'lodash';
import {GetQueryString} from '../../utils/ResourceParser';

const {
	SET_PLATFORM,

	SET_ACTIVITY_FLITER,

	SET_FLITER,

	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;

export function onPressCoupon() {
	return {
		type: SET_PLATFORM,
		payload: true
	};
}

export function getCategoryRequest() {
    return {
        type: GET_CATEGORY_REQUEST,
    };
}

export function getCategorySuccess(json) {
    return {
        type: GET_CATEGORY_SUCCESS,
        payload: json
    }
}

export function getCategoryFailure(error) {
    return {
        type: GET_CATEGORY_FAILURE,
        payload: error
    }
}

export function getCategory() {
	return (dispatch, getState) => {
		let {app, outlet} = getState();
        let parent_id = app.categoryId;
		dispatch(getCategoryRequest());
		return new OutletService(app.serviceHost).getCategory(parent_id)
        .then(json => {
			let params = parseListFromCategory(json);
			dispatch(getCategorySuccess(params));
		})
		.catch(error => {
			dispatch(getCategoryFailure(error));
		});
	};
}

function parseListFromCategory(json) {

	json.map((item, i) => {
		let url = item.sort_url;
		let content_code = GetQueryString(url,'content_code');
		if (!content_code) {
			content_code = item.content_code;
		}
		item.content_code = content_code;
	})
	return json;
}

export function getOutletHomeResourceRequest(content_code) {
    return {
        type: GET_OUTLETHOMERESOURCE_REQUEST,
		payload: content_code
    };
}

export function getOutletHomeResourceSuccess(json) {
    return {
        type: GET_OUTLETHOMERESOURCE_SUCCESS,
        payload: json
    }
}

export function getOutletHomeResourceFailure(content_code,error) {
    return {
        type: GET_OUTLETHOMERESOURCE_FAILURE,
        payload: {'error':error,'content_code':content_code}
    }
}

export function getOutletHomeResource(content_code,ptr) {
	return (dispatch, getState) => {
		let {app, outlet} = getState();
		let {categoryList} = outlet;
		dispatch(getOutletHomeResourceRequest({'ptr':ptr,'content_code':content_code}));
		return new OutletService(app.serviceHost).getOutletHomeResource(content_code)
        .then(json => {
			dispatch(getOutletHomeResourceSuccess({'json':json,'content_code':content_code}));
			let categoryNavigationItem;
			if (json.list) {
				json.list.map((item, i) => {
					 let template_name = item.template_name;
					 if (template_name == 'categoryNavigation') {
						 categoryNavigationItem = item.data[0];
					 }
				});
			}
			dispatch(getProductList(content_code,categoryNavigationItem));
		})
		.catch(error => {
			dispatch(getOutletHomeResourceFailure(content_code,error));
		});
	};
}

export function getProductListRequest(json) {
    return {
        type: GET_PRODUCT_REQUEST,
		payload: json
    };
}

export function getProductListSuccess(json) {
    return {
        type: GET_PRODUCT_SUCCESS,
        payload: json
    }
}

export function getProductListFailure(content_code,error) {
    return {
        type: GET_PRODUCT_FAILURE,
        payload: {'error':error,'content_code':content_code,'categoryNavigationItem':categoryNavigationItem}
    }
}

export function getProductList(content_code,categoryNavigationItem) {
	return (dispatch, getState) => {
		let {app, outlet} = getState();
		let {categoryList} = outlet;
		//limit,page,uid,firstProductSkn=''
		// if (productList.isFetching || productList.endReached || productList.error) {
		// 	return;
		// }

		let param = parseUrlForparam(categoryNavigationItem.url);
		let page = 1;//productList.currentPage + 1;
		let pageSize = 60;//productList.pageSize;

		dispatch(getProductListRequest({'content_code':content_code,'categoryNavigationItem':categoryNavigationItem}));
		return new OutletService(app.host).getProductList(pageSize,page,param)
        .then(json => {
			dispatch(getProductListSuccess({'json':json,'content_code':content_code,'categoryNavigationItem':categoryNavigationItem}));
		})
		.catch(error => {
			dispatch(getProductListFailure(content_code,error,categoryNavigationItem));
		});
	};
}

function parseUrlForparam(url) {
	let part = GetQueryString(url,'openby:yohobuy');
	let param =	JSON.parse(part).params;
	return param;
}

export function getOutletActivityListRequest(content_code) {
    return {
        type: GET_OUTLET_ACTIVITY_LIST_REQUEST,
		payload: content_code
    };
}

export function getOutletActivityListSuccess(json) {
    return {
        type: GET_OUTLET_ACTIVITY_LIST_SUCCESS,
        payload: json
    }
}

export function getOutletActivityListFailure(content_code,error) {
    return {
        type: GET_OUTLET_ACTIVITY_LIST_FAILURE,
        payload: {'error':error,'content_code':content_code}
    }
}

export function getOutletActivityList(content_code) {
	return (dispatch, getState) => {
		let {app, outlet} = getState();
		let {categoryList} = outlet;
		dispatch(getOutletActivityListRequest(content_code));
		return new OutletService(app.host).getOutletActivityList(app.yh_channel)
        .then(json => {
			dispatch(getOutletActivityListSuccess({'json':json,'content_code':content_code}));
		})
		.catch(error => {
			dispatch(getOutletActivityListFailure(content_code,error));
		});
	};
}


export function setActivityFliter(content_code,activityMore) {
    return {
        type: SET_ACTIVITY_FLITER,
		payload: {'activityMore':activityMore,'content_code':content_code}
    };
}

export function onPressFilter(content_code,value) {
    return {
        type: SET_FLITER,
		payload: {'value':value,'content_code':content_code}
    };
}