* refactor: extract rating values and emojis into reusable constants - Create packages/lib/rating.ts with RATING_OPTIONS array and validation utilities - Update bookings-single-view.tsx to use extracted constants - Add TypeScript interfaces for type safety - Enable reuse of rating constants across the codebase Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * refactor: move rating utilities to bookings feature directory - Move packages/lib/rating.ts to packages/features/bookings/lib/rating.ts - Update import path in bookings-single-view.tsx to use new location - Maintain domain-specific organization for booking-related utilities Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * fix: resolve linting issues in event-types.tsx - Fix quote style consistency (single to double quotes) - Add missing trailing comma - Resolve pre-commit hook formatting changes Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
export interface RatingOption {
|
|
value: number;
|
|
emoji: string;
|
|
}
|
|
|
|
export const MIN_RATING = 1;
|
|
export const MAX_RATING = 5;
|
|
export const DEFAULT_RATING = 3;
|
|
|
|
export const RATING_OPTIONS: RatingOption[] = [
|
|
{ value: 1, emoji: "😠" },
|
|
{ value: 2, emoji: "🙁" },
|
|
{ value: 3, emoji: "😐" },
|
|
{ value: 4, emoji: "😄" },
|
|
{ value: 5, emoji: "😍" },
|
|
];
|
|
|
|
/**
|
|
* Validates and normalizes a rating value to ensure it's within the valid range
|
|
* @param rating - The rating value to validate (can be string or number)
|
|
* @param defaultValue - The default value to use if rating is invalid (defaults to DEFAULT_RATING)
|
|
* @returns A valid rating number between MIN_RATING and MAX_RATING
|
|
*/
|
|
export function validateRating(
|
|
rating: string | number | null | undefined,
|
|
defaultValue = DEFAULT_RATING
|
|
): number {
|
|
const parsedRating = typeof rating === "string" ? parseInt(rating, 10) : rating;
|
|
|
|
if (!parsedRating || isNaN(parsedRating)) {
|
|
return defaultValue;
|
|
}
|
|
|
|
if (parsedRating > MAX_RATING) return MAX_RATING;
|
|
if (parsedRating < MIN_RATING) return MIN_RATING;
|
|
|
|
return parsedRating;
|
|
}
|
|
|
|
/**
|
|
* Gets the emoji for a given rating value
|
|
* @param rating - The rating value
|
|
* @returns The corresponding emoji or empty string if not found
|
|
*/
|
|
export function getRatingEmoji(rating: number): string {
|
|
const option = RATING_OPTIONS.find((opt) => opt.value === rating);
|
|
return option?.emoji || "";
|
|
}
|