Files
maternal-app/maternal-web/components/children/ChildDialog.tsx
Andrei b56f9546c2 feat: Complete high-priority i18n localization with date/time support
This commit implements comprehensive localization for high-priority components:

## Tracking Pages (4 files)
- Localized feeding, sleep, diaper, and medicine tracking pages
- Replaced hardcoded strings with translation keys from tracking namespace
- Added useTranslation hook integration
- All form labels, buttons, and messages now support multiple languages

## Child Dialog Components (2 files)
- Localized ChildDialog (add/edit child form)
- Localized DeleteConfirmDialog
- Added new translation keys to children.json for dialog content
- Includes validation messages and action buttons

## Date/Time Localization (14 files + new hook)
- Created useLocalizedDate hook wrapping date-fns with locale support
- Supports 5 languages: English, Spanish, French, Portuguese, Chinese
- Updated all date formatting across:
  * Tracking pages (feeding, sleep, diaper, medicine)
  * Activity pages (activities, history, track activity)
  * Settings components (sessions, biometric, device trust)
  * Analytics components (insights, growth, sleep chart, feeding graph)
- Date displays automatically adapt to user's language (e.g., "2 hours ago" → "hace 2 horas")

## Translation Updates
- Enhanced children.json with dialog section containing:
  * Form field labels (name, birthDate, gender, photoUrl)
  * Action buttons (add, update, delete, cancel, saving, deleting)
  * Delete confirmation messages
  * Validation error messages

Files changed: 17 files (+164, -113)
Languages supported: en, es, fr, pt-BR, zh-CN

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-03 11:49:48 +00:00

170 lines
4.4 KiB
TypeScript

'use client';
import { useState, useEffect } from 'react';
import {
Dialog,
DialogTitle,
DialogContent,
DialogActions,
Button,
TextField,
MenuItem,
Box,
Alert,
} from '@mui/material';
import { Child, CreateChildData } from '@/lib/api/children';
import { useTranslation } from '@/hooks/useTranslation';
interface ChildDialogProps {
open: boolean;
onClose: () => void;
onSubmit: (data: CreateChildData) => Promise<void>;
child?: Child | null;
isLoading?: boolean;
}
export function ChildDialog({ open, onClose, onSubmit, child, isLoading = false }: ChildDialogProps) {
const { t } = useTranslation('children');
const [formData, setFormData] = useState<CreateChildData>({
name: '',
birthDate: '',
gender: 'male',
photoUrl: '',
});
const [error, setError] = useState<string>('');
useEffect(() => {
if (child) {
setFormData({
name: child.name,
birthDate: child.birthDate.split('T')[0], // Convert to YYYY-MM-DD format
gender: child.gender,
photoUrl: child.photoUrl || '',
});
} else {
setFormData({
name: '',
birthDate: '',
gender: 'male',
photoUrl: '',
});
}
setError('');
}, [child, open]);
const handleChange = (field: keyof CreateChildData) => (
e: React.ChangeEvent<HTMLInputElement>
) => {
setFormData({ ...formData, [field]: e.target.value });
};
const handleSubmit = async () => {
setError('');
// Validation
if (!formData.name.trim()) {
setError(t('dialog.validation.nameRequired'));
return;
}
if (!formData.birthDate) {
setError(t('dialog.validation.birthDateRequired'));
return;
}
// Check if birth date is in the future
const selectedDate = new Date(formData.birthDate);
const today = new Date();
today.setHours(0, 0, 0, 0);
if (selectedDate > today) {
setError(t('dialog.validation.birthDateFuture'));
return;
}
try {
await onSubmit(formData);
onClose();
} catch (err: any) {
setError(err.message || t('errors.saveFailed'));
}
};
return (
<Dialog
open={open}
onClose={onClose}
maxWidth="sm"
fullWidth
aria-labelledby="child-dialog-title"
aria-describedby="child-dialog-description"
>
<DialogTitle id="child-dialog-title">{child ? t('editChild') : t('addChild')}</DialogTitle>
<DialogContent>
<Box
id="child-dialog-description"
sx={{ pt: 2, display: 'flex', flexDirection: 'column', gap: 2 }}
>
{error && (
<Alert severity="error" onClose={() => setError('')} role="alert">
{error}
</Alert>
)}
<TextField
label={t('dialog.name')}
value={formData.name}
onChange={handleChange('name')}
fullWidth
required
autoFocus
disabled={isLoading}
/>
<TextField
label={t('dialog.birthDate')}
type="date"
value={formData.birthDate}
onChange={handleChange('birthDate')}
fullWidth
required
InputLabelProps={{
shrink: true,
}}
disabled={isLoading}
/>
<TextField
label={t('dialog.gender')}
value={formData.gender}
onChange={handleChange('gender')}
fullWidth
required
select
disabled={isLoading}
>
<MenuItem value="male">{t('gender.male')}</MenuItem>
<MenuItem value="female">{t('gender.female')}</MenuItem>
<MenuItem value="other">{t('gender.other')}</MenuItem>
</TextField>
<TextField
label={t('dialog.photoUrl')}
value={formData.photoUrl}
onChange={handleChange('photoUrl')}
fullWidth
placeholder={t('dialog.photoPlaceholder')}
disabled={isLoading}
/>
</Box>
</DialogContent>
<DialogActions>
<Button onClick={onClose} disabled={isLoading}>
{t('dialog.cancel')}
</Button>
<Button onClick={handleSubmit} variant="contained" disabled={isLoading}>
{isLoading ? t('dialog.saving') : child ? t('dialog.update') : t('dialog.add')}
</Button>
</DialogActions>
</Dialog>
);
}