mirror of
https://github.com/perfect-panel/ppanel-web.git
synced 2026-02-14 12:21:10 -05:00
🐛 fix(order): Preserve last successful order on error during order creation
This commit is contained in:
parent
634be371b1
commit
2fb98be591
@ -13,7 +13,7 @@ import { Separator } from '@workspace/ui/components/separator';
|
|||||||
import { LoaderCircle } from 'lucide-react';
|
import { LoaderCircle } from 'lucide-react';
|
||||||
import { useTranslations } from 'next-intl';
|
import { useTranslations } from 'next-intl';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { useCallback, useEffect, useState, useTransition } from 'react';
|
import { useCallback, useEffect, useRef, useState, useTransition } from 'react';
|
||||||
import { SubscribeBilling } from './billing';
|
import { SubscribeBilling } from './billing';
|
||||||
import { SubscribeDetail } from './detail';
|
import { SubscribeDetail } from './detail';
|
||||||
|
|
||||||
@ -33,16 +33,28 @@ export default function Purchase({ subscribe, setSubscribe }: Readonly<PurchaseP
|
|||||||
coupon: '',
|
coupon: '',
|
||||||
});
|
});
|
||||||
const [loading, startTransition] = useTransition();
|
const [loading, startTransition] = useTransition();
|
||||||
|
const lastSuccessOrderRef = useRef<any>(null);
|
||||||
|
|
||||||
const { data: order } = useQuery({
|
const { data: order } = useQuery({
|
||||||
enabled: !!subscribe?.id,
|
enabled: !!subscribe?.id,
|
||||||
queryKey: ['preCreateOrder', params],
|
queryKey: ['preCreateOrder', params],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const { data } = await preCreateOrder({
|
try {
|
||||||
...params,
|
const { data } = await preCreateOrder({
|
||||||
subscribe_id: subscribe?.id as number,
|
...params,
|
||||||
} as API.PurchaseOrderRequest);
|
subscribe_id: subscribe?.id as number,
|
||||||
return data.data;
|
} as API.PurchaseOrderRequest);
|
||||||
|
const result = data.data;
|
||||||
|
if (result) {
|
||||||
|
lastSuccessOrderRef.current = result;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
if (lastSuccessOrderRef.current) {
|
||||||
|
return lastSuccessOrderRef.current;
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -19,7 +19,7 @@ import { Separator } from '@workspace/ui/components/separator';
|
|||||||
import { LoaderCircle } from 'lucide-react';
|
import { LoaderCircle } from 'lucide-react';
|
||||||
import { useTranslations } from 'next-intl';
|
import { useTranslations } from 'next-intl';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { useCallback, useEffect, useState, useTransition } from 'react';
|
import { useCallback, useEffect, useRef, useState, useTransition } from 'react';
|
||||||
import { SubscribeBilling } from './billing';
|
import { SubscribeBilling } from './billing';
|
||||||
import { SubscribeDetail } from './detail';
|
import { SubscribeDetail } from './detail';
|
||||||
|
|
||||||
@ -40,16 +40,28 @@ export default function Renewal({ id, subscribe }: Readonly<RenewalProps>) {
|
|||||||
user_subscribe_id: id,
|
user_subscribe_id: id,
|
||||||
});
|
});
|
||||||
const [loading, startTransition] = useTransition();
|
const [loading, startTransition] = useTransition();
|
||||||
|
const lastSuccessOrderRef = useRef<any>(null);
|
||||||
|
|
||||||
const { data: order } = useQuery({
|
const { data: order } = useQuery({
|
||||||
enabled: !!subscribe.id && open,
|
enabled: !!subscribe.id && open,
|
||||||
queryKey: ['preCreateOrder', params],
|
queryKey: ['preCreateOrder', params],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const { data } = await preCreateOrder({
|
try {
|
||||||
...params,
|
const { data } = await preCreateOrder({
|
||||||
subscribe_id: subscribe.id,
|
...params,
|
||||||
} as API.PurchaseOrderRequest);
|
subscribe_id: subscribe.id,
|
||||||
return data.data;
|
} as API.PurchaseOrderRequest);
|
||||||
|
const result = data.data;
|
||||||
|
// 请求成功时保存数据
|
||||||
|
if (result) {
|
||||||
|
lastSuccessOrderRef.current = result;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
if (lastSuccessOrderRef.current) {
|
||||||
|
return lastSuccessOrderRef.current;
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user