Added comprehensive localization to tracking and settings pages: **Translation Keys Added:** - Sleep: locations, status, duration formatting, success/delete messages - Diaper: conditions, rash severity and alert, success/delete messages - Activity: activity types, form labels, placeholders - Settings: profile, preferences, notifications, appearance, account actions - Common: shared labels (selectChild, noChildrenAdded, etc.) **Pages Localized:** 1. Sleep tracking page (/app/track/sleep/page.tsx) - All form labels and dropdowns - Location options (crib, bed, stroller, carrier, other) - Sleep status (completed/ongoing) - Duration display with interpolation - Success and delete messages 2. Diaper tracking page (/app/track/diaper/page.tsx) - Diaper types (wet, dirty, both, dry) - Conditions (normal, soft, hard, watery, mucus, blood) - Rash detection with severity levels - Alert message for diaper rash - Recent diapers display with translated labels 3. Activity tracking page (/app/track/activity/page.tsx) - Activity types (play, walk, music, reading, tummy time, outdoor, other) - Duration and description fields - Form placeholders - Recent activities display 4. Settings page (/app/settings/page.tsx) - Profile information section - Preferences, notifications, appearance sections - Account actions (logout) - Save/saving button states - Success message All pages now support multi-language translation and are ready for Spanish, French, Portuguese, and Chinese translations. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
528 lines
18 KiB
TypeScript
528 lines
18 KiB
TypeScript
'use client';
|
|
|
|
import { useState, useEffect } from 'react';
|
|
import {
|
|
Box,
|
|
Typography,
|
|
Button,
|
|
Paper,
|
|
TextField,
|
|
FormControl,
|
|
InputLabel,
|
|
Select,
|
|
MenuItem,
|
|
IconButton,
|
|
Alert,
|
|
CircularProgress,
|
|
Card,
|
|
CardContent,
|
|
Dialog,
|
|
DialogTitle,
|
|
DialogContent,
|
|
DialogContentText,
|
|
DialogActions,
|
|
Chip,
|
|
Snackbar,
|
|
} from '@mui/material';
|
|
import {
|
|
ArrowBack,
|
|
Save,
|
|
ChildCare,
|
|
Delete,
|
|
Refresh,
|
|
Add,
|
|
DirectionsWalk,
|
|
Toys,
|
|
MusicNote,
|
|
} from '@mui/icons-material';
|
|
import { useRouter } from 'next/navigation';
|
|
import { AppShell } from '@/components/layouts/AppShell/AppShell';
|
|
import { ProtectedRoute } from '@/components/common/ProtectedRoute';
|
|
import { withErrorBoundary } from '@/components/common/ErrorFallbacks';
|
|
import { useAuth } from '@/lib/auth/AuthContext';
|
|
import { trackingApi, Activity } from '@/lib/api/tracking';
|
|
import { childrenApi, Child } from '@/lib/api/children';
|
|
import { VoiceInputButton } from '@/components/voice/VoiceInputButton';
|
|
import { FormSkeleton, ActivityListSkeleton } from '@/components/common/LoadingSkeletons';
|
|
import { motion } from 'framer-motion';
|
|
import { useLocalizedDate } from '@/hooks/useLocalizedDate';
|
|
import { useTranslation } from '@/hooks/useTranslation';
|
|
|
|
interface ActivityData {
|
|
activityType: string;
|
|
duration?: number;
|
|
description?: string;
|
|
}
|
|
|
|
function ActivityTrackPage() {
|
|
const router = useRouter();
|
|
const { user } = useAuth();
|
|
const { formatDistanceToNow } = useLocalizedDate();
|
|
const { t } = useTranslation('tracking');
|
|
const [children, setChildren] = useState<Child[]>([]);
|
|
const [selectedChild, setSelectedChild] = useState<string>('');
|
|
|
|
// Activity state
|
|
const [activityType, setActivityType] = useState<string>('play');
|
|
const [duration, setDuration] = useState<string>('');
|
|
const [description, setDescription] = useState<string>('');
|
|
|
|
// Common state
|
|
const [notes, setNotes] = useState<string>('');
|
|
const [recentActivities, setRecentActivities] = useState<Activity[]>([]);
|
|
const [loading, setLoading] = useState(false);
|
|
const [childrenLoading, setChildrenLoading] = useState(true);
|
|
const [activitiesLoading, setActivitiesLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [successMessage, setSuccessMessage] = useState<string | null>(null);
|
|
|
|
// Delete confirmation dialog
|
|
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
|
const [activityToDelete, setActivityToDelete] = useState<string | null>(null);
|
|
|
|
const familyId = user?.families?.[0]?.familyId;
|
|
|
|
// Load children
|
|
useEffect(() => {
|
|
if (familyId) {
|
|
loadChildren();
|
|
}
|
|
}, [familyId]);
|
|
|
|
// Load recent activities when child is selected
|
|
useEffect(() => {
|
|
if (selectedChild) {
|
|
loadRecentActivities();
|
|
}
|
|
}, [selectedChild]);
|
|
|
|
const loadChildren = async () => {
|
|
if (!familyId) return;
|
|
|
|
try {
|
|
setChildrenLoading(true);
|
|
const childrenData = await childrenApi.getChildren(familyId);
|
|
setChildren(childrenData);
|
|
if (childrenData.length > 0) {
|
|
setSelectedChild(childrenData[0].id);
|
|
}
|
|
} catch (err: any) {
|
|
console.error('Failed to load children:', err);
|
|
setError(err.response?.data?.message || 'Failed to load children');
|
|
} finally {
|
|
setChildrenLoading(false);
|
|
}
|
|
};
|
|
|
|
const loadRecentActivities = async () => {
|
|
if (!selectedChild) return;
|
|
|
|
try {
|
|
setActivitiesLoading(true);
|
|
const activities = await trackingApi.getActivities(selectedChild, 'activity');
|
|
// Sort by timestamp descending and take last 10
|
|
const sorted = activities.sort((a, b) =>
|
|
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()
|
|
).slice(0, 10);
|
|
setRecentActivities(sorted);
|
|
} catch (err: any) {
|
|
console.error('Failed to load recent activities:', err);
|
|
} finally {
|
|
setActivitiesLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleSubmit = async () => {
|
|
if (!selectedChild) {
|
|
setError('Please select a child');
|
|
return;
|
|
}
|
|
|
|
// Validation
|
|
if (!activityType) {
|
|
setError('Please select activity type');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
const data: ActivityData = {
|
|
activityType,
|
|
duration: duration ? parseInt(duration) : undefined,
|
|
description: description || undefined,
|
|
};
|
|
|
|
await trackingApi.createActivity(selectedChild, {
|
|
type: 'activity',
|
|
timestamp: new Date().toISOString(),
|
|
data,
|
|
notes: notes || undefined,
|
|
});
|
|
|
|
setSuccessMessage(t('activity.success'));
|
|
|
|
// Reset form
|
|
resetForm();
|
|
|
|
// Reload recent activities
|
|
await loadRecentActivities();
|
|
} catch (err: any) {
|
|
console.error('Failed to save activity:', err);
|
|
setError(err.response?.data?.message || 'Failed to save activity');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const resetForm = () => {
|
|
setActivityType('play');
|
|
setDuration('');
|
|
setDescription('');
|
|
setNotes('');
|
|
};
|
|
|
|
const handleDeleteClick = (activityId: string) => {
|
|
setActivityToDelete(activityId);
|
|
setDeleteDialogOpen(true);
|
|
};
|
|
|
|
const handleDeleteConfirm = async () => {
|
|
if (!activityToDelete) return;
|
|
|
|
try {
|
|
setLoading(true);
|
|
await trackingApi.deleteActivity(activityToDelete);
|
|
setSuccessMessage(t('activity.deleted'));
|
|
setDeleteDialogOpen(false);
|
|
setActivityToDelete(null);
|
|
await loadRecentActivities();
|
|
} catch (err: any) {
|
|
console.error('Failed to delete activity:', err);
|
|
setError(err.response?.data?.message || 'Failed to delete activity');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const getActivityTypeIcon = (type: string) => {
|
|
switch (type) {
|
|
case 'play':
|
|
return <Toys />;
|
|
case 'walk':
|
|
return <DirectionsWalk />;
|
|
case 'music':
|
|
return <MusicNote />;
|
|
default:
|
|
return <ChildCare />;
|
|
}
|
|
};
|
|
|
|
const getActivityDetails = (activity: Activity) => {
|
|
const data = activity.data as ActivityData;
|
|
let details = data.activityType.charAt(0).toUpperCase() + data.activityType.slice(1);
|
|
if (data.duration) {
|
|
details += ` - ${data.duration} min`;
|
|
}
|
|
if (data.description) {
|
|
details += ` - ${data.description}`;
|
|
}
|
|
return details;
|
|
};
|
|
|
|
if (childrenLoading) {
|
|
return (
|
|
<ProtectedRoute>
|
|
<AppShell>
|
|
<Box>
|
|
<Typography variant="h4" fontWeight="600" sx={{ mb: 3 }}>
|
|
{t('trackActivity')}
|
|
</Typography>
|
|
<Paper sx={{ p: 3, mb: 3 }}>
|
|
<FormSkeleton />
|
|
</Paper>
|
|
<Typography variant="h6" fontWeight="600" sx={{ mb: 2 }}>
|
|
{t('activity.recentActivities')}
|
|
</Typography>
|
|
<ActivityListSkeleton count={3} />
|
|
</Box>
|
|
</AppShell>
|
|
</ProtectedRoute>
|
|
);
|
|
}
|
|
|
|
if (!familyId || children.length === 0) {
|
|
return (
|
|
<ProtectedRoute>
|
|
<AppShell>
|
|
<Card>
|
|
<CardContent sx={{ textAlign: 'center', py: 8 }}>
|
|
<ChildCare sx={{ fontSize: 64, color: 'text.secondary', mb: 2 }} />
|
|
<Typography variant="h6" color="text.secondary" gutterBottom>
|
|
{t('common.noChildrenAdded')}
|
|
</Typography>
|
|
<Typography variant="body2" color="text.secondary" sx={{ mb: 3 }}>
|
|
{t('common.noChildrenMessage')}
|
|
</Typography>
|
|
<Button
|
|
variant="contained"
|
|
startIcon={<Add />}
|
|
onClick={() => router.push('/children')}
|
|
>
|
|
{t('common.addChild')}
|
|
</Button>
|
|
</CardContent>
|
|
</Card>
|
|
</AppShell>
|
|
</ProtectedRoute>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<ProtectedRoute>
|
|
<AppShell>
|
|
<Box>
|
|
<Box sx={{ display: 'flex', alignItems: 'center', mb: 3 }}>
|
|
<IconButton onClick={() => router.back()} sx={{ mr: 2 }}>
|
|
<ArrowBack />
|
|
</IconButton>
|
|
<Typography variant="h4" fontWeight="600" sx={{ flex: 1 }}>
|
|
{t('trackActivity')}
|
|
</Typography>
|
|
<VoiceInputButton
|
|
onTranscript={(transcript) => {
|
|
console.log('[Activity] Voice transcript:', transcript);
|
|
}}
|
|
onClassifiedIntent={(result) => {
|
|
if (result.intent === 'activity' && result.structuredData) {
|
|
const data = result.structuredData;
|
|
// Auto-fill form with voice data
|
|
if (data.activityType) setActivityType(data.activityType);
|
|
if (data.duration) setDuration(data.duration.toString());
|
|
if (data.description) setDescription(data.description);
|
|
}
|
|
}}
|
|
size="medium"
|
|
/>
|
|
</Box>
|
|
|
|
{error && (
|
|
<Alert severity="error" sx={{ mb: 3 }} onClose={() => setError(null)}>
|
|
{error}
|
|
</Alert>
|
|
)}
|
|
|
|
<motion.div
|
|
initial={{ opacity: 0, y: 20 }}
|
|
animate={{ opacity: 1, y: 0 }}
|
|
transition={{ duration: 0.3 }}
|
|
>
|
|
{/* Child Selector */}
|
|
{children.length > 1 && (
|
|
<Paper sx={{ p: 2, mb: 3 }}>
|
|
<FormControl fullWidth>
|
|
<InputLabel>{t('common.selectChild')}</InputLabel>
|
|
<Select
|
|
value={selectedChild}
|
|
onChange={(e) => setSelectedChild(e.target.value)}
|
|
label={t('common.selectChild')}
|
|
>
|
|
{children.map((child) => (
|
|
<MenuItem key={child.id} value={child.id}>
|
|
{child.name}
|
|
</MenuItem>
|
|
))}
|
|
</Select>
|
|
</FormControl>
|
|
</Paper>
|
|
)}
|
|
|
|
{/* Main Form */}
|
|
<Paper sx={{ p: 3, mb: 3 }}>
|
|
<Box sx={{ display: 'flex', alignItems: 'center', mb: 3 }}>
|
|
<ChildCare sx={{ fontSize: 36, color: 'success.main', mr: 2 }} />
|
|
<Typography variant="h6" fontWeight="600">
|
|
{t('activity.title')}
|
|
</Typography>
|
|
</Box>
|
|
|
|
<FormControl fullWidth sx={{ mb: 3 }}>
|
|
<InputLabel>{t('activity.type')}</InputLabel>
|
|
<Select
|
|
value={activityType}
|
|
onChange={(e) => setActivityType(e.target.value)}
|
|
label={t('activity.type')}
|
|
>
|
|
<MenuItem value="play">{t('activity.types.play')}</MenuItem>
|
|
<MenuItem value="walk">{t('activity.types.walk')}</MenuItem>
|
|
<MenuItem value="music">{t('activity.types.music')}</MenuItem>
|
|
<MenuItem value="reading">{t('activity.types.reading')}</MenuItem>
|
|
<MenuItem value="tummy_time">{t('activity.types.tummyTime')}</MenuItem>
|
|
<MenuItem value="outdoor">{t('activity.types.outdoor')}</MenuItem>
|
|
<MenuItem value="other">{t('activity.types.other')}</MenuItem>
|
|
</Select>
|
|
</FormControl>
|
|
|
|
<TextField
|
|
fullWidth
|
|
label={t('activity.duration')}
|
|
type="number"
|
|
value={duration}
|
|
onChange={(e) => setDuration(e.target.value)}
|
|
sx={{ mb: 3 }}
|
|
placeholder={t('activity.placeholders.duration')}
|
|
/>
|
|
|
|
<TextField
|
|
fullWidth
|
|
label={t('activity.description')}
|
|
value={description}
|
|
onChange={(e) => setDescription(e.target.value)}
|
|
sx={{ mb: 3 }}
|
|
placeholder={t('activity.placeholders.description')}
|
|
/>
|
|
|
|
<TextField
|
|
fullWidth
|
|
label={t('activity.notes')}
|
|
multiline
|
|
rows={3}
|
|
value={notes}
|
|
onChange={(e) => setNotes(e.target.value)}
|
|
sx={{ mb: 3 }}
|
|
placeholder={t('activity.placeholders.notes')}
|
|
/>
|
|
|
|
<Button
|
|
fullWidth
|
|
type="button"
|
|
variant="contained"
|
|
size="large"
|
|
startIcon={<Save />}
|
|
onClick={handleSubmit}
|
|
disabled={loading}
|
|
>
|
|
{loading ? t('common.loading') : t('activity.logActivity')}
|
|
</Button>
|
|
</Paper>
|
|
|
|
{/* Recent Activities */}
|
|
<Paper sx={{ p: 3 }}>
|
|
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
|
|
<Typography variant="h6" fontWeight="600">
|
|
{t('activity.recentActivities')}
|
|
</Typography>
|
|
<IconButton onClick={loadRecentActivities} disabled={activitiesLoading}>
|
|
<Refresh />
|
|
</IconButton>
|
|
</Box>
|
|
|
|
{activitiesLoading ? (
|
|
<Box sx={{ display: 'flex', justifyContent: 'center', py: 4 }}>
|
|
<CircularProgress size={30} />
|
|
</Box>
|
|
) : recentActivities.length === 0 ? (
|
|
<Box sx={{ textAlign: 'center', py: 4 }}>
|
|
<Typography variant="body2" color="text.secondary">
|
|
{t('noEntries')}
|
|
</Typography>
|
|
</Box>
|
|
) : (
|
|
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
|
|
{recentActivities.map((activity, index) => {
|
|
const data = activity.data as ActivityData;
|
|
if (!data || !data.activityType) {
|
|
console.warn('[Activity] Activity missing activityType:', activity);
|
|
return null;
|
|
}
|
|
return (
|
|
<motion.div
|
|
key={activity.id}
|
|
initial={{ opacity: 0, y: 10 }}
|
|
animate={{ opacity: 1, y: 0 }}
|
|
transition={{ duration: 0.2, delay: index * 0.05 }}
|
|
>
|
|
<Card variant="outlined">
|
|
<CardContent>
|
|
<Box sx={{ display: 'flex', alignItems: 'flex-start', gap: 2 }}>
|
|
<Box sx={{ mt: 0.5 }}>
|
|
{getActivityTypeIcon(data.activityType)}
|
|
</Box>
|
|
<Box sx={{ flex: 1 }}>
|
|
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1, mb: 0.5 }}>
|
|
<Typography variant="body1" fontWeight="600">
|
|
{data.activityType.charAt(0).toUpperCase() + data.activityType.slice(1).replace('_', ' ')}
|
|
</Typography>
|
|
<Chip
|
|
label={formatDistanceToNow(new Date(activity.timestamp), { addSuffix: true })}
|
|
size="small"
|
|
variant="outlined"
|
|
/>
|
|
</Box>
|
|
<Typography variant="body2" color="text.secondary" sx={{ mb: 1 }}>
|
|
{getActivityDetails(activity)}
|
|
</Typography>
|
|
{activity.notes && (
|
|
<Typography variant="body2" color="text.secondary" sx={{ fontStyle: 'italic' }}>
|
|
{activity.notes}
|
|
</Typography>
|
|
)}
|
|
</Box>
|
|
<Box sx={{ display: 'flex', gap: 0.5 }}>
|
|
<IconButton
|
|
size="small"
|
|
color="error"
|
|
onClick={() => handleDeleteClick(activity.id)}
|
|
disabled={loading}
|
|
>
|
|
<Delete />
|
|
</IconButton>
|
|
</Box>
|
|
</Box>
|
|
</CardContent>
|
|
</Card>
|
|
</motion.div>
|
|
);
|
|
})}
|
|
</Box>
|
|
)}
|
|
</Paper>
|
|
</motion.div>
|
|
</Box>
|
|
|
|
{/* Delete Confirmation Dialog */}
|
|
<Dialog
|
|
open={deleteDialogOpen}
|
|
onClose={() => setDeleteDialogOpen(false)}
|
|
>
|
|
<DialogTitle>{t('common.delete')} {t('activity.title')}?</DialogTitle>
|
|
<DialogContent>
|
|
<DialogContentText>
|
|
{t('confirmDelete')}
|
|
</DialogContentText>
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<Button onClick={() => setDeleteDialogOpen(false)} disabled={loading}>
|
|
{t('common.cancel')}
|
|
</Button>
|
|
<Button onClick={handleDeleteConfirm} color="error" disabled={loading}>
|
|
{loading ? 'Deleting...' : t('common.delete')}
|
|
</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
|
|
{/* Success Snackbar */}
|
|
<Snackbar
|
|
open={!!successMessage}
|
|
autoHideDuration={3000}
|
|
onClose={() => setSuccessMessage(null)}
|
|
message={successMessage}
|
|
/>
|
|
</AppShell>
|
|
</ProtectedRoute>
|
|
);
|
|
}
|
|
|
|
export default withErrorBoundary(ActivityTrackPage, 'form');
|