Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 4a7aa3b

Browse files
committed
fix: always print decimals for prices
1 parent 14274a8 commit 4a7aa3b

7 files changed

Lines changed: 24 additions & 62 deletions

File tree

apps/ui/components/calendar/ReservationCalendarControls.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ function ReservationCalendarControls({
310310
reservationUnit,
311311
pricingDate: dateValue,
312312
minutes: duration,
313-
trailingZeros: true,
314313
})
315314
: null;
316315

apps/ui/components/reservation-unit/QuickReservation.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ function QuickReservation({
188188
reservationUnit,
189189
pricingDate: dateValue,
190190
minutes: duration,
191-
trailingZeros: true,
192191
});
193192
}, [duration, reservationUnit, dateValue]);
194193

apps/ui/components/reservation/ReservationCard.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,11 @@ function ReservationCard({ reservation, type }: PropsT): JSX.Element {
210210
new Date(reservation.end),
211211
new Date(reservation.begin)
212212
),
213-
trailingZeros: false,
214213
})
215214
: getReservationPrice(
216215
reservation.price ?? undefined,
217216
t("prices:priceFree"),
218-
false,
217+
true,
219218
i18n.language
220219
);
221220

apps/ui/components/reservation/ReservationInfoCard.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ export function ReservationInfoCard({
141141
reservationUnit,
142142
pricingDate: new Date(begin),
143143
minutes: duration,
144-
trailingZeros: true,
145144
})
146145
: getReservationPrice(
147146
reservation?.price,

apps/ui/modules/__tests__/reservationUnit.test.ts

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
isReservationUnitPaidInFuture,
3939
isReservationUnitPublished,
4040
isReservationUnitReservable,
41+
GetPriceType,
4142
} from "../reservationUnit";
4243
import mockTranslations from "../../public/locales/fi/prices.json";
4344
import { type ReservableMap, dateToKey, type RoundPeriod } from "../reservable";
@@ -217,7 +218,7 @@ describe("getPriceString", () => {
217218
highestPrice?: number;
218219
priceUnit?: PriceUnit;
219220
minutes?: number;
220-
}) {
221+
}): GetPriceType {
221222
return {
222223
pricing: constructPricing({
223224
lowestPrice,
@@ -234,7 +235,7 @@ describe("getPriceString", () => {
234235
highestPrice: 50.5,
235236
priceUnit: PriceUnit.Per_15Mins,
236237
});
237-
expect(getPriceString(input)).toBe("10 - 50,5 € / 15 min");
238+
expect(getPriceString(input)).toBe("10,00 - 50,50 € / 15 min");
238239
});
239240

240241
test("price range with no min", () => {
@@ -243,7 +244,7 @@ describe("getPriceString", () => {
243244
highestPrice: 50.5,
244245
priceUnit: PriceUnit.Per_15Mins,
245246
});
246-
expect(getPriceString(input)).toBe("0 - 50,5 € / 15 min");
247+
expect(getPriceString(input)).toBe("0 - 50,50 € / 15 min");
247248
});
248249

249250
test("price range with minutes", () => {
@@ -252,7 +253,7 @@ describe("getPriceString", () => {
252253
highestPrice: 60.5,
253254
minutes: 60,
254255
});
255-
expect(getPriceString(input)).toBe("0 - 60,5 €");
256+
expect(getPriceString(input)).toBe("0 - 60,50 €");
256257
});
257258

258259
test("price range with minutes", () => {
@@ -270,7 +271,7 @@ describe("getPriceString", () => {
270271
highestPrice: 100,
271272
minutes: 61,
272273
});
273-
expect(getPriceString(input)).toBe("0 - 125 €");
274+
expect(getPriceString(input)).toBe("0 - 125,00 €");
274275
});
275276

276277
test("price range with minutes", () => {
@@ -279,7 +280,7 @@ describe("getPriceString", () => {
279280
highestPrice: 100,
280281
minutes: 90,
281282
});
282-
expect(getPriceString(input)).toBe("0 - 150 €");
283+
expect(getPriceString(input)).toBe("0 - 150,00 €");
283284
});
284285

285286
test("price range with minutes", () => {
@@ -288,7 +289,7 @@ describe("getPriceString", () => {
288289
highestPrice: 100,
289290
minutes: 91,
290291
});
291-
expect(getPriceString(input)).toBe("0 - 175 €");
292+
expect(getPriceString(input)).toBe("0 - 175,00 €");
292293
});
293294

294295
test("price range with minutes", () => {
@@ -298,7 +299,7 @@ describe("getPriceString", () => {
298299
minutes: 60,
299300
priceUnit: PriceUnit.Per_15Mins,
300301
});
301-
expect(getPriceString(input)).toBe("0 - 120 €");
302+
expect(getPriceString(input)).toBe("0 - 120,00 €");
302303
});
303304

304305
test("price range with minutes", () => {
@@ -308,7 +309,7 @@ describe("getPriceString", () => {
308309
minutes: 60,
309310
priceUnit: PriceUnit.Per_30Mins,
310311
});
311-
expect(getPriceString(input)).toBe("0 - 60 €");
312+
expect(getPriceString(input)).toBe("0 - 60,00 €");
312313
});
313314

314315
test("price range with minutes", () => {
@@ -318,7 +319,7 @@ describe("getPriceString", () => {
318319
minutes: 61,
319320
priceUnit: PriceUnit.Per_30Mins,
320321
});
321-
expect(getPriceString(input)).toBe("0 - 75 €");
322+
expect(getPriceString(input)).toBe("0 - 75,00 €");
322323
});
323324

324325
test("price range with minutes and fixed unit", () => {
@@ -328,7 +329,7 @@ describe("getPriceString", () => {
328329
minutes: 61,
329330
priceUnit: PriceUnit.PerHalfDay,
330331
});
331-
expect(getPriceString(input)).toBe("10 - 100 €");
332+
expect(getPriceString(input)).toBe("10,00 - 100,00 €");
332333
});
333334

334335
test("price range with minutes and fixed unit", () => {
@@ -338,7 +339,7 @@ describe("getPriceString", () => {
338339
minutes: 1234,
339340
priceUnit: PriceUnit.PerDay,
340341
});
341-
expect(getPriceString(input)).toBe("10 - 100 €");
342+
expect(getPriceString(input)).toBe("10,00 - 100,00 €");
342343
});
343344

344345
test("price range with minutes and fixed unit", () => {
@@ -348,7 +349,7 @@ describe("getPriceString", () => {
348349
minutes: 1234,
349350
priceUnit: PriceUnit.PerWeek,
350351
});
351-
expect(getPriceString(input)).toBe("10 - 100 €");
352+
expect(getPriceString(input)).toBe("10,00 - 100,00 €");
352353
});
353354

354355
test("fixed price", () => {
@@ -357,16 +358,7 @@ describe("getPriceString", () => {
357358
highestPrice: 50,
358359
priceUnit: PriceUnit.Fixed,
359360
});
360-
expect(getPriceString(input)).toBe("50 €");
361-
});
362-
363-
test("fixed price with decimals", () => {
364-
const input = constructInput({
365-
lowestPrice: 50,
366-
highestPrice: 50,
367-
priceUnit: PriceUnit.Fixed,
368-
});
369-
expect(getPriceString({ ...input, trailingZeros: true })).toBe("50,00 €");
361+
expect(getPriceString(input)).toBe("50,00 €");
370362
});
371363

372364
test("no price", () => {
@@ -384,19 +376,7 @@ describe("getPriceString", () => {
384376
minutes: 180,
385377
priceUnit: PriceUnit.Per_15Mins,
386378
});
387-
expect(getPriceString(input)).toBe("0 - 606 €");
388-
});
389-
390-
test("total price with minutes and decimals", () => {
391-
const input = constructInput({
392-
lowestPrice: 0,
393-
highestPrice: 50.5,
394-
minutes: 180,
395-
priceUnit: PriceUnit.Per_15Mins,
396-
});
397-
expect(getPriceString({ ...input, trailingZeros: true })).toBe(
398-
"0 - 606,00 €"
399-
);
379+
expect(getPriceString(input)).toBe("0 - 606,00 €");
400380
});
401381
});
402382

