interestActions.js 7.03 KB
'use strict';

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

const YH_RNCacheTypeInterestList = 'YH_RNCacheTypeInterestList';

const {
	SET_TYPE,

	RESET_LIST_PAGE_INFO,
	SHOW_LOGIN_TIP,

	INTEREST_LIST_REQUEST,
	INTEREST_LIST_SUCCESS,
	INTEREST_LIST_FAILURE,

	INTEREST_LIKE_STATE_CHANGE,

    INTEREST_LIKE_REQUEST,
    INTEREST_LIKE_SUCCESS,
    INTEREST_LIKE_FAILURE,
	INTEREST_LIKE_TIP_REMOVE,

    LOAD_CACHED_INTEREST_LIST,

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

export function setType(type) {
    return {
        type: SET_TYPE,
        payload: type
    };
}

export function loadCachedInterestList() {
	return (dispatch, getState) => {
		let {app, interest} = getState();
		if (interest.cachedList.size > 0) {
			return;
		}

		store.get(YH_RNCacheTypeInterestList)
		.then(data => {
			if (!data) {
				return;
			}

			dispatch({
				type: LOAD_CACHED_INTEREST_LIST,
				payload: data,
			});
		})
		.catch(error => {

		});
	};
}

export function shouldShowLoginTip() {
	return (dispatch, getState) => {
		let {app, interest} = getState();
		if (interest.cachedList.size > 0) {
			return;
		}

		ReactNative.NativeModules.YH_CommonHelper.uid()
		.then(data => {
			dispatch(showLoginTip(false));
		})
		.catch(error => {
			dispatch(showLoginTip(true));
		});
	};
}

export function resetListPageInfo() {
	return {
		type: RESET_LIST_PAGE_INFO,
	};
}

export function showLoginTip(show) {
	return {
		type: SHOW_LOGIN_TIP,
		payload: show
	};
}

export function interestListRequest(ptr) {
    return {
        type: INTEREST_LIST_REQUEST,
        payload: ptr
    };
}

export function interestListSuccess(json) {
    return {
        type: INTEREST_LIST_SUCCESS,
        payload: json
    };
}

export function interestListFailure(error) {
    return {
        type: INTEREST_LIST_FAILURE,
        payload: error
    };
}

export function interestListTipRemove() {
	return {
		type: INTEREST_LIKE_TIP_REMOVE,
	}
}

/*
 * reload bool 是否需要强制重新请求数据,
 */
export function interestList(reload=false) {
	return (dispatch, getState) => {
		let {app, interest} = getState();

		if (reload) {
			// 强制刷新数据
			if (interest.ptr && interest.isFetching) {
				return;
			}
		} else {
			if (interest.isFetching || interest.endReached || interest.error) {
				return;
			}
		}

		let fetchList = (gender, uid, page, pageSize, sourcePage) => {
			dispatch(interestListRequest(reload));
			return new InterestService(app.serviceHost).fetchInterestList(gender, uid, page, pageSize, sourcePage)
			.then(json => {
				let payload = parseInterestList(json);
				payload.endReached = payload.currentPage == payload.pageCount;

				if (payload.currentPage > 1) {
					let oldList = interest.list.toJS();
					let list = [...oldList, ...payload.list];
					payload.list = list;
				} else {
					store.save(YH_RNCacheTypeInterestList, payload.list)
					.then(data => {

					})
					.catch(error => {

					});
				}

				dispatch(interestListSuccess(payload));

				dispatch(dataExposure('YB_SHOW_CONCERN', payload.logList));
			})
			.catch(error => {
				dispatch(interestListFailure(error));
			});
		}

		let gender = '';
		let uid = 0;
		let sourcePage = '';
		let page = interest.currentPage + 1;
		let pageSize = interest.pageSize;

		Promise.all([
            ReactNative.NativeModules.YH_CommonHelper.currentGender(),
            ReactNative.NativeModules.YH_CommonHelper.sourcePage('YH_CategoryConcernVC'),
        ]).then(result => {
        	gender = result[0];
        	sourcePage = result[1];

        	return ReactNative.NativeModules.YH_CommonHelper.uid();
        }).then(data => {
			uid = data;
			fetchList(gender, uid, page, pageSize, sourcePage);
		})
		.catch(error => {
			fetchList(gender, uid, page, pageSize, sourcePage);
		});
	};
}

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

	let list = json && json.list ? json.list : [];
	let logList = [];
	list.map((item) => {
		let productList = [];
		let activityList = [];

		item.product && item.product.map((product, i) => {
			let logProduct = {
				I_INDEX: i + 1,
				PRD_ID: product.product_id ? product.product_id : '',
				PRD_IMAGE: product.product_img ? product.product_img : '',
				ACTION_URL: product.url ? product.url : '', 
			};
			productList.push(logProduct);
		});

		if (item.brand_type == 'activity') {
			activityList.push({
				I_INDEX: 1,
				ACT_IMAGE: item.img && item.img.src ? item.img.src : '',
				ACTION_URL: item.img && item.img.url ? item.img.url : '',
			});
		}

		let logItem = {
			BRAND_ID: item.brand_id,
			BRAND_IMG: item.brand_img,
			PRD_LIST: productList,
			ACTIVITY_LIST: activityList,
		};
		logList.push(logItem);
	});

	return {
		list,
		logList,
		currentPage,
		pageCount,
		total,
	};
}

