/* eslint-disable consistent-return */ /* eslint-disable no-nested-ternary */ import { useRouter } from "next/navigation"; import { useEffectOnce } from "usehooks-ts"; import { DialogContent } from "@/components/ui/dialog"; import LIcon from "@/components/ui/lucid-icon/lucid-icon"; import Spinner from "@/components/ui/spinner"; import { useLocale } from "@/hooks/useLocale"; import { useBookStrategyMutation, usePayCCMutation, } from "@/services/eva-booking/payment/payment.service"; import type { I3DPollVariables } from "@/services/eva-booking/payment/payment-service.types"; interface PaymentBookingModalContentProps { data: I3DPollVariables & { reservationNote?: string }; onBookSuccess: (payload: { bookingCode: string }) => void; onBookFail: (payload: { message: string; data?: any }) => void; } const PaymentBookingModalContent = ({ ...props }: PaymentBookingModalContentProps) => { const payCCMutation = usePayCCMutation(); const bookMutation = useBookStrategyMutation(); const router = useRouter(); const { t } = useLocale(); async function payCC() { const payCCResponse = await payCCMutation.mutateAsync(props.data); if (!payCCResponse.success && payCCResponse.errors?.length > 0) { return props.onBookFail({ message: payCCResponse.errors[0]?.message ?? "Ödeme sırasında bir hata oluştu!", }); } if ( payCCResponse.success && (!payCCResponse.errors || payCCResponse.errors.length === 0) && payCCResponse.result ) { try { const response = await bookMutation.mutateAsync({ BaseCartProductRequest: { cartId: props.data.cartId, searchId: props.data.searchId, transactionKey: props.data.transactionKey, reservationNote: props.data.reservationNote ?? "", }, }); if ( response.result?.bookingCode && response.result?.bookingCode !== "" ) { if (!props.onBookSuccess) { router.push( `/booking-success?code=${encodeURIComponent( response.result?.bookingCode, )}`, ); } else { props.onBookSuccess({ bookingCode: response.result.bookingCode }); } } else { props.onBookFail({ message: "Rezervasyon oluşturulamadı" }); } } catch (error) { props.onBookFail({ message: "Rezervasyon oluşturulamadı" }); } } else { props.onBookFail({ message: "Ödeme sırasında hata oluştu" }); } } useEffectOnce(() => { payCC(); }); const isLoading = payCCMutation.isLoading || bookMutation.isLoading; const isPaymentSuccess = !payCCMutation.isLoading && !(payCCMutation.data?.errors && payCCMutation.data?.errors?.length > 0); // eslint-disable-next-line no-nested-ternary const paymentMessage = payCCMutation.isLoading ? t("The pay process is taking place") : payCCMutation.data?.errors && payCCMutation.data?.errors?.length > 0 ? t("An error occurred during paying") : t("Pay transaction is successful!"); const bookingMessage = bookMutation.isLoading ? "Rezervasyon oluşturuluyor" : bookMutation.data?.result?.bookingCode && bookMutation.data?.result?.bookingCode !== "" ? "Rezervasyon oluşturuldu" : "Rezervasyon oluşturulamadı"; return (
{isLoading && (
)}
{paymentMessage} {isPaymentSuccess && }
{isPaymentSuccess &&
{bookingMessage}
}
); }; export default PaymentBookingModalContent;