Merged in fix/sw-2091-remove-breakfast (pull request #1689)
fix(SW-2091): remove breakfast ancillaries * fix(SW-2091): remove breakfast ancillaries * Send array in correct format * Break out function Approved-by: Bianca Widstam
This commit is contained in:
@@ -11,12 +11,12 @@ import useLang from "@/hooks/useLang"
|
||||
|
||||
export default function RemoveButton({
|
||||
confirmationNumber,
|
||||
code,
|
||||
codes,
|
||||
title,
|
||||
onSuccess,
|
||||
}: {
|
||||
confirmationNumber: string
|
||||
code: string
|
||||
codes: string[]
|
||||
title?: string
|
||||
onSuccess: () => void
|
||||
}) {
|
||||
@@ -44,7 +44,7 @@ export default function RemoveButton({
|
||||
{
|
||||
language: lang,
|
||||
confirmationNumber,
|
||||
codes: [code],
|
||||
codes,
|
||||
},
|
||||
{
|
||||
onSuccess: (data) => {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { useRouter } from "next/navigation"
|
||||
import { useMemo } from "react"
|
||||
import { useIntl } from "react-intl"
|
||||
|
||||
import { MaterialIcon } from "@scandic-hotels/design-system/Icons"
|
||||
@@ -18,7 +17,10 @@ import styles from "./addedAncillaries.module.css"
|
||||
|
||||
import type { AddedAncillariesProps } from "@/types/components/myPages/myStay/ancillaries"
|
||||
import { BreakfastPackageEnum } from "@/types/enums/breakfast"
|
||||
import type { PackageSchema } from "@/types/trpc/routers/booking/confirmation"
|
||||
import type {
|
||||
BookingConfirmation,
|
||||
PackageSchema,
|
||||
} from "@/types/trpc/routers/booking/confirmation"
|
||||
|
||||
export function AddedAncillaries({
|
||||
ancillaries,
|
||||
@@ -27,42 +29,11 @@ export function AddedAncillaries({
|
||||
const intl = useIntl()
|
||||
const router = useRouter()
|
||||
|
||||
/**
|
||||
* All ancillaries that are added to the booking
|
||||
*
|
||||
* Adding a special ancillary for breakfast, calculated from
|
||||
* all breakfast packages that has been added as ancillaries,
|
||||
* not in the booking flow.
|
||||
*/
|
||||
const addedAncillaries = useMemo(() => {
|
||||
const addedBreakfastPackages = getBreakfastPackagesFromAncillaryFlow(
|
||||
booking.packages
|
||||
)
|
||||
if (!addedBreakfastPackages?.length) {
|
||||
return booking.ancillaries
|
||||
}
|
||||
const addedBreakfastPackages = getBreakfastPackagesFromAncillaryFlow(
|
||||
booking.packages
|
||||
)
|
||||
|
||||
const combinedBreakfastPackageAsAncillary: PackageSchema = {
|
||||
code: BreakfastPackageEnum.ANCILLARY_REGULAR_BREAKFAST,
|
||||
unitPrice: 0,
|
||||
points: 0,
|
||||
currency: addedBreakfastPackages[0].currency,
|
||||
type: addedBreakfastPackages[0].type,
|
||||
description: addedBreakfastPackages[0].description,
|
||||
comment: addedBreakfastPackages[0].comment,
|
||||
totalPrice: addedBreakfastPackages.reduce(
|
||||
(acc, curr) => acc + curr.totalPrice,
|
||||
0
|
||||
),
|
||||
unit: addedBreakfastPackages.reduce((acc, curr) => acc + curr.unit, 0),
|
||||
totalUnit: addedBreakfastPackages.reduce(
|
||||
(acc, curr) => acc + curr.totalUnit,
|
||||
0
|
||||
),
|
||||
}
|
||||
|
||||
return [combinedBreakfastPackageAsAncillary, ...booking.ancillaries]
|
||||
}, [booking.ancillaries, booking.packages])
|
||||
const addedAncillaries = getAddedAncillaries(booking, addedBreakfastPackages)
|
||||
|
||||
if (addedAncillaries.length === 0) {
|
||||
return null
|
||||
@@ -133,7 +104,12 @@ export function AddedAncillaries({
|
||||
<div className={styles.actions}>
|
||||
<RemoveButton
|
||||
confirmationNumber={booking.confirmationNumber}
|
||||
code={ancillary.code}
|
||||
codes={
|
||||
ancillary.code ===
|
||||
BreakfastPackageEnum.ANCILLARY_REGULAR_BREAKFAST
|
||||
? addedBreakfastPackages!.map((p) => p.code)
|
||||
: [ancillary.code]
|
||||
}
|
||||
title={ancillaryTitle}
|
||||
onSuccess={router.refresh}
|
||||
/>
|
||||
@@ -179,7 +155,12 @@ export function AddedAncillaries({
|
||||
<div className={styles.actions}>
|
||||
<RemoveButton
|
||||
confirmationNumber={booking.confirmationNumber}
|
||||
code={ancillary.code}
|
||||
codes={
|
||||
ancillary.code ===
|
||||
BreakfastPackageEnum.ANCILLARY_REGULAR_BREAKFAST
|
||||
? addedBreakfastPackages!.map((p) => p.code)
|
||||
: [ancillary.code]
|
||||
}
|
||||
title={ancillaryTitle}
|
||||
onSuccess={() => {
|
||||
trackRemoveAncillary(
|
||||
@@ -200,3 +181,40 @@ export function AddedAncillaries({
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* All ancillaries that are added to the booking
|
||||
*
|
||||
* Adding a special ancillary for breakfast, calculated from
|
||||
* all breakfast packages that has been added as ancillaries,
|
||||
* not in the booking flow.
|
||||
*/
|
||||
function getAddedAncillaries(
|
||||
booking: BookingConfirmation["booking"],
|
||||
addedBreakfastPackages: PackageSchema[] | undefined
|
||||
) {
|
||||
if (!addedBreakfastPackages?.length) {
|
||||
return booking.ancillaries
|
||||
}
|
||||
|
||||
const combinedBreakfastPackageAsAncillary: PackageSchema = {
|
||||
code: BreakfastPackageEnum.ANCILLARY_REGULAR_BREAKFAST,
|
||||
unitPrice: 0,
|
||||
points: 0,
|
||||
currency: addedBreakfastPackages[0].currency,
|
||||
type: addedBreakfastPackages[0].type,
|
||||
description: addedBreakfastPackages[0].description,
|
||||
comment: addedBreakfastPackages[0].comment,
|
||||
totalPrice: addedBreakfastPackages.reduce(
|
||||
(acc, curr) => acc + curr.totalPrice,
|
||||
0
|
||||
),
|
||||
unit: addedBreakfastPackages.reduce((acc, curr) => acc + curr.unit, 0),
|
||||
totalUnit: addedBreakfastPackages.reduce(
|
||||
(acc, curr) => acc + curr.totalUnit,
|
||||
0
|
||||
),
|
||||
}
|
||||
|
||||
return [combinedBreakfastPackageAsAncillary, ...booking.ancillaries]
|
||||
}
|
||||
|
||||
@@ -576,7 +576,7 @@ export const bookingMutationRouter = router({
|
||||
{
|
||||
headers,
|
||||
} as RequestInit,
|
||||
{ language, codes }
|
||||
[["language", language], ...codes.map((code) => ["codes", code])]
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
|
||||
Reference in New Issue
Block a user