-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuseRegistEvent.tsx
More file actions
74 lines (67 loc) · 2.46 KB
/
useRegistEvent.tsx
File metadata and controls
74 lines (67 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { toast } from "sonner";
import { eventsService } from "@/services/events";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useDialog } from "@/contexts";
import EventCheckStatusModal from "../components/EventCheckStatusModal";
import { useRouter } from "@/lib/navigation";
import { useTranslations } from "next-intl";
export const useRegistEvent = () => {
const t = useTranslations("EventsPage.Hook");
const router = useRouter();
const { openDialog, closeDialog } = useDialog();
const queryClient = useQueryClient();
const { mutate: registerEvent, isPending: isPendingRegister } = useMutation({
mutationKey: ["registerEvent"],
mutationFn: ({ event_id }: { event_id: number }) => eventsService.registerEvent(event_id),
onSuccess: (data) => {
toast.success(data?.message);
const orderNo = data?.data?.order_no;
if (orderNo) {
router.push(`/my-events/${orderNo}`);
} else {
router.push("/my-events");
}
},
onError: (error) => {
const message = error?.message || t("register-error");
toast.error(message);
},
});
const { mutate: checkPaymentStatus, isPending: isCheckingPayment } = useMutation({
mutationKey: ["checkPaymentStatus"],
mutationFn: async ({ transaction_no }: { transaction_no: string }) =>
eventsService.checkPaymentStatus(transaction_no),
onSuccess: (data, variables) => {
const paymentStatus = data?.data?.status;
openDialog({
content: (
<EventCheckStatusModal
status={paymentStatus}
transaction_no={data?.data?.transaction_no}
onRefresh={() => {
closeDialog();
setTimeout(() => {
checkPaymentStatus({ transaction_no: variables.transaction_no });
}, 300);
}}
/>
),
size: "sm",
});
queryClient.invalidateQueries({ queryKey: ["getListMyEvents"] });
if (paymentStatus?.toUpperCase() === "SUCCESS") {
setTimeout(() => {
toast.success("Payment verified!", {
description: "Your event registration is now confirmed.",
});
}, 3500);
}
},
onError: () => {
toast.error("Failed to check payment status", {
description: "Please try again later or contact support if the problem persists.",
});
},
});
return { checkPaymentStatus, isCheckingPayment, registerEvent, isPendingRegister };
};