shareDetailActions.js 5.68 KB
'use strict';

import ReactNative from 'react-native';
import Service from '../../services/ShareDetailService';
const Platform = require('Platform');

const {
    SHARE_DETAIL_REQUEST,
    SHARE_DETAIL_SUCCESS,
    SHARE_DETAIL_FAILURE,
    ADD_FAVORITE_REQUEST,
    ADD_FAVORITE_SUCCESS,
    ADD_FAVORITE_FAILURE,
    FAVORITE_INFO_REQUEST,
    FAVORITE_INFO_FAILURE,

    SHARE_CODE_INFO_REQUEST,
    SHARE_CODE_INFO_SUCCESS,
    SHARE_CODE_INFO_FAILURE,

    PRODUCT_LIST_REQUEST,
    PRODUCT_LIST_SUCCESS,
    PRODUCT_LIST_FAILURE,

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

export function fetchShareCodeInfoRequest() {
    return {
        type: SHARE_CODE_INFO_REQUEST
    }
}

export function fetchShareCodeInfoSuccess(json) {
    return {
        type: SHARE_CODE_INFO_SUCCESS,
        payload: json
    }
}

export function fetchShareCodeInfoFailue(error) {
    return {
        type: SHARE_CODE_INFO_FAILURE,
        payload: error
    }
}

export function fetchShareCodeInfo(skn) {
    return async (dispatch, getState) => {
        let { app: { host } } = getState();
        let getShareCodeInfo = (unionType) => {
            dispatch(fetchShareCodeInfoRequest());
            return new Service(host).fetchShareCodeInfo(skn,unionType)
                .then(json => {
                    dispatch(fetchShareCodeInfoSuccess(json));
                })
                .catch(error => {
                    dispatch(fetchShareCodeInfoFailue(error));
                });
        };

        ReactNative.NativeModules.YH_CommonHelper.unionType()
      		.then(unionType => {
                  getShareCodeInfo(unionType)
      		})
      		.catch(error => {
                  getShareCodeInfo()
      		});
    }
}

export function shareDetailRequest() {
    return {
        type: SHARE_DETAIL_REQUEST
    }
}

export function shareDetailSuccess(json) {
    return {
        type: SHARE_DETAIL_SUCCESS,
        payload: json
    }
}

export function shareDetailFailue(error) {
    return {
        type: SHARE_DETAIL_FAILURE,
        payload: error
    }
}

export function fetchShareDetail(params) {
    return async (dispatch, getState) => {
        let { app: { host } } = getState();
        dispatch(shareDetailRequest());
        return new Service(host).fetchShareDetail(params)
        .then(json => {
          dispatch(shareDetailSuccess(json));
        })
        .catch(error => {
          dispatch(shareDetailFailue(error));
        });
    }
}

export function addFavoriteRequest() {
    return {
        type: ADD_FAVORITE_REQUEST,
    }
}

export function addFavoriteSuccess(json) {
    return {
        type: ADD_FAVORITE_SUCCESS,
        payload: json
    }
}

export function addFavoriteFailure(e) {
    return {
        type: ADD_FAVORITE_FAILURE,
        payload: e
    }
}

export function addFavorite(params) {
    return async (dispatch, getState) => {
        let { app: { host }, shareDetail } = getState();
        let uid, data;
        let isCollect = shareDetail.get('isCollect');
        isCollect === 'Y' && (params.fav_id = params.id)
        let service = new Service(host);
        dispatch(addFavoriteRequest());
        try {
            uid = await ReactNative.NativeModules.YH_CommonHelper.uid();
            params = {uid, ...params};
            if (isCollect === 'Y') {
                data = await service.cancelFavorite(params);
            } else {
                data = await service.addFavorite(params);
            }
        } catch(e) {
            dispatch(addFavoriteFailure(e));
        }
        dispatch(addFavoriteSuccess(isCollect === 'Y' ? 'N' : 'Y'));
    }
}

export function favoriteInfoRequest() {
    return {
        type: FAVORITE_INFO_REQUEST,
    }
}

export function favoriteInfoFailure(e) {
    return {
        type: FAVORITE_INFO_FAILURE,
        payload: e,
    }
}

export function fetchFavoriteInfo(params) {
    return async (dispatch, getState) => {
        let { app: { host } } = getState();
        let uid, data;
        let service = new Service(host);
        dispatch(favoriteInfoRequest());
        try {
            uid = await ReactNative.NativeModules.YH_CommonHelper.uid();
            params = {
                method: 'app.favorite.isFavoriteNew',
                uid,
                ...params
            };
            data = await service.fetchInfo(params);
        } catch(e) {
            dispatch(favoriteInfoFailure(e));
        }
        dispatch(addFavoriteSuccess(data ? 'Y' : 'N'));
    }
}

export function productListRequest() {
    return {
        type: PRODUCT_LIST_REQUEST,
    };
}

export function productListSuccess(json) {
    return {
        type: PRODUCT_LIST_SUCCESS,
        payload: json
    }
}

export function productListFailure(error) {
    return {
        type: PRODUCT_LIST_FAILURE,
        payload: error
    }
}

export function getProductList(reload=false,product_skn) {
	return (dispatch, getState) => {
		let {app, shareDetail} = getState();
		let {productList} = shareDetail;
    if (reload) {

		} else {
			if (productList.isFetching || productList.endReached || productList.error) {
				return;
			}
		}

		let page = productList.page + 1;
		let size = productList.pageSize;
    let fromPage = Platform.OS === 'android' ? 'aFP_AllianceProductDetail' : 'iFP_AllianceProductDetail';

		dispatch(productListRequest());
    return new Service(app.host).fetchProductList(page, size,fromPage,product_skn)
    .then(json => {
			json.endReached = json.page == json.page_total;
			if (json.page > 1) {
				let oldList = productList.product_list.toJS();
				let product_list = [...oldList, ...json.product_list];
				json.product_list = product_list;
			}
			dispatch(productListSuccess(json));
		})
		.catch(error => {
			dispatch(productListFailure(error));
		});
	};
}