You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.3 KiB

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;
}
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);
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,
}
});
}
}));
export default usePreviewShippingStore;