Refactor: Implement type safety plan

This commit is contained in:
gpt-engineer-app[bot]
2025-10-17 14:35:15 +00:00
parent 921abb63a1
commit 0db54b402b
12 changed files with 290 additions and 38 deletions

View File

@@ -6,6 +6,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@
import { Textarea } from "@/components/ui/textarea";
import { Card } from "@/components/ui/card";
import { useUnitPreferences } from "@/hooks/useUnitPreferences";
import { toast } from "sonner";
import {
convertValueToMetric,
convertValueFromMetric,
@@ -13,6 +14,7 @@ import {
getMetricUnit,
getDisplayUnit
} from "@/lib/units";
import { validateMetricUnit } from "@/lib/unitValidation";
interface CoasterStat {
stat_name: string;
@@ -83,7 +85,20 @@ export function CoasterStatsEditor({
const updateStat = (index: number, field: keyof CoasterStat, value: any) => {
const newStats = [...stats];
newStats[index] = { ...newStats[index], [field]: value };
// Ensure unit is metric when updating unit field
if (field === 'unit' && value) {
try {
validateMetricUnit(value, 'Unit');
newStats[index] = { ...newStats[index], [field]: value };
} catch (error) {
toast.error(`Invalid unit: ${value}. Please use metric units only (km/h, m, cm, kg, G, etc.)`);
return;
}
} else {
newStats[index] = { ...newStats[index], [field]: value };
}
onChange(newStats);
};

View File

@@ -5,6 +5,7 @@ import { Label } from "@/components/ui/label";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Card } from "@/components/ui/card";
import { useUnitPreferences } from "@/hooks/useUnitPreferences";
import { toast } from "sonner";
import {
convertValueToMetric,
convertValueFromMetric,
@@ -12,6 +13,7 @@ import {
getMetricUnit,
getDisplayUnit
} from "@/lib/units";
import { validateMetricUnit } from "@/lib/unitValidation";
interface TechnicalSpec {
spec_name: string;
@@ -62,7 +64,20 @@ export function TechnicalSpecsEditor({
const updateSpec = (index: number, field: keyof TechnicalSpec, value: any) => {
const newSpecs = [...specs];
newSpecs[index] = { ...newSpecs[index], [field]: value };
// Ensure unit is metric when updating unit field
if (field === 'unit' && value) {
try {
validateMetricUnit(value, 'Unit');
newSpecs[index] = { ...newSpecs[index], [field]: value };
} catch (error) {
toast.error(`Invalid unit: ${value}. Please use metric units only (m, km/h, cm, kg, celsius, etc.)`);
return;
}
} else {
newSpecs[index] = { ...newSpecs[index], [field]: value };
}
onChange(newSpecs);
};