Files
maternal-app/maternal-web/components/forms/UnitInput.tsx
Andrei 9d66b58f20
Some checks failed
CI/CD Pipeline / Build Application (push) Has been cancelled
CI/CD Pipeline / Lint and Test (push) Has been cancelled
CI/CD Pipeline / E2E Tests (push) Has been cancelled
fix: Connect measurement unit preference to backend storage
Fixed measurement unit not persisting across page refreshes:

- Settings page now includes measurementUnit in the preferences object when saving
- MeasurementUnitSelector now accepts value/onChange props for controlled usage
- Settings state properly loads and saves measurementUnit from user preferences
- UnitInput component will now correctly read imperial/metric from user.preferences.measurementUnit

Previously, measurementUnit was only saved to localStorage but not synced to backend,
causing UnitInput to always default to metric since it reads from user.preferences.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-03 12:57:25 +00:00

117 lines
3.4 KiB
TypeScript

'use client';
import { TextField, InputAdornment, TextFieldProps } from '@mui/material';
import { useAuth } from '@/lib/auth/AuthContext';
import {
convertVolume,
convertVolumeToMl,
convertWeight,
convertWeightToKg,
convertHeight,
convertHeightToCm,
getUnitSymbol
} from '@/lib/utils/unitConversion';
import { MeasurementSystem } from '@/hooks/useLocale';
type UnitType = 'volume' | 'weight' | 'height';
interface UnitInputProps extends Omit<TextFieldProps, 'onChange' | 'value'> {
/**
* The type of measurement (volume, weight, or height)
*/
type: UnitType;
/**
* The value in metric units (ml, kg, or cm)
*/
value: number | string;
/**
* Callback when value changes, always returns metric value
*/
onChange: (metricValue: number) => void;
}
/**
* UnitInput - A text field that automatically handles unit conversions
*
* Storage: Always stores values in metric (ml, kg, cm) in the database
* Display: Shows values in user's preferred measurement system
* Input: Accepts values in user's preferred units, converts to metric
*/
export function UnitInput({ type, value, onChange, ...textFieldProps }: UnitInputProps) {
const { user } = useAuth();
const measurementSystem: MeasurementSystem =
(user?.preferences?.measurementUnit as MeasurementSystem) || 'metric';
// Debug logging
console.log('[UnitInput] Measurement system:', measurementSystem, 'User preferences:', user?.preferences);
// Get the display unit symbol
const unitSymbol = getUnitSymbol(type, measurementSystem);
// Convert metric value to display value
const getDisplayValue = (metricValue: number | string): number => {
const numericValue = typeof metricValue === 'string' ? parseFloat(metricValue) : metricValue;
if (isNaN(numericValue)) return 0;
switch (type) {
case 'volume':
return convertVolume(numericValue, measurementSystem).value;
case 'weight':
return convertWeight(numericValue, measurementSystem).value;
case 'height':
return convertHeight(numericValue, measurementSystem).value;
default:
return numericValue;
}
};
// Convert display value back to metric
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const displayValue = parseFloat(event.target.value);
if (isNaN(displayValue)) {
onChange(0);
return;
}
let metricValue: number;
switch (type) {
case 'volume':
metricValue = convertVolumeToMl(displayValue, measurementSystem);
break;
case 'weight':
metricValue = convertWeightToKg(displayValue, measurementSystem);
break;
case 'height':
metricValue = convertHeightToCm(displayValue, measurementSystem);
break;
default:
metricValue = displayValue;
}
onChange(metricValue);
};
const displayValue = getDisplayValue(value);
const roundedDisplayValue = Math.round(displayValue * 100) / 100; // Round to 2 decimals
return (
<TextField
{...textFieldProps}
type="number"
value={roundedDisplayValue || ''}
onChange={handleChange}
InputProps={{
endAdornment: <InputAdornment position="end">{unitSymbol}</InputAdornment>,
...textFieldProps.InputProps,
}}
inputProps={{
step: type === 'volume' ? '0.1' : '0.01',
min: 0,
...textFieldProps.inputProps,
}}
/>
);
}