@@ -1041,7 +1021,6 @@ describe("getReservationUnitPrice", () => {
10411021
}): GetReservationUnitPriceProps {
10421022
return {
10431023
pricingDate: date,
1044-
trailingZeros: true,
10451024
reservationUnit: {
10461025
pricings: [
10471026
constructPricing({

apps/ui/modules/reservationUnit.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,8 @@ export function getFuturePricing(
258258
: futurePricings[0];
259259
}
260260

261-
function formatPrice(
262-
price: number,
263-
trailingZeros: boolean,
264-
toCurrency?: boolean
265-
): string {
266-
const enableDecimals = price !== 0 && trailingZeros;
261+
function formatPrice(price: number, toCurrency?: boolean): string {
262+
const enableDecimals = price !== 0;
267263
const currencyFormatter = enableDecimals
268264
? "currencyWithDecimals"
269265
: "currency";
@@ -273,10 +269,9 @@ function formatPrice(
273269
return formatter.format(price);
274270
}
275271

276-
type GetPriceType = {
272+
export type GetPriceType = {
277273
pricing: PricingFieldsFragment;
278274
minutes?: number; // additional minutes for total price calculation
279-
trailingZeros?: boolean;
280275
};
281276

282277
function isPriceZero(pricing: PricingFieldsFragment): boolean {
@@ -294,7 +289,7 @@ function isPriceZero(pricing: PricingFieldsFragment): boolean {
294289
// TODO rewrite this return number normally
295290
// and a separate function to format it to string
296291
export function getPriceString(props: GetPriceType): string {
297-
const { pricing, minutes, trailingZeros = false } = props;
292+
const { pricing, minutes } = props;
298293

299294
if (isPriceZero(pricing)) {
300295
return i18n?.t("prices:priceFree") ?? "0";
@@ -305,8 +300,8 @@ export function getPriceString(props: GetPriceType): string {
305300
const lowestPrice = parseFloat(pricing.lowestPrice) * volume;
306301
const priceString =
307302
lowestPrice === highestPrice
308-
? formatPrice(lowestPrice, trailingZeros, true)
309-
: `${formatPrice(lowestPrice, trailingZeros)} - ${formatPrice(highestPrice, trailingZeros, true)}`;
303+
? formatPrice(lowestPrice, true)
304+
: `${formatPrice(lowestPrice)} - ${formatPrice(highestPrice, true)}`;
310305
const unitString =
311306
pricing.priceUnit === PriceUnit.Fixed || minutes
312307
? ""
@@ -318,18 +313,12 @@ export type GetReservationUnitPriceProps = {
318313
reservationUnit?: PriceReservationUnitFragment | null;
319314
pricingDate?: Date;
320315
minutes?: number;
321-
trailingZeros?: boolean;
322316
};
323317

324318
export function getReservationUnitPrice(
325319
props: GetReservationUnitPriceProps
326320
): string | null {
327-
const {
328-
reservationUnit: ru,
329-
pricingDate,
330-
minutes,
331-
trailingZeros = false,
332-
} = props;
321+
const { reservationUnit: ru, pricingDate, minutes } = props;
333322
if (pricingDate != null && Number.isNaN(pricingDate.getTime())) {
334323
// eslint-disable-next-line no-console
335324
console.warn("Invalid pricing date", pricingDate);
@@ -358,7 +347,6 @@ export function getReservationUnitPrice(
358347
return getPriceString({
359348
pricing,
360349
minutes,
361-
trailingZeros,
362350
});
363351
}
364352

apps/ui/pages/reservation-unit/[id].tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,6 @@ function ReservationUnit({
12221222
date: toUIDate(new Date(futurePricing.begins)),
12231223
price: getPriceString({
12241224
pricing: futurePricing,
1225-
trailingZeros: true,
12261225
}).toLocaleLowerCase(),
12271226
}}
12281227
components={{ bold: <strong /> }}

0 commit comments

Comments
 (0)