Files
maternal-app/maternal-web/components/settings/MeasurementUnitSelector.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

88 lines
2.5 KiB
TypeScript

'use client';
import { useState, useEffect } from 'react';
import {
FormControl,
InputLabel,
Select,
MenuItem,
SelectChangeEvent,
Box,
Typography,
} from '@mui/material';
import { useLocale, MeasurementSystem } from '@/hooks/useLocale';
import { useTranslation } from '@/hooks/useTranslation';
interface MeasurementUnitSelectorProps {
variant?: 'standard' | 'outlined' | 'filled';
fullWidth?: boolean;
showDescription?: boolean;
value?: MeasurementSystem;
onChange?: (value: MeasurementSystem) => void;
}
/**
* Measurement unit selector component for choosing between Metric and Imperial systems
*/
export function MeasurementUnitSelector({
variant = 'outlined',
fullWidth = true,
showDescription = true,
value,
onChange,
}: MeasurementUnitSelectorProps) {
const { measurementSystem, setMeasurementSystem } = useLocale();
const { t } = useTranslation('settings');
// Use controlled value if provided, otherwise use hook value
const selectedSystem = value !== undefined ? value : measurementSystem;
const handleSystemChange = (event: SelectChangeEvent) => {
const newSystem = event.target.value as MeasurementSystem;
// If onChange is provided (controlled), use it
if (onChange) {
onChange(newSystem);
} else {
// Otherwise use the hook (uncontrolled)
setMeasurementSystem(newSystem);
}
};
return (
<FormControl variant={variant} fullWidth={fullWidth}>
<InputLabel id="measurement-unit-selector-label">
{t('preferences.measurementUnits')}
</InputLabel>
<Select
labelId="measurement-unit-selector-label"
id="measurement-unit-selector"
value={selectedSystem}
label={t('preferences.measurementUnits')}
onChange={handleSystemChange}
>
<MenuItem value="metric">
<Box>
<Typography variant="body1">{t('preferences.metric')}</Typography>
{showDescription && (
<Typography variant="caption" color="text.secondary">
kg, cm, °C, ml
</Typography>
)}
</Box>
</MenuItem>
<MenuItem value="imperial">
<Box>
<Typography variant="body1">{t('preferences.imperial')}</Typography>
{showDescription && (
<Typography variant="caption" color="text.secondary">
lb, in, °F, oz
</Typography>
)}
</Box>
</MenuItem>
</Select>
</FormControl>
);
}