Files
web/apps/scandic-web/hooks/booking/useGuaranteePaymentFailedToast.ts
Joakim Jäderberg 72f4f72a17 Merged in SW-3317-move-toast-to-design-system (pull request #2716)
SW-3317 move toast to design system

* chore: Move toast to design-system and add interaction tests

* Move toast to design-system and add storybook tests

* Merge branch 'master' of bitbucket.org:scandic-swap/web into SW-3317-move-toast-to-design-system

* merge

* move sonner dependency to @scandic-hotels/design-system


Approved-by: Anton Gunnarsson
2025-08-27 13:03:17 +00:00

57 lines
1.7 KiB
TypeScript

"use client"
import { usePathname, useRouter, useSearchParams } from "next/navigation"
import { useCallback, useEffect } from "react"
import { useIntl } from "react-intl"
import { toast } from "@scandic-hotels/design-system/Toast"
import { BookingErrorCodeEnum } from "@scandic-hotels/trpc/enums/bookingErrorCode"
export function useGuaranteePaymentFailedToast() {
const intl = useIntl()
const searchParams = useSearchParams()
const pathname = usePathname()
const router = useRouter()
const getErrorMessage = useCallback(
(errorCode: string | null) => {
switch (errorCode) {
case "AncillaryFailed":
return intl.formatMessage({
defaultMessage:
"The product could not be added. Your booking is guaranteed. Please try again.",
})
default:
return intl.formatMessage({
defaultMessage:
"We had an issue guaranteeing your booking. Please try again.",
})
}
},
[intl]
)
useEffect(() => {
const errorCode = searchParams.get("errorCode")
const errorMessage = getErrorMessage(errorCode)
if (!errorCode || errorCode === BookingErrorCodeEnum.TransactionCancelled)
return
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])
}