export function interestLikeStateChange(like, index) {
    return {
        type: INTEREST_LIKE_STATE_CHANGE,
        payload: {like, index}
    };
}

export function interestLikeRequest() {
    return {
        type: INTEREST_LIKE_REQUEST,
    };
}

export function interestLikeSuccess(json) {
    return {
        type: INTEREST_LIKE_SUCCESS,
        payload: json
    };
}

export function interestLikeFailure(error) {
    return {
        type: INTEREST_LIKE_FAILURE,
        payload: error
    };
}

export function addFavorite(item, index) {
	return (dispatch, getState) => {
		let {app, interest} = getState();

		let likeOpt = (id, type='brand', uid=0, fromPage='') => {
			dispatch(interestLikeStateChange('Y', index));
			dispatch(interestLikeRequest());
			return new InterestService(app.host).addFavorite(id, type, uid, fromPage)
			.then(json => {
				dispatch(interestLikeSuccess(json));
			})
			.catch(error => {
				dispatch(interestLikeFailure(error));
			});
		};

		let id = item.get('brand_id', '0');
		let uid = 0;
		let sourcePage = '';
		ReactNative.NativeModules.YH_CommonHelper.sourcePage('YH_CategoryConcernVC')
		.then(data => {
			sourcePage = data;
			return ReactNative.NativeModules.YH_CommonHelper.uid();
		})
		.then(data => {
			uid = data;
			likeOpt(id, 'brand', uid, sourcePage);
		})
		.catch(error => {
			ReactNative.NativeModules.YH_CommonHelper.login()
			.then(data => {
				uid = data;
				likeOpt(id, 'brand', uid, sourcePage);
				dispatch(resetListPageInfo());
				dispatch(interestList(true));
			})
			.catch(error => {

			});
		});
	};
}


export function jumpWithUrl(url) {
	if (!url) {
		__DEV__ && console.log('Illegal url');
		return {
	        type: JUMP_WITH_URL
	    }
	}

    ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
    return {
        type: JUMP_WITH_URL,
		payload: url
    };
}

function dataExposure(eventName, data) {
	return (dispatch, getState) => {
		if (!eventName || !data || data.length == 0) {
			return;
		}
		
		let params = {
			DATA: data,
		};

		console.log(eventName)
		console.log(params)

		ReactNative.NativeModules.YH_CommonHelper.logEvent(eventName, params);
	};
}