|
|
|
import { create } from 'zustand';
|
|
|
|
import { ordersApi } from '../services/api/orders';
|
|
|
|
import { AddressDataItem, CartShippingFeeData, ShippingFeeData,DomesticShippingFeeData } from '../services/api/orders';
|
|
|
|
|
|
|
|
interface PreviewShippingState {
|
|
|
|
freightForwarderAddress: AddressDataItem | null;
|
|
|
|
shippingFees: CartShippingFeeData | null;
|
|
|
|
domesticShippingFees: DomesticShippingFeeData | null;
|
|
|
|
isLoading: boolean;
|
|
|
|
error: string | null;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface PreviewShippingStore {
|
|
|
|
// State
|
|
|
|
state: PreviewShippingState;
|
|
|
|
|
|
|
|
// 获取货代地址
|
|
|
|
fetchFreightForwarderAddress: (transportMode: number | null) => Promise<void>;
|
|
|
|
// 计算物流价格
|
|
|
|
calculateShippingFee: (data: ShippingFeeData) => Promise<void>;
|
|
|
|
// 计算国内物流价格
|
|
|
|
calculateDomesticShippingFee: (data: ShippingFeeData) => Promise<void>;
|
|
|
|
// 重置状态
|
|
|
|
resetState: () => void;
|
|
|
|
// 清空运费数据
|
|
|
|
clearShippingFees: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const usePreviewShippingStore = create<PreviewShippingStore>((set) => ({
|
|
|
|
state: {
|
|
|
|
freightForwarderAddress: null,
|
|
|
|
shippingFees: null,
|
|
|
|
domesticShippingFees: null,
|
|
|
|
isLoading: false,
|
|
|
|
error: null,
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchFreightForwarderAddress: async (transportMode: number | null) => {
|
|
|
|
set((state) => ({
|
|
|
|
state: { ...state.state, isLoading: true, error: null }
|
|
|
|
}));
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await ordersApi.freightForwarderAddress(transportMode);
|
|
|
|
if (response.current_country_address != null){
|
|
|
|
response.other_addresses.unshift(response.current_country_address);
|
|
|
|
}
|
|
|
|
set((state) => ({
|
|
|
|
state: {
|
|
|
|
...state.state,
|
|
|
|
freightForwarderAddress: response,
|
|
|
|
isLoading: false
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
} catch (error) {
|
|
|
|
set((state) => ({
|
|
|
|
state: {
|
|
|
|
...state.state,
|
|
|
|
error: error instanceof Error ? error.message : 'Failed to fetch freight forwarder address',
|
|
|
|
isLoading: false
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
calculateShippingFee: async (data: ShippingFeeData) => {
|
|
|
|
set((state) => ({
|
|
|
|
state: { ...state.state, isLoading: false, error: null }
|
|
|
|
}));
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await ordersApi.calcShippingFee(data);
|
|
|
|
|
|
|
|
set((state) => ({
|
|
|
|
state: {
|
|
|
|
...state.state,
|
|
|
|
shippingFees: response,
|
|
|
|
isLoading: false
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
} catch (error) {
|
|
|
|
set((state) => ({
|
|
|
|
state: {
|
|
|
|
...state.state,
|
|
|
|
error: error instanceof Error ? error.message : 'Failed to calculate shipping fee',
|
|
|
|
isLoading: false
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
calculateDomesticShippingFee: async (data: ShippingFeeData) => {
|
|
|
|
set((state) => ({
|
|
|
|
state: { ...state.state, isLoading: false, error: null }
|
|
|
|
}));
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await ordersApi.calcDomesticShippingFee(data);
|
|
|
|
set((state) => ({
|
|
|
|
state: {
|
|
|
|
...state.state,
|
|
|
|
domesticShippingFees: response,
|
|
|
|
isLoading: false
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
} catch (error) {
|
|
|
|
set((state) => ({
|
|
|
|
state: {
|
|
|
|
...state.state,
|
|
|
|
error: error instanceof Error ? error.message : 'Failed to calculate domestic shipping fee',
|
|
|
|
isLoading: false
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
resetState: () => {
|
|
|
|
set({
|
|
|
|
state: {
|
|
|
|
freightForwarderAddress: null,
|
|
|
|
shippingFees: null,
|
|
|
|
domesticShippingFees: null,
|
|
|
|
isLoading: false,
|
|
|
|
error: null,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
clearShippingFees: () => {
|
|
|
|
set((state) => ({
|
|
|
|
state: {
|
|
|
|
...state.state,
|
|
|
|
shippingFees: null,
|
|
|
|
domesticShippingFees: null,
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
export default usePreviewShippingStore;
|