Fix/STAY-133 * fix: Add static summary buttons row on add ancillary flow * fix: refactor handling of modals * fix: refactor file structure for add ancillary flow * Merged in chore/replace-deprecated-body (pull request #3300) Replace deprecated <Body> with <Typography> * chore: replace deprecated body component * refactor: replace Body component with Typography across various components * merge Approved-by: Bianca Widstam Approved-by: Matilda Landström Approved-by: Bianca Widstam Approved-by: Matilda Landström
147 lines
4.4 KiB
TypeScript
147 lines
4.4 KiB
TypeScript
import { AlertTypeEnum } from "@scandic-hotels/common/constants/alert"
|
|
import { guaranteeCallback } from "@scandic-hotels/common/constants/routes/hotelReservation"
|
|
import { BookingErrorCodeEnum } from "@scandic-hotels/trpc/enums/bookingErrorCode"
|
|
import { BreakfastPackageEnum } from "@scandic-hotels/trpc/enums/breakfast"
|
|
|
|
import { isWebview } from "@/constants/routes/webviews"
|
|
import { env } from "@/env/client"
|
|
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
import type { Packages } from "@scandic-hotels/trpc/types/packages"
|
|
import type { IntlShape } from "react-intl"
|
|
|
|
import type { AncillaryErrorMessage } from "@/types/components/myPages/myStay/ancillaries"
|
|
import type { BreakfastData } from "@/stores/my-stay/add-ancillary-flow"
|
|
import type { AncillaryFormData } from "./schema"
|
|
|
|
/**
|
|
* This function calculates some breakfast data in the store.
|
|
* It is used in various places in the add flow, but only needs
|
|
* to be calculated once.
|
|
*/
|
|
export function calculateBreakfastData(
|
|
isBreakfast: boolean,
|
|
packages: Packages | null,
|
|
nrOfAdults: number,
|
|
childrenAges: number[],
|
|
nrOfNights: number
|
|
): BreakfastData | null {
|
|
if (!isBreakfast || !packages) {
|
|
return null
|
|
}
|
|
|
|
const { nrOfPayingChildren, nrOfFreeChildren } = childrenAges.reduce(
|
|
(total, childAge) => {
|
|
if (childAge >= 4) {
|
|
total.nrOfPayingChildren = total.nrOfPayingChildren + 1
|
|
} else {
|
|
total.nrOfFreeChildren = total.nrOfFreeChildren + 1
|
|
}
|
|
return total
|
|
},
|
|
{ nrOfPayingChildren: 0, nrOfFreeChildren: 0 }
|
|
)
|
|
|
|
const adultPackage = packages.find(
|
|
(p) => p.code === BreakfastPackageEnum.ANCILLARY_REGULAR_BREAKFAST
|
|
)
|
|
const childPackage = packages.find(
|
|
(p) => p.code === BreakfastPackageEnum.ANCILLARY_CHILD_PAYING_BREAKFAST
|
|
)
|
|
const priceAdult = adultPackage?.localPrice.price ?? 0
|
|
const priceChild = childPackage?.localPrice.price ?? 0
|
|
const currency =
|
|
adultPackage?.localPrice.currency ?? childPackage?.localPrice.currency
|
|
|
|
const totalPrice =
|
|
priceAdult * nrOfAdults * nrOfNights +
|
|
priceChild * nrOfPayingChildren * nrOfNights
|
|
|
|
if (!currency) {
|
|
return null
|
|
}
|
|
|
|
return {
|
|
nrOfAdults,
|
|
nrOfPayingChildren,
|
|
nrOfFreeChildren,
|
|
nrOfNights,
|
|
priceAdult,
|
|
priceChild,
|
|
currency,
|
|
totalPrice,
|
|
}
|
|
}
|
|
|
|
export function buildBreakfastPackages(
|
|
data: AncillaryFormData,
|
|
breakfastData: BreakfastData
|
|
) {
|
|
const packages = [
|
|
{
|
|
code: BreakfastPackageEnum.ANCILLARY_REGULAR_BREAKFAST,
|
|
quantity: breakfastData.nrOfAdults,
|
|
comment: data.optionalText || undefined,
|
|
},
|
|
{
|
|
code: BreakfastPackageEnum.ANCILLARY_CHILD_PAYING_BREAKFAST,
|
|
quantity: breakfastData.nrOfPayingChildren,
|
|
comment: data.optionalText || undefined,
|
|
},
|
|
{
|
|
code: BreakfastPackageEnum.FREE_CHILD_BREAKFAST,
|
|
quantity: breakfastData.nrOfFreeChildren,
|
|
comment: data.optionalText || undefined,
|
|
},
|
|
]
|
|
|
|
return packages.filter((pkg) => pkg.quantity > 0)
|
|
}
|
|
|
|
export function getErrorMessage(
|
|
intl: IntlShape,
|
|
errorCode: string | null
|
|
): AncillaryErrorMessage {
|
|
switch (errorCode) {
|
|
case BookingErrorCodeEnum.TransactionFailed:
|
|
return {
|
|
message: intl.formatMessage({
|
|
id: "guaranteePayment.failed",
|
|
defaultMessage:
|
|
"We had an issue guaranteeing your booking. Please try again.",
|
|
}),
|
|
type: AlertTypeEnum.Alarm,
|
|
}
|
|
case BookingErrorCodeEnum.TransactionCancelled:
|
|
return {
|
|
message: intl.formatMessage({
|
|
id: "guaranteePayment.cancelled",
|
|
defaultMessage:
|
|
"You have cancelled the payment. Your booking is not guaranteed.",
|
|
}),
|
|
type: AlertTypeEnum.Warning,
|
|
}
|
|
case "AncillaryFailed":
|
|
return {
|
|
message: intl.formatMessage({
|
|
id: "guaranteePayment.ancillaryFailed",
|
|
defaultMessage:
|
|
"The product could not be added. Your booking is guaranteed. Please try again.",
|
|
}),
|
|
type: AlertTypeEnum.Alarm,
|
|
}
|
|
default:
|
|
return {
|
|
message: intl.formatMessage({
|
|
id: "guaranteePayment.genericError",
|
|
defaultMessage: "Something went wrong! Please try again later.",
|
|
}),
|
|
type: AlertTypeEnum.Alarm,
|
|
}
|
|
}
|
|
}
|
|
|
|
export function getGuaranteeCallback(lang: Lang, pathname: string) {
|
|
return `${env.NEXT_PUBLIC_NODE_ENV === "development" ? `http://localhost:${env.NEXT_PUBLIC_PORT}` : ""}${guaranteeCallback(lang, isWebview(pathname))}`
|
|
}
|