shareDetailActions.js 3.09 KB
'use strict';

import ReactNative from 'react-native';
import Service from '../../services/ShareDetailService';

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,

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

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'));
    }
}