detailActions.js 1.82 KB
'use strict';

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

const {
	SET_ORDER_CODE,

	GET_DETAIL_REQUEST,
    GET_DETAIL_SUCCESS,
    GET_DETAIL_FAILURE,

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

export function setOrderCode(order_code) {
	return {
        type: SET_ORDER_CODE,
        payload: order_code,
    };
}

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

		dispatch(getDetailRequest());
		return new DetailService(app.host).getDetailWithOrderCode(order_code)
			.then(json => {
				//Get first product skn.
				if (json.order_goods && json.order_goods.length) {
					json.firstProductSKN = json.order_goods[0].product_skn;
				}
				//Get express type.
				let type = 2;
				if (json.express_detail && json.express_detail.length) {
					let {accept_address, acceptTime} = json.express_detail[0];
					type = acceptTime && accept_address ? 0 : 2;
					json.acceptTime = acceptTime;
					json.accept_address = accept_address;
				} else if (json.express_number && json.express_number.length) {
					type = 1;
				}
				json.expressType = type;
				if (json.express_company) {
					let {caption} = json.express_company;
					json.expressName = caption;
				}

				// console.log(product_skn);
				// console.log(json);
				dispatch(getDetailSuccess(json));
			})
			.catch(error => {
				dispatch(getDetailFailure(error));
			});
	};
}

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