61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
|
|
import { usePaymentRouteFlow } from "@/app/_hooks/use-payment-route-flow";
|
|
import { useAppNavigator } from "@/router/use-app-navigator";
|
|
import type { PayChannel } from "@/data/dto/payment";
|
|
import type { SubscriptionReturnTo } from "@/lib/navigation/subscription_exit";
|
|
|
|
import type { SubscriptionType } from "./subscription-screen.helpers";
|
|
|
|
export interface UseSubscriptionPaymentFlowInput {
|
|
subscriptionType: SubscriptionType;
|
|
shouldResumePendingOrder: boolean;
|
|
returnTo: SubscriptionReturnTo;
|
|
initialPayChannel: PayChannel;
|
|
}
|
|
|
|
export function useSubscriptionPaymentFlow({
|
|
subscriptionType,
|
|
shouldResumePendingOrder,
|
|
returnTo,
|
|
initialPayChannel,
|
|
}: UseSubscriptionPaymentFlowInput) {
|
|
const navigator = useAppNavigator();
|
|
const { payment, paymentDispatch } = usePaymentRouteFlow({
|
|
initialPayChannel,
|
|
paymentType: subscriptionType,
|
|
shouldResumePendingOrder,
|
|
});
|
|
const successDialogShownOrderRef = useRef<string | null>(null);
|
|
const [showPaymentSuccessDialog, setShowPaymentSuccessDialog] =
|
|
useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!payment.isPaid || !payment.currentOrderId) return;
|
|
if (successDialogShownOrderRef.current === payment.currentOrderId) return;
|
|
|
|
successDialogShownOrderRef.current = payment.currentOrderId;
|
|
setShowPaymentSuccessDialog(true);
|
|
}, [payment.currentOrderId, payment.isPaid]);
|
|
|
|
const handleBackClick = () => {
|
|
navigator.exitSubscription(returnTo);
|
|
};
|
|
|
|
const handlePaymentSuccessClose = () => {
|
|
setShowPaymentSuccessDialog(false);
|
|
paymentDispatch({ type: "PaymentReset" });
|
|
navigator.exitSubscriptionAfterSuccess(returnTo);
|
|
};
|
|
|
|
return {
|
|
payment,
|
|
paymentDispatch,
|
|
showPaymentSuccessDialog,
|
|
handleBackClick,
|
|
handlePaymentSuccessClose,
|
|
};
|
|
}
|