diff --git a/src/features/club-details/components/ReviewSummary.tsx b/src/features/club-details/components/ReviewSummary.tsx index 5368db57..3abb9e6c 100644 --- a/src/features/club-details/components/ReviewSummary.tsx +++ b/src/features/club-details/components/ReviewSummary.tsx @@ -1,22 +1,12 @@ import ProgressBar from '@/components/progress-bar/ProgressBar'; import RatingDisplay from '@/components/rating-display/RatingDisplay'; import { ClubReviewResponse } from '../types'; +import { + calculateRatingPercentage, + getRatingCount, +} from '@/features/club-details/utils/rating'; function ReviewSummary({ reviewInfo }: { reviewInfo: ClubReviewResponse }) { - const ratingCounts = (score: number) => { - return reviewInfo.ratingCounts[ - score === 5 - ? 'five' - : score === 4 - ? 'four' - : score === 3 - ? 'three' - : score === 2 - ? 'two' - : 'one' - ]; - }; - return (

@@ -25,7 +15,7 @@ function ReviewSummary({ reviewInfo }: { reviewInfo: ClubReviewResponse }) {
@@ -51,12 +41,15 @@ function ReviewSummary({ reviewInfo }: { reviewInfo: ClubReviewResponse }) { }} >
- {ratingCounts(score)} + {getRatingCount(score, reviewInfo.ratingCounts)}
))} @@ -65,4 +58,5 @@ function ReviewSummary({ reviewInfo }: { reviewInfo: ClubReviewResponse }) {

); } + export default ReviewSummary; diff --git a/src/features/club-details/utils/rating.ts b/src/features/club-details/utils/rating.ts new file mode 100644 index 00000000..19b75c21 --- /dev/null +++ b/src/features/club-details/utils/rating.ts @@ -0,0 +1,30 @@ +import { ClubReviewResponse } from '@/features/club-details/types'; + +type RatingCounts = ClubReviewResponse['ratingCounts']; + +export const getRatingCount = (score: number, ratingCounts: RatingCounts) => { + return ratingCounts[ + score === 5 + ? 'five' + : score === 4 + ? 'four' + : score === 3 + ? 'three' + : score === 2 + ? 'two' + : 'one' + ]; +}; + +export const calculateRatingPercentage = ( + score: number, + ratingCounts: RatingCounts, +) => { + const totalCount = Object.values(ratingCounts).reduce( + (sum, count) => sum + count, + 0, + ); + return totalCount === 0 + ? 0 + : (getRatingCount(score, ratingCounts) / totalCount) * 100; +};