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",
})}
</Body>
{totalPrice.euro && (
{totalPrice.requested && (
<Caption color="uiTextMediumContrast">
{intl.formatMessage({ id: "Approx." })}{" "}
{intl.formatNumber(totalPrice.euro.price, {
currency: CurrencyEnum.EUR,
{intl.formatNumber(totalPrice.requested.price, {
currency: totalPrice.requested.currency,
style: "currency",
})}
</Caption>

View File

@@ -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,
}
}

View File

@@ -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 })

View File

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