Merged in fix/STAY-2-GLA-cancelled (pull request #3109)
Fix/STAY-2 GLA cancelled * fix: show toast on cancelling GLA flow * fix: show the ancillary GLA errors as inline alerts Approved-by: Bianca Widstam Approved-by: Erik Tiekstra
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
"use client"
|
||||
|
||||
import { usePathname, useRouter, useSearchParams } from "next/navigation"
|
||||
import { useCallback, useEffect } from "react"
|
||||
import { useCallback, useEffect, useRef } from "react"
|
||||
import { useIntl } from "react-intl"
|
||||
|
||||
import { toast } from "@scandic-hotels/design-system/Toast"
|
||||
import { BookingErrorCodeEnum } from "@scandic-hotels/trpc/enums/bookingErrorCode"
|
||||
|
||||
import { isAncillaryError } from "@/components/HotelReservation/MyStay/utils"
|
||||
|
||||
export function useGuaranteePaymentFailedToast() {
|
||||
const hasRunOnce = useRef(false)
|
||||
const intl = useIntl()
|
||||
const searchParams = useSearchParams()
|
||||
const pathname = usePathname()
|
||||
@@ -16,11 +19,11 @@ export function useGuaranteePaymentFailedToast() {
|
||||
const getErrorMessage = useCallback(
|
||||
(errorCode: string | null) => {
|
||||
switch (errorCode) {
|
||||
case "AncillaryFailed":
|
||||
case BookingErrorCodeEnum.TransactionCancelled:
|
||||
return intl.formatMessage({
|
||||
id: "guaranteePayment.ancillaryFailed",
|
||||
id: "guaranteePayment.cancelled",
|
||||
defaultMessage:
|
||||
"The product could not be added. Your booking is guaranteed. Please try again.",
|
||||
"You have cancelled the payment. Your booking is not guaranteed.",
|
||||
})
|
||||
default:
|
||||
return intl.formatMessage({
|
||||
@@ -34,25 +37,33 @@ export function useGuaranteePaymentFailedToast() {
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
const errorCode = searchParams.get("errorCode")
|
||||
const errorMessage = getErrorMessage(errorCode)
|
||||
if (!errorCode || errorCode === BookingErrorCodeEnum.TransactionCancelled)
|
||||
// To prevent multiple toasts in strict mode
|
||||
if (hasRunOnce.current) {
|
||||
return
|
||||
}
|
||||
const errorCode = searchParams.get("errorCode")
|
||||
if (!errorCode) {
|
||||
return
|
||||
}
|
||||
|
||||
// Ancillary errors are handled in AddAncillaryFlowModal
|
||||
if (isAncillaryError(searchParams)) {
|
||||
hasRunOnce.current = true
|
||||
return
|
||||
}
|
||||
|
||||
const errorMessage = getErrorMessage(errorCode)
|
||||
const toastType =
|
||||
errorCode === BookingErrorCodeEnum.TransactionCancelled
|
||||
? "warning"
|
||||
: "error"
|
||||
toast[toastType](errorMessage)
|
||||
|
||||
const ancillary = searchParams.get("ancillary")
|
||||
if ((errorCode && ancillary) || errorCode === "AncillaryFailed") {
|
||||
return
|
||||
}
|
||||
toast[toastType](errorMessage)
|
||||
|
||||
const queryParams = new URLSearchParams(searchParams.toString())
|
||||
queryParams.delete("errorCode")
|
||||
|
||||
router.push(`${pathname}?${queryParams.toString()}`)
|
||||
hasRunOnce.current = true
|
||||
}, [searchParams, pathname, router, getErrorMessage])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user