fix: change euro price to user's currency + merge state correctly

This commit is contained in:
Christel Westerberg
2024-12-03 13:59:35 +01:00
parent 4dbdf81090
commit 528107e0ef
4 changed files with 58 additions and 46 deletions

View File

@@ -227,11 +227,11 @@ export default function SummaryUI({
style: "currency", style: "currency",
})} })}
</Body> </Body>
{totalPrice.euro && ( {totalPrice.requested && (
<Caption color="uiTextMediumContrast"> <Caption color="uiTextMediumContrast">
{intl.formatMessage({ id: "Approx." })}{" "} {intl.formatMessage({ id: "Approx." })}{" "}
{intl.formatNumber(totalPrice.euro.price, { {intl.formatNumber(totalPrice.requested.price, {
currency: CurrencyEnum.EUR, currency: totalPrice.requested.currency,
style: "currency", style: "currency",
})} })}
</Caption> </Caption>

View File

@@ -77,9 +77,9 @@ export function subtract(...nums: (number | string | undefined)[]) {
export function getInitialRoomPrice(roomRate: RoomRate, isMember: boolean) { export function getInitialRoomPrice(roomRate: RoomRate, isMember: boolean) {
if (isMember && roomRate.memberRate) { if (isMember && roomRate.memberRate) {
return { return {
euro: { requested: roomRate.memberRate.requestedPrice && {
currency: CurrencyEnum.EUR, currency: roomRate.memberRate.requestedPrice.currency,
price: roomRate.memberRate.requestedPrice?.pricePerStay ?? 0, price: roomRate.memberRate.requestedPrice.pricePerStay,
}, },
local: { local: {
currency: roomRate.memberRate.localPrice.currency, currency: roomRate.memberRate.localPrice.currency,
@@ -89,9 +89,9 @@ export function getInitialRoomPrice(roomRate: RoomRate, isMember: boolean) {
} }
return { return {
euro: { requested: roomRate.publicRate.requestedPrice && {
currency: CurrencyEnum.EUR, currency: roomRate.publicRate.requestedPrice.currency,
price: roomRate.publicRate.requestedPrice?.pricePerStay ?? 0, price: roomRate.publicRate.requestedPrice.pricePerStay,
}, },
local: { local: {
currency: roomRate.publicRate.localPrice.currency, currency: roomRate.publicRate.localPrice.currency,
@@ -103,9 +103,9 @@ export function getInitialRoomPrice(roomRate: RoomRate, isMember: boolean) {
export function getInitialTotalPrice(roomRate: RoomRate, isMember: boolean) { export function getInitialTotalPrice(roomRate: RoomRate, isMember: boolean) {
if (isMember && roomRate.memberRate) { if (isMember && roomRate.memberRate) {
return { return {
euro: { requested: roomRate.memberRate.requestedPrice && {
currency: CurrencyEnum.EUR, currency: roomRate.memberRate.requestedPrice.currency,
price: roomRate.memberRate.requestedPrice?.pricePerStay ?? 0, price: roomRate.memberRate.requestedPrice.pricePerStay,
}, },
local: { local: {
currency: roomRate.memberRate.localPrice.currency, currency: roomRate.memberRate.localPrice.currency,
@@ -115,9 +115,9 @@ export function getInitialTotalPrice(roomRate: RoomRate, isMember: boolean) {
} }
return { return {
euro: { requested: roomRate.publicRate.requestedPrice && {
currency: CurrencyEnum.EUR, currency: roomRate.publicRate.requestedPrice.currency,
price: roomRate.publicRate.requestedPrice?.pricePerStay ?? 0, price: roomRate.publicRate.requestedPrice.pricePerStay,
}, },
local: { local: {
currency: roomRate.publicRate.localPrice.currency, currency: roomRate.publicRate.localPrice.currency,
@@ -165,31 +165,31 @@ export function calcTotalPrice(
totalPrice: state.totalPrice, totalPrice: state.totalPrice,
} }
if (state.requestedPrice?.pricePerStay) { if (state.requestedPrice?.pricePerStay) {
roomAndTotalPrice.roomPrice.euro = { roomAndTotalPrice.roomPrice.requested = {
currency: CurrencyEnum.EUR, currency: state.requestedPrice.currency,
price: state.requestedPrice.pricePerStay, price: state.requestedPrice.pricePerStay,
} }
let totalPriceEuro = state.requestedPrice.pricePerStay let totalPriceRequested = state.requestedPrice.pricePerStay
if (state.breakfast) { if (state.breakfast) {
totalPriceEuro = add( totalPriceRequested = add(
totalPriceEuro, totalPriceRequested,
state.breakfast.requestedPrice.totalPrice state.breakfast.requestedPrice.totalPrice
) )
} }
if (state.packages) { if (state.packages) {
totalPriceEuro = state.packages.reduce((total, pkg) => { totalPriceRequested = state.packages.reduce((total, pkg) => {
if (pkg.requestedPrice.totalPrice) { if (pkg.requestedPrice.totalPrice) {
total = add(total, pkg.requestedPrice.totalPrice) total = add(total, pkg.requestedPrice.totalPrice)
} }
return total return total
}, totalPriceEuro) }, totalPriceRequested)
} }
roomAndTotalPrice.totalPrice.euro = { roomAndTotalPrice.totalPrice.requested = {
currency: CurrencyEnum.EUR, currency: state.requestedPrice.currency,
price: totalPriceEuro, price: totalPriceRequested,
} }
} }

View File

@@ -115,10 +115,12 @@ export function createDetailsStore(
if (initialState.packages) { if (initialState.packages) {
initialState.packages.forEach((pkg) => { initialState.packages.forEach((pkg) => {
initialTotalPrice.euro.price = add( if (initialTotalPrice.requested) {
initialTotalPrice.euro.price, initialTotalPrice.requested.price = add(
pkg.requestedPrice.totalPrice initialTotalPrice.requested.price,
) pkg.requestedPrice.totalPrice
)
}
initialTotalPrice.local.price = add( initialTotalPrice.local.price = add(
initialTotalPrice.local.price, initialTotalPrice.local.price,
pkg.localPrice.totalPrice pkg.localPrice.totalPrice
@@ -165,7 +167,7 @@ export function createDetailsStore(
setTotalPrice(totalPrice) { setTotalPrice(totalPrice) {
return set( return set(
produce((state: DetailsState) => { produce((state: DetailsState) => {
state.totalPrice.euro = totalPrice.euro state.totalPrice.requested = totalPrice.requested
state.totalPrice.local = totalPrice.local state.totalPrice.local = totalPrice.local
}) })
) )
@@ -194,7 +196,8 @@ export function createDetailsStore(
return set( return set(
produce((state: DetailsState) => { produce((state: DetailsState) => {
state.isValid.breakfast = true 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 stateTotalLocalPrice = state.totalPrice.local.price
const addToTotalPrice = const addToTotalPrice =
@@ -206,7 +209,7 @@ export function createDetailsStore(
breakfast === false breakfast === false
if (addToTotalPrice) { if (addToTotalPrice) {
const breakfastTotalEuroPrice = parseInt( const breakfastTotalRequestedPrice = parseInt(
breakfast.requestedPrice.totalPrice breakfast.requestedPrice.totalPrice
) )
const breakfastTotalPrice = parseInt( const breakfastTotalPrice = parseInt(
@@ -214,9 +217,10 @@ export function createDetailsStore(
) )
state.totalPrice = { state.totalPrice = {
euro: { requested: state.totalPrice.requested && {
currency: CurrencyEnum.EUR, currency: state.totalPrice.requested.currency,
price: stateTotalEuroPrice + breakfastTotalEuroPrice, price:
stateTotalRequestedPrice + breakfastTotalRequestedPrice,
}, },
local: { local: {
currency: breakfast.localPrice.currency, currency: breakfast.localPrice.currency,
@@ -229,21 +233,22 @@ export function createDetailsStore(
let currency = let currency =
state.totalPrice.local.currency ?? langToCurrency() state.totalPrice.local.currency ?? langToCurrency()
let currentBreakfastTotalPrice = 0 let currentBreakfastTotalPrice = 0
let currentBreakfastTotalEuroPrice = 0 let currentBreakfastTotalRequestedPrice = 0
if (state.breakfast) { if (state.breakfast) {
currentBreakfastTotalPrice = parseInt( currentBreakfastTotalPrice = parseInt(
state.breakfast.localPrice.totalPrice state.breakfast.localPrice.totalPrice
) )
currentBreakfastTotalEuroPrice = parseInt( currentBreakfastTotalRequestedPrice = parseInt(
state.breakfast.requestedPrice.totalPrice state.breakfast.requestedPrice.totalPrice
) )
currency = state.breakfast.localPrice.currency currency = state.breakfast.localPrice.currency
} }
let euroPrice = let requestedPrice =
stateTotalEuroPrice - currentBreakfastTotalEuroPrice stateTotalRequestedPrice -
if (euroPrice < 0) { currentBreakfastTotalRequestedPrice
euroPrice = 0 if (requestedPrice < 0) {
requestedPrice = 0
} }
let localPrice = let localPrice =
stateTotalLocalPrice - currentBreakfastTotalPrice stateTotalLocalPrice - currentBreakfastTotalPrice
@@ -252,9 +257,9 @@ export function createDetailsStore(
} }
state.totalPrice = { state.totalPrice = {
euro: { requested: state.totalPrice.requested && {
currency: CurrencyEnum.EUR, currency: state.totalPrice.requested.currency,
price: euroPrice, price: requestedPrice,
}, },
local: { local: {
currency, currency,
@@ -349,8 +354,15 @@ export function createDetailsStore(
persistedState.booking, persistedState.booking,
currentState.booking currentState.booking
) )
if (!isSameBooking) { 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 }) return deepmerge(currentState, persistedState ?? {}, { arrayMerge })

View File

@@ -15,7 +15,7 @@ interface TPrice {
} }
export interface Price { export interface Price {
euro: TPrice | undefined requested: TPrice | undefined
local: TPrice local: TPrice
} }