detailActions.js 2.94 KB
'use strict';

import ReactNative 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,
} = require('../../constants/actionTypes').default;

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

export function getDetail() {
	return (dispatch, getState) => {
		let {app, detail} = getState();
		let {product_skn} = detail;
		// if (!product_skn) {
		// 	return ;
		// }
		dispatch(getDetailRequest());
		return new DetailService(app.host).getDetailWithProductSKN(product_skn)
			.then(json => {
				// console.log(product_skn);
				// console.log(json);
				dispatch(getDetailSuccess(json));
				return new DetailService(app.singleHost).getFavoriteStatusWithProductID(json.product_id)
				.then(json => {
					dispatch(setFavoriteStatus(json));
				})
				.catch(error => {
					dispatch(setFavoriteStatus(false));
				})
			})
			.catch(error => {
				dispatch(getDetailFailure(error));
			});
	};
}

export function changeFavoriteStatus(favorite) {
	return (dispatch, getState) => {
		let {app, detail} = getState();
		let product_id = detail.get('product').get('data').get('product_id');
		if (product_id && favorite) {
			new DetailService(app.host).cancelFavorite(product_id);
		}
		if (product_id && !favorite) {
			new DetailService(app.host).addFavorite(product_id);
		}
		dispatch(setFavoriteStatus(!favorite));
	}
}


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} = getState();
		let {product_skn} = detail;
		// if (!product_skn) {
		// 	return ;
		// }
		dispatch(getSimilarRequest());
		return new DetailService(app.host).getSimilarListWithSKN(product_skn)
			.then(json => {
				// console.log(product_skn);
				// console.log(json);
				dispatch(getSimilarSuccess(json.product_list));
			})
			.catch(error => {
				// console.log(product_skn);
				// console.log(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
    };
}