Feat/SW-1370/Guarantee my stay ancillaries * feat(SW-1370): guarantee for ancillaries * feat(SW-1370): remove console log * feat(SW-1370): add translations * feat(SW-1370): small fix * feat(SW-1370): fix must be guaranteed * feat(SW-1370): fix logic and comments pr * feat(SW-1370): fix comments pr * feat(SW-1370): fix comments pr * feat(SW-1370): add translation * feat(SW-1370): add translation and fix pr comment * feat(SW-1370): fix pr comment * feat(SW-1370): fix encoding path refId issue * feat(SW-1370): refactor AddAncillaryStore usage and introduce context provider * feat(SW-1370): refactor * feat(SW-1370): refactor ancillaries * feat(SW-1370): fix merge Approved-by: Simon.Emanuelsson
62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
"use client"
|
|
|
|
import { usePathname, useRouter, useSearchParams } from "next/navigation"
|
|
import { useCallback, useEffect } from "react"
|
|
import { useIntl } from "react-intl"
|
|
|
|
import { BookingErrorCodeEnum } from "@/constants/booking"
|
|
|
|
import { toast } from "@/components/TempDesignSystem/Toasts"
|
|
|
|
export function useGuaranteePaymentFailedToast() {
|
|
const intl = useIntl()
|
|
const searchParams = useSearchParams()
|
|
const pathname = usePathname()
|
|
const router = useRouter()
|
|
|
|
const getErrorMessage = useCallback(
|
|
(errorCode: string | null) => {
|
|
switch (errorCode) {
|
|
case BookingErrorCodeEnum.TransactionCancelled:
|
|
return intl.formatMessage({
|
|
id: "You have cancelled to process to guarantee your booking.",
|
|
})
|
|
case "AncillaryFailed":
|
|
return intl.formatMessage({
|
|
id: "The product could not be added. Your booking is guaranteed. Please try again.",
|
|
})
|
|
default:
|
|
return intl.formatMessage({
|
|
id: "We had an issue guaranteeing your booking. Please try again.",
|
|
})
|
|
}
|
|
},
|
|
[intl]
|
|
)
|
|
|
|
useEffect(() => {
|
|
const errorCode = searchParams.get("errorCode")
|
|
const errorMessage = getErrorMessage(errorCode)
|
|
if (!errorCode) return
|
|
|
|
// setTimeout is needed to show toasts on page load: https://sonner.emilkowal.ski/toast#render-toast-on-page-load
|
|
setTimeout(() => {
|
|
const toastType =
|
|
errorCode === BookingErrorCodeEnum.TransactionCancelled
|
|
? "warning"
|
|
: "error"
|
|
|
|
toast[toastType](errorMessage)
|
|
})
|
|
const ancillary = searchParams.get("ancillary")
|
|
if ((errorCode && ancillary) || errorCode === "AncillaryFailed") {
|
|
return
|
|
}
|
|
|
|
const queryParams = new URLSearchParams(searchParams.toString())
|
|
queryParams.delete("errorCode")
|
|
|
|
router.push(`${pathname}?${queryParams.toString()}`)
|
|
}, [searchParams, pathname, router, getErrorMessage])
|
|
}
|