diff --git a/components/HotelReservation/EnterDetails/Summary/UI/index.tsx b/components/HotelReservation/EnterDetails/Summary/UI/index.tsx index 5e61ef155..2a33812f0 100644 --- a/components/HotelReservation/EnterDetails/Summary/UI/index.tsx +++ b/components/HotelReservation/EnterDetails/Summary/UI/index.tsx @@ -227,11 +227,11 @@ export default function SummaryUI({ style: "currency", })} - {totalPrice.euro && ( + {totalPrice.requested && ( {intl.formatMessage({ id: "Approx." })}{" "} - {intl.formatNumber(totalPrice.euro.price, { - currency: CurrencyEnum.EUR, + {intl.formatNumber(totalPrice.requested.price, { + currency: totalPrice.requested.currency, style: "currency", })} diff --git a/stores/enter-details/helpers.ts b/stores/enter-details/helpers.ts index 496b75ba6..f76caeb19 100644 --- a/stores/enter-details/helpers.ts +++ b/stores/enter-details/helpers.ts @@ -77,9 +77,9 @@ export function subtract(...nums: (number | string | undefined)[]) { export function getInitialRoomPrice(roomRate: RoomRate, isMember: boolean) { if (isMember && roomRate.memberRate) { return { - euro: { - currency: CurrencyEnum.EUR, - price: roomRate.memberRate.requestedPrice?.pricePerStay ?? 0, + requested: roomRate.memberRate.requestedPrice && { + currency: roomRate.memberRate.requestedPrice.currency, + price: roomRate.memberRate.requestedPrice.pricePerStay, }, local: { currency: roomRate.memberRate.localPrice.currency, @@ -89,9 +89,9 @@ export function getInitialRoomPrice(roomRate: RoomRate, isMember: boolean) { } return { - euro: { - currency: CurrencyEnum.EUR, - price: roomRate.publicRate.requestedPrice?.pricePerStay ?? 0, + requested: roomRate.publicRate.requestedPrice && { + currency: roomRate.publicRate.requestedPrice.currency, + price: roomRate.publicRate.requestedPrice.pricePerStay, }, local: { currency: roomRate.publicRate.localPrice.currency, @@ -103,9 +103,9 @@ export function getInitialRoomPrice(roomRate: RoomRate, isMember: boolean) { export function getInitialTotalPrice(roomRate: RoomRate, isMember: boolean) { if (isMember && roomRate.memberRate) { return { - euro: { - currency: CurrencyEnum.EUR, - price: roomRate.memberRate.requestedPrice?.pricePerStay ?? 0, + requested: roomRate.memberRate.requestedPrice && { + currency: roomRate.memberRate.requestedPrice.currency, + price: roomRate.memberRate.requestedPrice.pricePerStay, }, local: { currency: roomRate.memberRate.localPrice.currency, @@ -115,9 +115,9 @@ export function getInitialTotalPrice(roomRate: RoomRate, isMember: boolean) { } return { - euro: { - currency: CurrencyEnum.EUR, - price: roomRate.publicRate.requestedPrice?.pricePerStay ?? 0, + requested: roomRate.publicRate.requestedPrice && { + currency: roomRate.publicRate.requestedPrice.currency, + price: roomRate.publicRate.requestedPrice.pricePerStay, }, local: { currency: roomRate.publicRate.localPrice.currency, @@ -165,31 +165,31 @@ export function calcTotalPrice( totalPrice: state.totalPrice, } if (state.requestedPrice?.pricePerStay) { - roomAndTotalPrice.roomPrice.euro = { - currency: CurrencyEnum.EUR, + roomAndTotalPrice.roomPrice.requested = { + currency: state.requestedPrice.currency, price: state.requestedPrice.pricePerStay, } - let totalPriceEuro = state.requestedPrice.pricePerStay + let totalPriceRequested = state.requestedPrice.pricePerStay if (state.breakfast) { - totalPriceEuro = add( - totalPriceEuro, + totalPriceRequested = add( + totalPriceRequested, state.breakfast.requestedPrice.totalPrice ) } if (state.packages) { - totalPriceEuro = state.packages.reduce((total, pkg) => { + totalPriceRequested = state.packages.reduce((total, pkg) => { if (pkg.requestedPrice.totalPrice) { total = add(total, pkg.requestedPrice.totalPrice) } return total - }, totalPriceEuro) + }, totalPriceRequested) } - roomAndTotalPrice.totalPrice.euro = { - currency: CurrencyEnum.EUR, - price: totalPriceEuro, + roomAndTotalPrice.totalPrice.requested = { + currency: state.requestedPrice.currency, + price: totalPriceRequested, } } diff --git a/stores/enter-details/index.ts b/stores/enter-details/index.ts index a78116374..4b70fb254 100644 --- a/stores/enter-details/index.ts +++ b/stores/enter-details/index.ts @@ -115,10 +115,12 @@ export function createDetailsStore( if (initialState.packages) { initialState.packages.forEach((pkg) => { - initialTotalPrice.euro.price = add( - initialTotalPrice.euro.price, - pkg.requestedPrice.totalPrice - ) + if (initialTotalPrice.requested) { + initialTotalPrice.requested.price = add( + initialTotalPrice.requested.price, + pkg.requestedPrice.totalPrice + ) + } initialTotalPrice.local.price = add( initialTotalPrice.local.price, pkg.localPrice.totalPrice @@ -165,7 +167,7 @@ export function createDetailsStore( setTotalPrice(totalPrice) { return set( produce((state: DetailsState) => { - state.totalPrice.euro = totalPrice.euro + state.totalPrice.requested = totalPrice.requested state.totalPrice.local = totalPrice.local }) ) @@ -194,7 +196,8 @@ export function createDetailsStore( return set( produce((state: DetailsState) => { state.isValid.breakfast = true - const stateTotalEuroPrice = state.totalPrice.euro?.price || 0 + const stateTotalRequestedPrice = + state.totalPrice.requested?.price || 0 const stateTotalLocalPrice = state.totalPrice.local.price const addToTotalPrice = @@ -206,7 +209,7 @@ export function createDetailsStore( breakfast === false if (addToTotalPrice) { - const breakfastTotalEuroPrice = parseInt( + const breakfastTotalRequestedPrice = parseInt( breakfast.requestedPrice.totalPrice ) const breakfastTotalPrice = parseInt( @@ -214,9 +217,10 @@ export function createDetailsStore( ) state.totalPrice = { - euro: { - currency: CurrencyEnum.EUR, - price: stateTotalEuroPrice + breakfastTotalEuroPrice, + requested: state.totalPrice.requested && { + currency: state.totalPrice.requested.currency, + price: + stateTotalRequestedPrice + breakfastTotalRequestedPrice, }, local: { currency: breakfast.localPrice.currency, @@ -229,21 +233,22 @@ export function createDetailsStore( let currency = state.totalPrice.local.currency ?? langToCurrency() let currentBreakfastTotalPrice = 0 - let currentBreakfastTotalEuroPrice = 0 + let currentBreakfastTotalRequestedPrice = 0 if (state.breakfast) { currentBreakfastTotalPrice = parseInt( state.breakfast.localPrice.totalPrice ) - currentBreakfastTotalEuroPrice = parseInt( + currentBreakfastTotalRequestedPrice = parseInt( state.breakfast.requestedPrice.totalPrice ) currency = state.breakfast.localPrice.currency } - let euroPrice = - stateTotalEuroPrice - currentBreakfastTotalEuroPrice - if (euroPrice < 0) { - euroPrice = 0 + let requestedPrice = + stateTotalRequestedPrice - + currentBreakfastTotalRequestedPrice + if (requestedPrice < 0) { + requestedPrice = 0 } let localPrice = stateTotalLocalPrice - currentBreakfastTotalPrice @@ -252,9 +257,9 @@ export function createDetailsStore( } state.totalPrice = { - euro: { - currency: CurrencyEnum.EUR, - price: euroPrice, + requested: state.totalPrice.requested && { + currency: state.totalPrice.requested.currency, + price: requestedPrice, }, local: { currency, @@ -349,8 +354,15 @@ export function createDetailsStore( persistedState.booking, currentState.booking ) + if (!isSameBooking) { - return deepmerge(persistedState, currentState, { arrayMerge }) + // We get the booking data from query params, and the "newest" booking data should always be used. + // Merging the two states can lead to issues since some params or values in arrays might be removed. + // @ts-expect-error - persistedState cannot be typed + delete persistedState.booking + return deepmerge(persistedState, currentState, { + arrayMerge, + }) } } return deepmerge(currentState, persistedState ?? {}, { arrayMerge }) diff --git a/types/stores/enter-details.ts b/types/stores/enter-details.ts index e44fd8ce1..85270217f 100644 --- a/types/stores/enter-details.ts +++ b/types/stores/enter-details.ts @@ -15,7 +15,7 @@ interface TPrice { } export interface Price { - euro: TPrice | undefined + requested: TPrice | undefined local: TPrice }