From 9f14e83eb42f89127c0bb27c7b3acfd689fe3acb Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Mon, 26 Aug 2024 21:19:59 +0100 Subject: [PATCH] Show rating on the discover page --- src/components/Rating/Rating.tsx | 10 ++++++++-- src/lib/number.ts | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/components/Rating/Rating.tsx b/src/components/Rating/Rating.tsx index 8c5b38c0e..dfff6f8b5 100644 --- a/src/components/Rating/Rating.tsx +++ b/src/components/Rating/Rating.tsx @@ -1,5 +1,6 @@ import { useState } from 'react'; import { cn } from '../../lib/classname'; +import { decimalIfNeeded } from '../../lib/number.ts'; type RatingProps = { rating?: number; @@ -40,7 +41,7 @@ export function Rating(props: RatingProps) { if (readOnly) { return; } - + setStars(counter); onRatingChange?.(counter); }} @@ -49,7 +50,12 @@ export function Rating(props: RatingProps) { ); })} {(props.total || 0) > 0 && ( - + + {decimalIfNeeded(Number(props.rating!))} + + )} + {(props.total || 0) > 0 && ( + ({Intl.NumberFormat('en-US').format(props.total!)}) )} diff --git a/src/lib/number.ts b/src/lib/number.ts index 6527e13c8..9f3b2938a 100644 --- a/src/lib/number.ts +++ b/src/lib/number.ts @@ -5,3 +5,7 @@ export const formatter = Intl.NumberFormat('en-US', { export function formatCommaNumber(number: number): string { return formatter.format(number); } + +export function decimalIfNeeded(number: number): string { + return number % 1 === 0 ? number.toString() : number.toFixed(1); +}