mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 09:11:13 -05:00
Fix unit conversion logic
This commit is contained in:
@@ -10,7 +10,8 @@ import {
|
||||
convertValueToMetric,
|
||||
convertValueFromMetric,
|
||||
detectUnitType,
|
||||
getMetricUnit
|
||||
getMetricUnit,
|
||||
getDisplayUnit
|
||||
} from "@/lib/units";
|
||||
|
||||
interface CoasterStat {
|
||||
@@ -86,7 +87,7 @@ export function CoasterStatsEditor({
|
||||
onChange(newStats);
|
||||
};
|
||||
|
||||
// Get display value (convert from metric if needed)
|
||||
// Get display value (convert from metric to user's preferred units)
|
||||
const getDisplayValue = (stat: CoasterStat): string => {
|
||||
if (!stat.stat_value || !stat.unit) return String(stat.stat_value || '');
|
||||
|
||||
@@ -96,9 +97,12 @@ export function CoasterStatsEditor({
|
||||
const unitType = detectUnitType(stat.unit);
|
||||
if (unitType === 'unknown') return String(stat.stat_value);
|
||||
|
||||
// Assume stored value is in metric, convert to user's preferred units
|
||||
const metricUnit = getMetricUnit(stat.unit);
|
||||
const displayValue = convertValueFromMetric(numValue, stat.unit, metricUnit);
|
||||
// stat.unit is the metric unit (e.g., "km/h")
|
||||
// Get the display unit based on user preference (e.g., "mph" for imperial)
|
||||
const displayUnit = getDisplayUnit(stat.unit, preferences.measurement_system);
|
||||
|
||||
// Convert from metric to display unit
|
||||
const displayValue = convertValueFromMetric(numValue, displayUnit, stat.unit);
|
||||
|
||||
return String(displayValue);
|
||||
};
|
||||
@@ -157,24 +161,25 @@ export function CoasterStatsEditor({
|
||||
value={getDisplayValue(stat)}
|
||||
onChange={(e) => {
|
||||
const inputValue = e.target.value;
|
||||
// If unit is recognized, convert to metric for storage
|
||||
if (stat.unit) {
|
||||
const numValue = parseFloat(inputValue);
|
||||
if (!isNaN(numValue)) {
|
||||
const metricValue = convertValueToMetric(numValue, stat.unit);
|
||||
updateStat(index, 'stat_value', metricValue);
|
||||
} else {
|
||||
updateStat(index, 'stat_value', 0);
|
||||
}
|
||||
const numValue = parseFloat(inputValue);
|
||||
|
||||
if (!isNaN(numValue) && stat.unit) {
|
||||
// Determine what unit the user is entering (based on their preference)
|
||||
const displayUnit = getDisplayUnit(stat.unit, preferences.measurement_system);
|
||||
// Convert from user's input unit to metric for storage
|
||||
const metricValue = convertValueToMetric(numValue, displayUnit);
|
||||
updateStat(index, 'stat_value', metricValue);
|
||||
} else {
|
||||
updateStat(index, 'stat_value', parseFloat(inputValue) || 0);
|
||||
updateStat(index, 'stat_value', numValue || 0);
|
||||
}
|
||||
}}
|
||||
placeholder="0"
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground mt-1">
|
||||
Using {preferences.measurement_system} units
|
||||
</p>
|
||||
{stat.unit && detectUnitType(stat.unit) !== 'unknown' && (
|
||||
<p className="text-xs text-muted-foreground mt-1">
|
||||
Enter in {getDisplayUnit(stat.unit, preferences.measurement_system)}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
<Label className="text-xs">Unit</Label>
|
||||
|
||||
@@ -9,7 +9,8 @@ import {
|
||||
convertValueToMetric,
|
||||
convertValueFromMetric,
|
||||
detectUnitType,
|
||||
getMetricUnit
|
||||
getMetricUnit,
|
||||
getDisplayUnit
|
||||
} from "@/lib/units";
|
||||
|
||||
interface TechnicalSpec {
|
||||
@@ -65,7 +66,7 @@ export function TechnicalSpecsEditor({
|
||||
onChange(newSpecs);
|
||||
};
|
||||
|
||||
// Get display value (convert from metric if needed)
|
||||
// Get display value (convert from metric to user's preferred units)
|
||||
const getDisplayValue = (spec: TechnicalSpec): string => {
|
||||
if (!spec.spec_value || !spec.unit || spec.spec_type !== 'number') return spec.spec_value;
|
||||
|
||||
@@ -75,9 +76,12 @@ export function TechnicalSpecsEditor({
|
||||
const unitType = detectUnitType(spec.unit);
|
||||
if (unitType === 'unknown') return spec.spec_value;
|
||||
|
||||
// Assume stored value is in metric, convert to user's preferred units
|
||||
const metricUnit = getMetricUnit(spec.unit);
|
||||
const displayValue = convertValueFromMetric(numValue, spec.unit, metricUnit);
|
||||
// spec.unit is the metric unit (e.g., "km/h")
|
||||
// Get the display unit based on user preference (e.g., "mph" for imperial)
|
||||
const displayUnit = getDisplayUnit(spec.unit, preferences.measurement_system);
|
||||
|
||||
// Convert from metric to display unit
|
||||
const displayValue = convertValueFromMetric(numValue, displayUnit, spec.unit);
|
||||
|
||||
return String(displayValue);
|
||||
};
|
||||
@@ -135,15 +139,15 @@ export function TechnicalSpecsEditor({
|
||||
value={getDisplayValue(spec)}
|
||||
onChange={(e) => {
|
||||
const inputValue = e.target.value;
|
||||
const numValue = parseFloat(inputValue);
|
||||
|
||||
// If type is number and unit is recognized, convert to metric for storage
|
||||
if (spec.spec_type === 'number' && spec.unit) {
|
||||
const numValue = parseFloat(inputValue);
|
||||
if (!isNaN(numValue)) {
|
||||
const metricValue = convertValueToMetric(numValue, spec.unit);
|
||||
updateSpec(index, 'spec_value', String(metricValue));
|
||||
} else {
|
||||
updateSpec(index, 'spec_value', inputValue);
|
||||
}
|
||||
if (spec.spec_type === 'number' && spec.unit && !isNaN(numValue)) {
|
||||
// Determine what unit the user is entering (based on their preference)
|
||||
const displayUnit = getDisplayUnit(spec.unit, preferences.measurement_system);
|
||||
// Convert from user's input unit to metric for storage
|
||||
const metricValue = convertValueToMetric(numValue, displayUnit);
|
||||
updateSpec(index, 'spec_value', String(metricValue));
|
||||
} else {
|
||||
updateSpec(index, 'spec_value', inputValue);
|
||||
}
|
||||
@@ -151,9 +155,9 @@ export function TechnicalSpecsEditor({
|
||||
placeholder="Value"
|
||||
type={spec.spec_type === 'number' ? 'number' : 'text'}
|
||||
/>
|
||||
{spec.spec_type === 'number' && spec.unit && (
|
||||
{spec.spec_type === 'number' && spec.unit && detectUnitType(spec.unit) !== 'unknown' && (
|
||||
<p className="text-xs text-muted-foreground mt-1">
|
||||
Using {preferences.measurement_system} units
|
||||
Enter in {getDisplayUnit(spec.unit, preferences.measurement_system)}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user