detailActions.js 6.36 KB
'use strict';

import ReactNative, {NativeModules} from 'react-native'
import DetailService from '../../services/DetailService';
import Immutable, {Map} from 'immutable';

const {
    SET_PRODUCT_SKN,

    GET_DETAIL_REQUEST,
    GET_DETAIL_SUCCESS,
    GET_DETAIL_FAILURE,
    GET_SIMILAR_REQUEST,
    GET_SIMILAR_SUCCESS,
    GET_SIMILAR_FAILURE,
    SET_FAVORITE_STATUS,
    SET_DETAIL_PRAISE,
    SET_DETAIL_TOTAL_PRAISE,
    DETAIL_LIKE_REQUEST,
    DETAIL_LIKE_SUCCESS
} = require('../../constants/actionTypes').default;

export function likeRequest() {
    return {type: DETAIL_LIKE_REQUEST}
}

export function likeSuccess(json) {
    return {type: DETAIL_LIKE_SUCCESS, payload: json}
}

export function onPressPraise() {
    return (dispatch, getState) => {
        let {app, detail} = getState();

        let forLike = detail.get('is_praised') == 'N';
        let product_skn = detail.get('product_skn');


        let likeStatus = forLike
            ? 'Y'
            : 'N';

        let likeOpt = (forLike, product_skn, uid) => {

            let likeCount = detail.get('total_praised');
            if (forLike) {
                likeCount = likeCount + 1;
            } else {
                likeCount = likeCount - 1 < 0? 0: likeCount -1;
            }
            dispatch(likeSuccess({likeStatus, likeCount}));
            NativeModules.YH_CommonHelper.goodGoodsRecommendDetailPraiseStatusChanged({likeStatus, likeCount});
            let param = {
                PRD_SKN: product_skn,
                LIKE_TYPE: forLike? 1: 2,
                POS_ID: 102,
            }
            NativeModules.YH_CommonHelper.logEvent('YB_GOOD_GDS_LIKE_C', param);

            return new DetailService(app.host).commitLike(forLike, product_skn, uid).then(json => {

            }).catch(error => {

            });
        };

        let uid = 0;
        ReactNative.NativeModules.YH_CommonHelper.uid().then(data => {
            uid = data;
            likeOpt(forLike, product_skn, uid);
        }).catch(error => {
            ReactNative.NativeModules.YH_CommonHelper.login().then(data => {
                uid = data;
                likeOpt(forLike, product_skn, uid);
            }).catch(error => {
            });
        });
    }
}

export function setDetailTotalPraise(total_praised) {
    return {type: SET_DETAIL_TOTAL_PRAISE, payload: total_praised};
}

export function setProductSKN(product_skn) {
    return {type: SET_PRODUCT_SKN, payload: product_skn};
}

export function setDetailPraise(is_praised) {
    return {type: SET_DETAIL_PRAISE, payload: is_praised};
}

export function getDetail() {
    return (dispatch, getState) => {
        let {app, detail} = getState();
        let {product_skn} = detail;
        dispatch(getDetailRequest());
        return new DetailService(app.host).getDetailWithProductSKN(product_skn).then(json => {
            dispatch(getDetailSuccess(json));
            return new DetailService(app.singleHost).getFavoriteStatusWithProductID(json.product_id).then(json => {
                dispatch(setFavoriteStatus(json));
                let param = {
                    PRD_SKN: product_skn + '',
                    AB_TYPE: 'A'
                }
                NativeModules.YH_CommonHelper.logEvent('YB_GOOD_GDS_DETAIL_L', param);
            }).catch(error => {
                dispatch(setFavoriteStatus(false));
            })
        }).catch(error => {
            dispatch(getDetailFailure(error));
        });
    };
}

export function changeFavoriteStatus(favorite) {
    return (dispatch, getState) => {
        let {app, detail} = getState();
        let data = detail.get('product').get('data');
        if (!data) {
            return;
        }
        let product_id = data.get('product_id', 0);
        let favRequest = (product_id, favorite) => {
            if (product_id && favorite) {
                new DetailService(app.host).cancelFavorite(product_id);
            }
            if (product_id && !favorite) {
                new DetailService(app.host).addFavorite(product_id);
            }
            let param = {
                PRD_SKN: detail.product_skn,
                AB_TYPE: 'A',
                COL_TYPE: favorite
                    ? '2'
                    : '1'
            }
            NativeModules.YH_CommonHelper.logEvent('YB_GOOD_GDS_COLLECT_C', param);
            dispatch(setFavoriteStatus(!favorite));
        }
        ReactNative.NativeModules.YH_CommonHelper.uid().then(uid => {
            favRequest(product_id, favorite);
        }).catch(error => {
            ReactNative.NativeModules.YH_CommonHelper.login().then(uid => {
                favRequest(product_id, favorite);
            }).catch(error => {});
        });
    }
}

export function setFavoriteStatus(favorite) {
    return {type: SET_FAVORITE_STATUS, payload: favorite};
}

export function getDetailRequest() {
    return {type: GET_DETAIL_REQUEST};
}

export function getDetailSuccess(json) {
    return {type: GET_DETAIL_SUCCESS, payload: json};
}

export function getDetailFailure(error) {
    return {type: GET_DETAIL_FAILURE, payload: error};
}

export function getSimilar() {
    return (dispatch, getState) => {
        let {app, detail, list} = getState();
        let {product_skn} = detail;
        dispatch(getSimilarRequest());
        return new DetailService(app.host).getSimilarListWithSKN(product_skn).then(json => {

            let tempAry = [];
            for (var i = 0; i < json.product_list.length; i++) {
                let {product_skn} = json.product_list[i];
                if (product_skn) {
                    tempAry.push(product_skn + '');
                }
            }
            let similarPrd = tempAry.join(',');
            let similarParam = {
                AB_TYPE: 'A',
                MAIN_PRD: product_skn + '',
                MAIN_INDEX: list.selected_index,
                SIMILAR_PRD: similarPrd,
                POS_ID: '100013'
            }
            NativeModules.YH_CommonHelper.logEvent('YB_SIMILAR_GDS_L', similarParam);
            dispatch(getSimilarSuccess(json.product_list));
        }).catch(error => {
            dispatch(getSimilarFailure(error));
        });
    };
}

export function getSimilarRequest() {
    return {type: GET_SIMILAR_REQUEST};
}

export function getSimilarSuccess(json) {
    return {type: GET_SIMILAR_SUCCESS, payload: json};
}

export function getSimilarFailure(error) {
    return {type: GET_SIMILAR_FAILURE, payload: error};
}