homeActions.js 4.15 KB
'use strict';

import ReactNative from 'react-native';
import HomeService from '../../services/HomeService';
import store from 'react-native-simple-store';

const {
	SET_CHANNEL,

	GET_GLOBAL_HOME_RESOURCE_REQUEST,
	GET_GLOBAL_HOME_RESOURCE_SUCCESS,
	GET_GLOBAL_HOME_RESOURCE_FAILURE,

	GET_GLOBAL_PRODUCT_LIST_REQUEST,
	GET_GLOBAL_PRODUCT_LIST_SUCCESS,
	GET_GLOBAL_PRODUCT_LIST_FAILURE,

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


export function getGlobalHomeResourceRequest(reload) {
    return {
        type: GET_GLOBAL_HOME_RESOURCE_REQUEST,
		payload: reload
    }
}

export function getGlobalHomeResourceSuccess(json) {
    return {
        type: GET_GLOBAL_HOME_RESOURCE_SUCCESS,
        payload: json
    }
}

export function getGlobalHomeResourceFailure(error) {
    return {
        type: GET_GLOBAL_HOME_RESOURCE_FAILURE,
        payload: error
    }
}
export function getGlobalHomeResource(reload=false) {
	return (dispatch, getState) => {
		let {app, home} = getState();

		dispatch(getGlobalHomeResourceRequest(reload));
        return new HomeService(app.serviceHost).getGlobalHomeResource()
        .then(json => {
			let data = parseProductList(json)
			dispatch(getGlobalHomeResourceSuccess(data));
			let info = data.groupInfo;
			dispatch(getGlobalProductList(home.groupInfo.fliter,data.groupInfo[home.groupInfo.fliter]));
		})
		.catch(error => {
			dispatch(getGlobalHomeResourceFailure(error));
		});
	};
}

function parseProductList(json) {

	let groupInfo = {};
	let list = [];
	let page = json.page;
	let content_code = json.content_code;
	let total = json.total;
	let total_page = json.total_page;

	let list_0 = json.list;
	for (var i = 0; i < list_0.length; i++) {
		list.push(list_0[i]);
		if (list_0[i].template_name == 'paramsGroup') {
			groupInfo = list_0[i].data.list;
		}
	}

	return {
		list,
		page,
		total,
		total_page,
		content_code,
		groupInfo,
	};
}

export function getGlobalProductListRequest(json) {
    return {
        type: GET_GLOBAL_PRODUCT_LIST_REQUEST,
		payload: json,
    }
}

export function getGlobalProductListSuccess(json) {
    return {
        type: GET_GLOBAL_PRODUCT_LIST_SUCCESS,
        payload: json
    }
}

export function getGlobalProductListFailure(error) {
    return {
        type: GET_GLOBAL_PRODUCT_LIST_FAILURE,
        payload: error
    }
}
export function getGlobalProductList(index,json,reload=false) {
	return (dispatch, getState) => {
		let {app, home} = getState();

		if (json.isFetching || json.endReached) {
			return;
		}

		let page_0 = json.page?json.page:1;
		let page = reload?parseInt(page_0)+1:parseInt(page_0);
		let order = json.order;
		let sort = json.msort?json.msort:'';
		
		if (json) {
			json.page = page;
			json.isFetching = true;
			json.filter = index;
			json.endReached = false;
		}
		dispatch(getGlobalProductListRequest(json));
        return new HomeService(app.host).getGlobalProductList(page,order,sort)
        .then(json => {
			let data = parseDetailProductList(index,home,json);
			dispatch(getGlobalProductListSuccess(data));
		})
		.catch(error => {
			dispatch(getGlobalProductListFailure(error));
		});
	};
}

function parseDetailProductList(index,home,json) {

	let page = json.page;
	let total = json.total;
	let product_list = json.product_list;
	let page_total = json.page_total;
	let isFetching = false;
	let error = '';
	let filter = index;

	let list = home.groupInfo.list.toJS();

	for (var i = 0; i < list.length; i++) {
		if (i == filter) {
			if (page > 1) {
				let oldList =list[i].product;
			 	product_list = [...oldList, ...product_list];
			}
			list[i].product = product_list;
			list[i].page = page;
			list[i].isFetching = isFetching;
			list[i].error = error;
			list[i].total = total;
			list[i].page_total = page_total;
			list[i].filter = parseInt(filter);
			list[i].endReached = page == page_total;
		}
	}
	return list;
}

export function setFilter(value) {
    return {
        type: SET_FLITER,
		payload: value,
    }
}

export function onPressFilter(value) {
	return (dispatch, getState) => {
		let {app, home} = getState();
		dispatch(setFilter(value));
		let data = home.toJS();
		let list = data.groupInfo.list;
		dispatch(getGlobalProductList(value,list[value]));
	};
}