yohoCoinActions.js 7.37 KB
'use strict';

import ReactNative from 'react-native';
import YohoCoinService from '../../services/YohoCoinService';

const {
	GET_YOHO_COIN_NUM_REQUEST,
	GET_YOHO_COIN_NUM_SUCCESS,
	GET_YOHO_COIN_NUM_FAILURE,

	GET_BANNER_REQUEST,
	GET_BANNER_SUCCESS,
	GET_BANNER_FAILURE,

	GET_WEIXIN_REQUEST,
	GET_WEIXIN_SUCCESS,
	GET_WEIXIN_FAILURE,

	BOY_GIRL_FAVORITE_REQUEST,
	BOY_GIRL_FAVORITE_SUCCESS,
	BOY_GIRL_FAVORITE_FAILURE,

	KIDS_FAVORITE_REQUEST,
	KIDS_FAVORITE_SUCCESS,
	KIDS_FAVORITE_FAILURE,

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



export function getYohoCoinNumRequest() {
    return {
        type: GET_YOHO_COIN_NUM_REQUEST,
    };
}

export function getYohoCoinNumSuccess(json) {
    return {
        type: GET_YOHO_COIN_NUM_SUCCESS,
        payload: json
    }
}

export function getYohoCoinNumFailure(error) {
    return {
        type: GET_YOHO_COIN_NUM_FAILURE,
        payload: error
    }
}

export function getYohoCoinNum() {
	return (dispatch, getState) => {
		let {app, yohoCoin} = getState();

		let fetchYohoCoinNum = (uid) => {
			dispatch(getYohoCoinNumRequest());
			return new YohoCoinService(app.host).getYohoCoinNum(uid)
			.then(json => {
				dispatch(getYohoCoinNumSuccess(json));
			})
			.catch(error => {
				dispatch(getYohoCoinNumFailure(error));
			});
		}

		ReactNative.NativeModules.YH_CommonHelper.uid()
		.then(uid => {
			fetchYohoCoinNum(uid);
		})
		.catch(error => {
			fetchYohoCoinNum(0);
		});
	};
}


export function getBannerRequest() {
    return {
        type: GET_BANNER_REQUEST,
    };
}

export function getBannerSuccess(json) {
    return {
        type: GET_BANNER_SUCCESS,
        payload: json
    }
}

export function getBannerFailure(error) {
    return {
        type: GET_BANNER_FAILURE,
        payload: error
    }
}

export function getBanner() {
	return (dispatch, getState) => {
		let {app, yohoCoin} = getState();

		dispatch(getBannerRequest());
		return new YohoCoinService(app.serviceHost).getBanner()
		.then(json => {
			let payload = parseBanner(json);
			dispatch(getBannerSuccess(payload));
		})
		.catch(error => {
			dispatch(getBannerFailure(error));
		});
	};
}

function parseBanner (json) {
	let dic = json&&json.length>0?json[0]:null;
	let list = dic?dic.data:null;
	return list;
}


function getWeixinRequest() {
	return {
        type: GET_WEIXIN_REQUEST,
    };
}

function getWeixinSuccess(json) {
    return {
        type: GET_WEIXIN_SUCCESS,
        payload: json
    };
}

function getWeixinFailure(error) {
    return {
        type: GET_WEIXIN_FAILURE,
        payload: error
    };
}

export function getWeixin() {
	return (dispatch, getState) => {
		let {app, yohoCoin} = getState();
        let {weixin} = yohoCoin;

		if (weixin.isFetching) {
			return;
		}

		dispatch(getWeixinRequest());
		return new YohoCoinService(app.host).getWeixin()
			.then(json => {
				dispatch(getWeixinSuccess(json));
			})
			.catch(error => {
				dispatch(getWeixinFailure(error));
			});
	};
}

export function fetchFavoriteList() {
	return (dispatch, getState) => {
		let {app, yohoCoin} = getState();
		let channel = app.channel;
		if (channel == '1' || channel == '2' || channel == '4' ) {
			dispatch(fetchBoyGirlFavoriteList());
		}else {
			dispatch(fetchKidsFavoriteList());
		}
	}
}

export function fetchFavoriteRequest() {
    return {
        type: BOY_GIRL_FAVORITE_REQUEST,
    };
}

export function fetchFavoriteSuccess(json) {
    return {
        type: BOY_GIRL_FAVORITE_SUCCESS,
        payload: json,
    };
}

export function fetchFavoriteFailure(error) {
    return {
        type: BOY_GIRL_FAVORITE_FAILURE,
        payload: error,
    };
}

export function fetchBoyGirlFavoriteList() {
	return (dispatch, getState) => {
		let {app, yohoCoin} = getState();
		let channel = app.channel;
		let currentChannelData = yohoCoin.favorite?yohoCoin.favorite.toJS():null;

		if (currentChannelData.isFetching
			|| currentChannelData.endReached
			|| (currentChannelData.list.size == 0)) {
			return;
		}

		dispatch(fetchFavoriteRequest());

		let page = currentChannelData.currentPage + 1;

		let fetchFavorite = (contentCode, gender, fromPage) => {
			return new YohoCoinService(app.host).fetchFavoriteData(channel, contentCode, gender, fromPage, page)
			.then(json =>{
				let payload = parseBoyGirlFavorite(json);

				if (payload.currentPage > 1) {
					let oldList = currentChannelData.list;
					let newList = [...oldList, ...payload.list];
					payload.list = newList;
				}
				payload.endReached = payload.currentPage == payload.pageCount;
				dispatch(fetchFavoriteSuccess(payload));
			})
			.catch(error => {
				dispatch(fetchFavoriteFailure(error));
			});
		}

		let sourcePage = '';
		let contentCode = '';
		let gender = '';

		Promise.all([
			ReactNative.NativeModules.YH_CommonHelper.sourcePage('YH_HomeViewController'),
			ReactNative.NativeModules.YH_CommonHelper.currentHomeContentCode(),
			ReactNative.NativeModules.YH_CommonHelper.currentGender(),
		]).then(result => {
			sourcePage = result[0];
			contentCode = result[1];
			gender = result[2];
			fetchFavorite(contentCode, gender, sourcePage);
		})
		.catch(error => {
			fetchFavorite(contentCode, gender, sourcePage);
		});
	}
}

export function fetchKidsFavoriteRequest() {
    return {
        type: KIDS_FAVORITE_REQUEST,
    };
}

export function fetchKidsFavoriteSuccess(json) {
    return {
        type: KIDS_FAVORITE_SUCCESS,
        payload: json
    };
}

export function fetchKidsFavoriteFailure(error) {
    return {
        type: KIDS_FAVORITE_FAILURE,
        payload: error
    };
}

export function fetchKidsFavoriteList() {
	return (dispatch, getState) => {
		let {app, yohoCoin} = getState();

		let currentChannelData = yohoCoin.favorite?yohoCoin.favorite.toJS():null;

		if (currentChannelData.isFetching
			|| currentChannelData.endReached
			|| (currentChannelData.list.size == 0)) {
			return;
		}

		let page = currentChannelData.currentPage + 1;

		dispatch(fetchKidsFavoriteRequest());

		let fetchFavorite = (fromPage) => {
			return new YohoCoinService(app.host).fetchKidFavoriteData(fromPage, page)
			.then(json =>{
				let payload = parseKidsFavorite(json);

				if (payload.currentPage > 1) {
					let oldList = currentChannelData.list;
					let newList = [...oldList, ...payload.list];
					payload.list = newList;
				}
				payload.endReached = payload.currentPage == payload.pageCount;
				dispatch(fetchKidsFavoriteSuccess(payload));
			})
			.catch(error =>{
				dispatch(fetchKidsFavoriteFailure(error));
			});
		}

		let sourcePage = '';
		Promise.all([
			ReactNative.NativeModules.YH_CommonHelper.sourcePage('YH_HomeViewController'),
		]).then(result => {
			sourcePage = result[0];
			fetchFavorite(sourcePage);
		})
		.catch(error => {
			fetchFavorite(sourcePage);
		});
	}
}

function parseBoyGirlFavorite(json) {
	let currentPage = json && json.page ? json.page : 1;
	let pageCount = json && json.page_total ? json.page_total : 0;
	let total = json && json.total ? json.total : 0;

	let list = json && json.product_list ? json.product_list : [];
    let rec_id = json && json.rec_id ? json.rec_id : '';

	return {
		currentPage,
		pageCount,
		total,
		list,
        rec_id
	}
}

function parseKidsFavorite(json) {
	let currentPage = json && json.page ? json.page : 1;
	let pageCount = json && json.page_total ? json.page_total : 0;
	let total = json && json.total ? json.total : 0;

	let list = json && json.product_list ? json.product_list : [];

	return {
		currentPage,
		pageCount,
		total,
		list
	}
}