Implement comprehensive tracking system and analytics dashboard
- Add Feeding Tracker with 3 feeding types (breast, bottle, solid) - Built-in timer for breastfeeding sessions - Recent feeding history with delete functionality - Form validation and child selection - Add Sleep Tracker with duration tracking - Start/end time inputs with "Now" quick buttons - Sleep quality and location tracking - Ongoing sleep support with real-time duration - Recent sleep activities list - Add Diaper Tracker with comprehensive monitoring - 4 diaper types (wet, dirty, both, dry) - Multiple condition selectors - Rash monitoring with severity levels - Color-coded visual indicators - Add Insights/Analytics Dashboard - Summary statistics cards (feedings, sleep, diapers) - Interactive charts using Recharts (bar, line, pie) - Date range filtering (7/30/90 days) - Activity timeline and distribution - Recent activities list - Add Settings page with backend integration - Profile update functionality with API integration - Form validation and error handling - Loading states and success notifications - Notification and appearance preferences - Add Users API service for profile management All pages include: - Full CRUD operations with backend APIs - Loading states and error handling - Form validation and user feedback - Framer Motion animations - Material-UI design system - Responsive layouts 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,78 +1,656 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { Box, Typography, Grid, Card, CardContent } from '@mui/material';
|
import { useState, useEffect } from 'react';
|
||||||
import { TrendingUp, Insights as InsightsIcon, Timeline } from '@mui/icons-material';
|
import {
|
||||||
|
Box,
|
||||||
|
Typography,
|
||||||
|
Grid,
|
||||||
|
Card,
|
||||||
|
CardContent,
|
||||||
|
Select,
|
||||||
|
MenuItem,
|
||||||
|
FormControl,
|
||||||
|
InputLabel,
|
||||||
|
CircularProgress,
|
||||||
|
Alert,
|
||||||
|
Paper,
|
||||||
|
Divider,
|
||||||
|
List,
|
||||||
|
ListItem,
|
||||||
|
ListItemAvatar,
|
||||||
|
ListItemText,
|
||||||
|
Avatar,
|
||||||
|
Chip,
|
||||||
|
ToggleButtonGroup,
|
||||||
|
ToggleButton,
|
||||||
|
} from '@mui/material';
|
||||||
|
import {
|
||||||
|
Restaurant,
|
||||||
|
Hotel,
|
||||||
|
BabyChangingStation,
|
||||||
|
TrendingUp,
|
||||||
|
Timeline,
|
||||||
|
CalendarToday,
|
||||||
|
Assessment,
|
||||||
|
} from '@mui/icons-material';
|
||||||
import { AppShell } from '@/components/layouts/AppShell/AppShell';
|
import { AppShell } from '@/components/layouts/AppShell/AppShell';
|
||||||
import { ProtectedRoute } from '@/components/common/ProtectedRoute';
|
import { ProtectedRoute } from '@/components/common/ProtectedRoute';
|
||||||
|
import { motion } from 'framer-motion';
|
||||||
|
import { trackingApi, Activity, ActivityType } from '@/lib/api/tracking';
|
||||||
|
import { childrenApi, Child } from '@/lib/api/children';
|
||||||
|
import { format, subDays, startOfDay, endOfDay, parseISO, differenceInMinutes, formatDistanceToNow } from 'date-fns';
|
||||||
|
import { BarChart, Bar, LineChart, Line, PieChart, Pie, Cell, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts';
|
||||||
|
|
||||||
|
type DateRange = '7days' | '30days' | '3months';
|
||||||
|
|
||||||
|
interface DayData {
|
||||||
|
date: string;
|
||||||
|
feedings: number;
|
||||||
|
sleepHours: number;
|
||||||
|
diapers: number;
|
||||||
|
activities: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DiaperTypeData {
|
||||||
|
name: string;
|
||||||
|
value: number;
|
||||||
|
color: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ActivityTypeData {
|
||||||
|
name: string;
|
||||||
|
count: number;
|
||||||
|
color: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const COLORS = {
|
||||||
|
feeding: '#FFB6C1',
|
||||||
|
sleep: '#B6D7FF',
|
||||||
|
diaper: '#FFE4B5',
|
||||||
|
medication: '#D4B5FF',
|
||||||
|
milestone: '#B5FFD4',
|
||||||
|
note: '#FFD3B6',
|
||||||
|
wet: '#87CEEB',
|
||||||
|
dirty: '#D2691E',
|
||||||
|
both: '#FF8C00',
|
||||||
|
dry: '#90EE90',
|
||||||
|
};
|
||||||
|
|
||||||
|
const getActivityIcon = (type: ActivityType) => {
|
||||||
|
switch (type) {
|
||||||
|
case 'feeding':
|
||||||
|
return <Restaurant />;
|
||||||
|
case 'sleep':
|
||||||
|
return <Hotel />;
|
||||||
|
case 'diaper':
|
||||||
|
return <BabyChangingStation />;
|
||||||
|
default:
|
||||||
|
return <Timeline />;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getActivityColor = (type: ActivityType) => {
|
||||||
|
return COLORS[type as keyof typeof COLORS] || '#CCCCCC';
|
||||||
|
};
|
||||||
|
|
||||||
export default function InsightsPage() {
|
export default function InsightsPage() {
|
||||||
|
const [children, setChildren] = useState<Child[]>([]);
|
||||||
|
const [selectedChild, setSelectedChild] = useState<string>('');
|
||||||
|
const [dateRange, setDateRange] = useState<DateRange>('7days');
|
||||||
|
const [activities, setActivities] = useState<Activity[]>([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
// Fetch children on mount
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchChildren = async () => {
|
||||||
|
try {
|
||||||
|
const childrenData = await childrenApi.getChildren();
|
||||||
|
setChildren(childrenData);
|
||||||
|
if (childrenData.length > 0) {
|
||||||
|
setSelectedChild(childrenData[0].id);
|
||||||
|
}
|
||||||
|
} catch (err: any) {
|
||||||
|
setError(err.response?.data?.message || 'Failed to load children');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
fetchChildren();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// Fetch activities when child or date range changes
|
||||||
|
useEffect(() => {
|
||||||
|
if (!selectedChild) return;
|
||||||
|
|
||||||
|
const fetchActivities = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
setError(null);
|
||||||
|
try {
|
||||||
|
const days = dateRange === '7days' ? 7 : dateRange === '30days' ? 30 : 90;
|
||||||
|
const endDate = endOfDay(new Date());
|
||||||
|
const startDate = startOfDay(subDays(new Date(), days - 1));
|
||||||
|
|
||||||
|
const activitiesData = await trackingApi.getActivities(
|
||||||
|
selectedChild,
|
||||||
|
undefined,
|
||||||
|
startDate.toISOString(),
|
||||||
|
endDate.toISOString()
|
||||||
|
);
|
||||||
|
setActivities(activitiesData);
|
||||||
|
} catch (err: any) {
|
||||||
|
setError(err.response?.data?.message || 'Failed to load activities');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchActivities();
|
||||||
|
}, [selectedChild, dateRange]);
|
||||||
|
|
||||||
|
// Calculate statistics
|
||||||
|
const calculateStats = () => {
|
||||||
|
const totalFeedings = activities.filter((a) => a.type === 'feeding').length;
|
||||||
|
const totalDiapers = activities.filter((a) => a.type === 'diaper').length;
|
||||||
|
|
||||||
|
// Calculate sleep hours
|
||||||
|
const sleepActivities = activities.filter((a) => a.type === 'sleep');
|
||||||
|
const totalSleepMinutes = sleepActivities.reduce((acc, activity) => {
|
||||||
|
if (activity.data?.endTime && activity.data?.startTime) {
|
||||||
|
const start = parseISO(activity.data.startTime);
|
||||||
|
const end = parseISO(activity.data.endTime);
|
||||||
|
return acc + differenceInMinutes(end, start);
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}, 0);
|
||||||
|
const days = dateRange === '7days' ? 7 : dateRange === '30days' ? 30 : 90;
|
||||||
|
const avgSleepHours = days > 0 ? (totalSleepMinutes / 60 / days).toFixed(1) : '0.0';
|
||||||
|
|
||||||
|
// Most common activity type
|
||||||
|
const typeCounts: Record<string, number> = {};
|
||||||
|
activities.forEach((a) => {
|
||||||
|
typeCounts[a.type] = (typeCounts[a.type] || 0) + 1;
|
||||||
|
});
|
||||||
|
const mostCommonType = Object.entries(typeCounts).sort((a, b) => b[1] - a[1])[0]?.[0] || 'None';
|
||||||
|
|
||||||
|
return {
|
||||||
|
totalFeedings,
|
||||||
|
avgSleepHours,
|
||||||
|
totalDiapers,
|
||||||
|
mostCommonType,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// Prepare chart data - daily breakdown
|
||||||
|
const prepareDailyData = (): DayData[] => {
|
||||||
|
const days = dateRange === '7days' ? 7 : dateRange === '30days' ? 30 : 90;
|
||||||
|
const dailyMap = new Map<string, DayData>();
|
||||||
|
|
||||||
|
// Initialize all days
|
||||||
|
for (let i = days - 1; i >= 0; i--) {
|
||||||
|
const date = format(subDays(new Date(), i), 'yyyy-MM-dd');
|
||||||
|
dailyMap.set(date, {
|
||||||
|
date: format(subDays(new Date(), i), 'MMM dd'),
|
||||||
|
feedings: 0,
|
||||||
|
sleepHours: 0,
|
||||||
|
diapers: 0,
|
||||||
|
activities: 0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Populate with actual data
|
||||||
|
activities.forEach((activity) => {
|
||||||
|
const dateKey = format(parseISO(activity.timestamp), 'yyyy-MM-dd');
|
||||||
|
const data = dailyMap.get(dateKey);
|
||||||
|
if (data) {
|
||||||
|
data.activities += 1;
|
||||||
|
if (activity.type === 'feeding') data.feedings += 1;
|
||||||
|
if (activity.type === 'diaper') data.diapers += 1;
|
||||||
|
if (activity.type === 'sleep' && activity.data?.endTime && activity.data?.startTime) {
|
||||||
|
const start = parseISO(activity.data.startTime);
|
||||||
|
const end = parseISO(activity.data.endTime);
|
||||||
|
const hours = differenceInMinutes(end, start) / 60;
|
||||||
|
data.sleepHours += hours;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return Array.from(dailyMap.values()).map((d) => ({
|
||||||
|
...d,
|
||||||
|
sleepHours: Number(d.sleepHours.toFixed(1)),
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
// Prepare diaper type data
|
||||||
|
const prepareDiaperData = (): DiaperTypeData[] => {
|
||||||
|
const diaperActivities = activities.filter((a) => a.type === 'diaper');
|
||||||
|
const typeCount: Record<string, number> = {};
|
||||||
|
|
||||||
|
diaperActivities.forEach((activity) => {
|
||||||
|
const type = activity.data?.type || 'unknown';
|
||||||
|
typeCount[type] = (typeCount[type] || 0) + 1;
|
||||||
|
});
|
||||||
|
|
||||||
|
return Object.entries(typeCount).map(([name, value]) => ({
|
||||||
|
name: name.charAt(0).toUpperCase() + name.slice(1),
|
||||||
|
value,
|
||||||
|
color: COLORS[name as keyof typeof COLORS] || '#CCCCCC',
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
// Prepare activity type data
|
||||||
|
const prepareActivityTypeData = (): ActivityTypeData[] => {
|
||||||
|
const typeCount: Record<string, number> = {};
|
||||||
|
|
||||||
|
activities.forEach((activity) => {
|
||||||
|
typeCount[activity.type] = (typeCount[activity.type] || 0) + 1;
|
||||||
|
});
|
||||||
|
|
||||||
|
return Object.entries(typeCount).map(([name, count]) => ({
|
||||||
|
name: name.charAt(0).toUpperCase() + name.slice(1),
|
||||||
|
count,
|
||||||
|
color: COLORS[name as keyof typeof COLORS] || '#CCCCCC',
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
const stats = calculateStats();
|
||||||
|
const dailyData = prepareDailyData();
|
||||||
|
const diaperData = prepareDiaperData();
|
||||||
|
const activityTypeData = prepareActivityTypeData();
|
||||||
|
const recentActivities = [...activities]
|
||||||
|
.sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime())
|
||||||
|
.slice(0, 20);
|
||||||
|
|
||||||
|
// Empty state check
|
||||||
|
const noChildren = children.length === 0;
|
||||||
|
const noActivities = activities.length === 0 && !loading;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ProtectedRoute>
|
<ProtectedRoute>
|
||||||
<AppShell>
|
<AppShell>
|
||||||
<Box>
|
<motion.div
|
||||||
<Typography variant="h4" fontWeight="600" gutterBottom>
|
initial={{ opacity: 0, y: 20 }}
|
||||||
Insights & Analytics
|
animate={{ opacity: 1, y: 0 }}
|
||||||
</Typography>
|
transition={{ duration: 0.5 }}
|
||||||
<Typography variant="body1" color="text.secondary" sx={{ mb: 4 }}>
|
>
|
||||||
Track patterns and get insights about your child's activities
|
<Box>
|
||||||
</Typography>
|
<Typography variant="h4" fontWeight="600" gutterBottom>
|
||||||
|
Insights & Analytics
|
||||||
|
</Typography>
|
||||||
|
<Typography variant="body1" color="text.secondary" sx={{ mb: 4 }}>
|
||||||
|
Track patterns and get insights about your child's activities
|
||||||
|
</Typography>
|
||||||
|
|
||||||
<Grid container spacing={3}>
|
{/* Filters */}
|
||||||
<Grid item xs={12} md={6}>
|
<Paper sx={{ p: 3, mb: 3 }}>
|
||||||
<Card>
|
<Grid container spacing={2} alignItems="center">
|
||||||
<CardContent>
|
{children.length > 1 && (
|
||||||
<Box sx={{ display: 'flex', alignItems: 'center', mb: 2 }}>
|
<Grid item xs={12} sm={6} md={4}>
|
||||||
<TrendingUp sx={{ mr: 1, color: 'primary.main' }} />
|
<FormControl fullWidth>
|
||||||
<Typography variant="h6" fontWeight="600">
|
<InputLabel>Child</InputLabel>
|
||||||
Sleep Patterns
|
<Select
|
||||||
</Typography>
|
value={selectedChild}
|
||||||
</Box>
|
onChange={(e) => setSelectedChild(e.target.value)}
|
||||||
<Typography variant="body2" color="text.secondary">
|
label="Child"
|
||||||
Average sleep duration: Coming soon
|
>
|
||||||
</Typography>
|
{children.map((child) => (
|
||||||
<Typography variant="body2" color="text.secondary">
|
<MenuItem key={child.id} value={child.id}>
|
||||||
Sleep quality: Coming soon
|
{child.name}
|
||||||
</Typography>
|
</MenuItem>
|
||||||
</CardContent>
|
))}
|
||||||
</Card>
|
</Select>
|
||||||
</Grid>
|
</FormControl>
|
||||||
|
</Grid>
|
||||||
|
)}
|
||||||
|
<Grid item xs={12} sm={6} md={4}>
|
||||||
|
<ToggleButtonGroup
|
||||||
|
value={dateRange}
|
||||||
|
exclusive
|
||||||
|
onChange={(_, newValue) => newValue && setDateRange(newValue)}
|
||||||
|
fullWidth
|
||||||
|
size="large"
|
||||||
|
>
|
||||||
|
<ToggleButton value="7days">7 Days</ToggleButton>
|
||||||
|
<ToggleButton value="30days">30 Days</ToggleButton>
|
||||||
|
<ToggleButton value="3months">3 Months</ToggleButton>
|
||||||
|
</ToggleButtonGroup>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
</Paper>
|
||||||
|
|
||||||
<Grid item xs={12} md={6}>
|
{/* Error State */}
|
||||||
<Card>
|
{error && (
|
||||||
<CardContent>
|
<Alert severity="error" sx={{ mb: 3 }}>
|
||||||
<Box sx={{ display: 'flex', alignItems: 'center', mb: 2 }}>
|
{error}
|
||||||
<InsightsIcon sx={{ mr: 1, color: 'success.main' }} />
|
</Alert>
|
||||||
<Typography variant="h6" fontWeight="600">
|
)}
|
||||||
Feeding Patterns
|
|
||||||
</Typography>
|
|
||||||
</Box>
|
|
||||||
<Typography variant="body2" color="text.secondary">
|
|
||||||
Average feeding frequency: Coming soon
|
|
||||||
</Typography>
|
|
||||||
<Typography variant="body2" color="text.secondary">
|
|
||||||
Total daily intake: Coming soon
|
|
||||||
</Typography>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</Grid>
|
|
||||||
|
|
||||||
<Grid item xs={12}>
|
{/* No Children State */}
|
||||||
<Card>
|
{noChildren && !loading && (
|
||||||
<CardContent>
|
<Alert severity="info" sx={{ mb: 3 }}>
|
||||||
<Box sx={{ display: 'flex', alignItems: 'center', mb: 2 }}>
|
No children found. Please add a child to view insights.
|
||||||
<Timeline sx={{ mr: 1, color: 'info.main' }} />
|
</Alert>
|
||||||
<Typography variant="h6" fontWeight="600">
|
)}
|
||||||
Activity Timeline
|
|
||||||
</Typography>
|
{/* Loading State */}
|
||||||
|
{loading && (
|
||||||
|
<Box sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', py: 8 }}>
|
||||||
|
<CircularProgress />
|
||||||
</Box>
|
</Box>
|
||||||
<Typography variant="body2" color="text.secondary">
|
)}
|
||||||
Detailed analytics and trends will be displayed here
|
|
||||||
</Typography>
|
{/* No Activities State */}
|
||||||
</CardContent>
|
{noActivities && !noChildren && (
|
||||||
</Card>
|
<Alert severity="info" sx={{ mb: 3 }}>
|
||||||
</Grid>
|
No activities found for the selected date range. Start tracking activities to see insights!
|
||||||
</Grid>
|
</Alert>
|
||||||
</Box>
|
)}
|
||||||
|
|
||||||
|
{/* Content */}
|
||||||
|
{!loading && !noChildren && !noActivities && (
|
||||||
|
<>
|
||||||
|
{/* Summary Statistics */}
|
||||||
|
<Grid container spacing={3} sx={{ mb: 3 }}>
|
||||||
|
<Grid item xs={12} sm={6} md={3}>
|
||||||
|
<motion.div
|
||||||
|
initial={{ opacity: 0, scale: 0.9 }}
|
||||||
|
animate={{ opacity: 1, scale: 1 }}
|
||||||
|
transition={{ duration: 0.3, delay: 0 }}
|
||||||
|
>
|
||||||
|
<Card sx={{ bgcolor: COLORS.feeding, color: 'white' }}>
|
||||||
|
<CardContent>
|
||||||
|
<Box sx={{ display: 'flex', alignItems: 'center', mb: 1 }}>
|
||||||
|
<Restaurant sx={{ fontSize: 32, mr: 1 }} />
|
||||||
|
<Typography variant="h6" fontWeight="600">
|
||||||
|
Feedings
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
<Typography variant="h3" fontWeight="700">
|
||||||
|
{stats.totalFeedings}
|
||||||
|
</Typography>
|
||||||
|
<Typography variant="body2" sx={{ opacity: 0.9, mt: 1 }}>
|
||||||
|
Total count
|
||||||
|
</Typography>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</motion.div>
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
<Grid item xs={12} sm={6} md={3}>
|
||||||
|
<motion.div
|
||||||
|
initial={{ opacity: 0, scale: 0.9 }}
|
||||||
|
animate={{ opacity: 1, scale: 1 }}
|
||||||
|
transition={{ duration: 0.3, delay: 0.1 }}
|
||||||
|
>
|
||||||
|
<Card sx={{ bgcolor: COLORS.sleep, color: 'white' }}>
|
||||||
|
<CardContent>
|
||||||
|
<Box sx={{ display: 'flex', alignItems: 'center', mb: 1 }}>
|
||||||
|
<Hotel sx={{ fontSize: 32, mr: 1 }} />
|
||||||
|
<Typography variant="h6" fontWeight="600">
|
||||||
|
Sleep
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
<Typography variant="h3" fontWeight="700">
|
||||||
|
{stats.avgSleepHours}h
|
||||||
|
</Typography>
|
||||||
|
<Typography variant="body2" sx={{ opacity: 0.9, mt: 1 }}>
|
||||||
|
Average per day
|
||||||
|
</Typography>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</motion.div>
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
<Grid item xs={12} sm={6} md={3}>
|
||||||
|
<motion.div
|
||||||
|
initial={{ opacity: 0, scale: 0.9 }}
|
||||||
|
animate={{ opacity: 1, scale: 1 }}
|
||||||
|
transition={{ duration: 0.3, delay: 0.2 }}
|
||||||
|
>
|
||||||
|
<Card sx={{ bgcolor: COLORS.diaper, color: 'white' }}>
|
||||||
|
<CardContent>
|
||||||
|
<Box sx={{ display: 'flex', alignItems: 'center', mb: 1 }}>
|
||||||
|
<BabyChangingStation sx={{ fontSize: 32, mr: 1 }} />
|
||||||
|
<Typography variant="h6" fontWeight="600">
|
||||||
|
Diapers
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
<Typography variant="h3" fontWeight="700">
|
||||||
|
{stats.totalDiapers}
|
||||||
|
</Typography>
|
||||||
|
<Typography variant="body2" sx={{ opacity: 0.9, mt: 1 }}>
|
||||||
|
Total changes
|
||||||
|
</Typography>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</motion.div>
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
<Grid item xs={12} sm={6} md={3}>
|
||||||
|
<motion.div
|
||||||
|
initial={{ opacity: 0, scale: 0.9 }}
|
||||||
|
animate={{ opacity: 1, scale: 1 }}
|
||||||
|
transition={{ duration: 0.3, delay: 0.3 }}
|
||||||
|
>
|
||||||
|
<Card sx={{ bgcolor: COLORS.milestone, color: 'white' }}>
|
||||||
|
<CardContent>
|
||||||
|
<Box sx={{ display: 'flex', alignItems: 'center', mb: 1 }}>
|
||||||
|
<TrendingUp sx={{ fontSize: 32, mr: 1 }} />
|
||||||
|
<Typography variant="h6" fontWeight="600">
|
||||||
|
Top Activity
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
<Typography variant="h3" fontWeight="700" sx={{ textTransform: 'capitalize' }}>
|
||||||
|
{stats.mostCommonType}
|
||||||
|
</Typography>
|
||||||
|
<Typography variant="body2" sx={{ opacity: 0.9, mt: 1 }}>
|
||||||
|
Most frequent
|
||||||
|
</Typography>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</motion.div>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
{/* Charts */}
|
||||||
|
<Grid container spacing={3} sx={{ mb: 3 }}>
|
||||||
|
{/* Feeding Frequency Chart */}
|
||||||
|
<Grid item xs={12} md={6}>
|
||||||
|
<Card>
|
||||||
|
<CardContent>
|
||||||
|
<Box sx={{ display: 'flex', alignItems: 'center', mb: 2 }}>
|
||||||
|
<Restaurant sx={{ mr: 1, color: COLORS.feeding }} />
|
||||||
|
<Typography variant="h6" fontWeight="600">
|
||||||
|
Feeding Frequency
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
<ResponsiveContainer width="100%" height={250}>
|
||||||
|
<BarChart data={dailyData}>
|
||||||
|
<CartesianGrid strokeDasharray="3 3" />
|
||||||
|
<XAxis dataKey="date" tick={{ fontSize: 12 }} />
|
||||||
|
<YAxis />
|
||||||
|
<Tooltip />
|
||||||
|
<Bar dataKey="feedings" fill={COLORS.feeding} name="Feedings" />
|
||||||
|
</BarChart>
|
||||||
|
</ResponsiveContainer>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
{/* Sleep Duration Chart */}
|
||||||
|
<Grid item xs={12} md={6}>
|
||||||
|
<Card>
|
||||||
|
<CardContent>
|
||||||
|
<Box sx={{ display: 'flex', alignItems: 'center', mb: 2 }}>
|
||||||
|
<Hotel sx={{ mr: 1, color: COLORS.sleep }} />
|
||||||
|
<Typography variant="h6" fontWeight="600">
|
||||||
|
Sleep Duration (Hours)
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
<ResponsiveContainer width="100%" height={250}>
|
||||||
|
<LineChart data={dailyData}>
|
||||||
|
<CartesianGrid strokeDasharray="3 3" />
|
||||||
|
<XAxis dataKey="date" tick={{ fontSize: 12 }} />
|
||||||
|
<YAxis />
|
||||||
|
<Tooltip />
|
||||||
|
<Line
|
||||||
|
type="monotone"
|
||||||
|
dataKey="sleepHours"
|
||||||
|
stroke={COLORS.sleep}
|
||||||
|
strokeWidth={3}
|
||||||
|
name="Sleep Hours"
|
||||||
|
dot={{ fill: COLORS.sleep, r: 4 }}
|
||||||
|
/>
|
||||||
|
</LineChart>
|
||||||
|
</ResponsiveContainer>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
{/* Diaper Changes by Type */}
|
||||||
|
{diaperData.length > 0 && (
|
||||||
|
<Grid item xs={12} md={6}>
|
||||||
|
<Card>
|
||||||
|
<CardContent>
|
||||||
|
<Box sx={{ display: 'flex', alignItems: 'center', mb: 2 }}>
|
||||||
|
<BabyChangingStation sx={{ mr: 1, color: COLORS.diaper }} />
|
||||||
|
<Typography variant="h6" fontWeight="600">
|
||||||
|
Diaper Changes by Type
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
<ResponsiveContainer width="100%" height={250}>
|
||||||
|
<PieChart>
|
||||||
|
<Pie
|
||||||
|
data={diaperData}
|
||||||
|
cx="50%"
|
||||||
|
cy="50%"
|
||||||
|
labelLine={false}
|
||||||
|
label={({ name, percent }) => `${name} ${(percent * 100).toFixed(0)}%`}
|
||||||
|
outerRadius={80}
|
||||||
|
fill="#8884d8"
|
||||||
|
dataKey="value"
|
||||||
|
>
|
||||||
|
{diaperData.map((entry, index) => (
|
||||||
|
<Cell key={`cell-${index}`} fill={entry.color} />
|
||||||
|
))}
|
||||||
|
</Pie>
|
||||||
|
<Tooltip />
|
||||||
|
</PieChart>
|
||||||
|
</ResponsiveContainer>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</Grid>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Activity Timeline */}
|
||||||
|
<Grid item xs={12} md={diaperData.length > 0 ? 6 : 12}>
|
||||||
|
<Card>
|
||||||
|
<CardContent>
|
||||||
|
<Box sx={{ display: 'flex', alignItems: 'center', mb: 2 }}>
|
||||||
|
<Assessment sx={{ mr: 1, color: 'primary.main' }} />
|
||||||
|
<Typography variant="h6" fontWeight="600">
|
||||||
|
Activity Timeline
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
<ResponsiveContainer width="100%" height={250}>
|
||||||
|
<BarChart data={dailyData}>
|
||||||
|
<CartesianGrid strokeDasharray="3 3" />
|
||||||
|
<XAxis dataKey="date" tick={{ fontSize: 12 }} />
|
||||||
|
<YAxis />
|
||||||
|
<Tooltip />
|
||||||
|
<Legend />
|
||||||
|
<Bar dataKey="feedings" fill={COLORS.feeding} name="Feedings" />
|
||||||
|
<Bar dataKey="diapers" fill={COLORS.diaper} name="Diapers" />
|
||||||
|
<Bar dataKey="sleepHours" fill={COLORS.sleep} name="Sleep (hrs)" />
|
||||||
|
</BarChart>
|
||||||
|
</ResponsiveContainer>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
{/* Activity Type Distribution */}
|
||||||
|
{activityTypeData.length > 0 && (
|
||||||
|
<Card sx={{ mb: 3 }}>
|
||||||
|
<CardContent>
|
||||||
|
<Typography variant="h6" fontWeight="600" gutterBottom>
|
||||||
|
Activity Distribution
|
||||||
|
</Typography>
|
||||||
|
<Box sx={{ display: 'flex', gap: 2, flexWrap: 'wrap', mt: 2 }}>
|
||||||
|
{activityTypeData.map((activity) => (
|
||||||
|
<Chip
|
||||||
|
key={activity.name}
|
||||||
|
icon={getActivityIcon(activity.name.toLowerCase() as ActivityType)}
|
||||||
|
label={`${activity.name}: ${activity.count}`}
|
||||||
|
sx={{
|
||||||
|
bgcolor: activity.color,
|
||||||
|
color: 'white',
|
||||||
|
fontSize: '0.9rem',
|
||||||
|
fontWeight: 600,
|
||||||
|
px: 1,
|
||||||
|
py: 2,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</Box>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Recent Activities */}
|
||||||
|
<Card>
|
||||||
|
<CardContent>
|
||||||
|
<Typography variant="h6" fontWeight="600" gutterBottom>
|
||||||
|
Recent Activities (Last 20)
|
||||||
|
</Typography>
|
||||||
|
<Divider sx={{ my: 2 }} />
|
||||||
|
<List sx={{ maxHeight: 400, overflow: 'auto' }}>
|
||||||
|
{recentActivities.map((activity, index) => (
|
||||||
|
<motion.div
|
||||||
|
key={activity.id}
|
||||||
|
initial={{ opacity: 0, x: -20 }}
|
||||||
|
animate={{ opacity: 1, x: 0 }}
|
||||||
|
transition={{ duration: 0.3, delay: index * 0.02 }}
|
||||||
|
>
|
||||||
|
<ListItem
|
||||||
|
sx={{
|
||||||
|
borderBottom: index < recentActivities.length - 1 ? '1px solid' : 'none',
|
||||||
|
borderColor: 'divider',
|
||||||
|
py: 1.5,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<ListItemAvatar>
|
||||||
|
<Avatar sx={{ bgcolor: getActivityColor(activity.type) }}>
|
||||||
|
{getActivityIcon(activity.type)}
|
||||||
|
</Avatar>
|
||||||
|
</ListItemAvatar>
|
||||||
|
<ListItemText
|
||||||
|
primary={
|
||||||
|
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
|
||||||
|
<Typography variant="body1" fontWeight="600" sx={{ textTransform: 'capitalize' }}>
|
||||||
|
{activity.type}
|
||||||
|
</Typography>
|
||||||
|
<Chip
|
||||||
|
label={formatDistanceToNow(parseISO(activity.timestamp), { addSuffix: true })}
|
||||||
|
size="small"
|
||||||
|
sx={{ height: 20, fontSize: '0.7rem' }}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
}
|
||||||
|
secondary={
|
||||||
|
<Typography variant="body2" color="text.secondary">
|
||||||
|
{activity.notes || format(parseISO(activity.timestamp), 'MMM dd, yyyy HH:mm')}
|
||||||
|
</Typography>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</ListItem>
|
||||||
|
</motion.div>
|
||||||
|
))}
|
||||||
|
</List>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
</motion.div>
|
||||||
</AppShell>
|
</AppShell>
|
||||||
</ProtectedRoute>
|
</ProtectedRoute>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,23 +1,48 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { Box, Typography, Card, CardContent, TextField, Button, Divider, Switch, FormControlLabel } from '@mui/material';
|
import { Box, Typography, Card, CardContent, TextField, Button, Divider, Switch, FormControlLabel, Alert, CircularProgress, Snackbar } from '@mui/material';
|
||||||
import { Save, Logout } from '@mui/icons-material';
|
import { Save, Logout } from '@mui/icons-material';
|
||||||
import { useAuth } from '@/lib/auth/AuthContext';
|
import { useAuth } from '@/lib/auth/AuthContext';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { AppShell } from '@/components/layouts/AppShell/AppShell';
|
import { AppShell } from '@/components/layouts/AppShell/AppShell';
|
||||||
import { ProtectedRoute } from '@/components/common/ProtectedRoute';
|
import { ProtectedRoute } from '@/components/common/ProtectedRoute';
|
||||||
|
import { usersApi } from '@/lib/api/users';
|
||||||
|
import { motion } from 'framer-motion';
|
||||||
|
|
||||||
export default function SettingsPage() {
|
export default function SettingsPage() {
|
||||||
const { user, logout } = useAuth();
|
const { user, logout, refreshUser } = useAuth();
|
||||||
|
const [name, setName] = useState(user?.name || '');
|
||||||
const [settings, setSettings] = useState({
|
const [settings, setSettings] = useState({
|
||||||
notifications: true,
|
notifications: true,
|
||||||
emailUpdates: false,
|
emailUpdates: false,
|
||||||
darkMode: false,
|
darkMode: false,
|
||||||
});
|
});
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const [successMessage, setSuccessMessage] = useState<string | null>(null);
|
||||||
|
const [nameError, setNameError] = useState<string | null>(null);
|
||||||
|
|
||||||
const handleSave = () => {
|
const handleSave = async () => {
|
||||||
// Save settings functionality to be implemented
|
// Validate name
|
||||||
alert('Settings saved successfully!');
|
if (!name || name.trim() === '') {
|
||||||
|
setNameError('Name cannot be empty');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsLoading(true);
|
||||||
|
setError(null);
|
||||||
|
setNameError(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await usersApi.updateProfile({ name: name.trim() });
|
||||||
|
await refreshUser();
|
||||||
|
setSuccessMessage('Profile updated successfully!');
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('Failed to update profile:', err);
|
||||||
|
setError(err.response?.data?.message || 'Failed to update profile. Please try again.');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleLogout = async () => {
|
const handleLogout = async () => {
|
||||||
@@ -35,104 +60,165 @@ export default function SettingsPage() {
|
|||||||
Manage your account settings and preferences
|
Manage your account settings and preferences
|
||||||
</Typography>
|
</Typography>
|
||||||
|
|
||||||
|
{/* Error Alert */}
|
||||||
|
{error && (
|
||||||
|
<motion.div
|
||||||
|
initial={{ opacity: 0, y: -10 }}
|
||||||
|
animate={{ opacity: 1, y: 0 }}
|
||||||
|
transition={{ duration: 0.3 }}
|
||||||
|
>
|
||||||
|
<Alert severity="error" sx={{ mb: 3 }} onClose={() => setError(null)}>
|
||||||
|
{error}
|
||||||
|
</Alert>
|
||||||
|
</motion.div>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* Profile Settings */}
|
{/* Profile Settings */}
|
||||||
<Card sx={{ mb: 3 }}>
|
<motion.div
|
||||||
<CardContent>
|
initial={{ opacity: 0, y: 20 }}
|
||||||
<Typography variant="h6" fontWeight="600" gutterBottom>
|
animate={{ opacity: 1, y: 0 }}
|
||||||
Profile Information
|
transition={{ duration: 0.4 }}
|
||||||
</Typography>
|
>
|
||||||
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2, mt: 2 }}>
|
<Card sx={{ mb: 3 }}>
|
||||||
<TextField
|
<CardContent>
|
||||||
label="Name"
|
<Typography variant="h6" fontWeight="600" gutterBottom>
|
||||||
defaultValue={user?.name}
|
Profile Information
|
||||||
fullWidth
|
</Typography>
|
||||||
/>
|
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2, mt: 2 }}>
|
||||||
<TextField
|
<TextField
|
||||||
label="Email"
|
label="Name"
|
||||||
defaultValue={user?.email}
|
value={name}
|
||||||
fullWidth
|
onChange={(e) => {
|
||||||
disabled
|
setName(e.target.value);
|
||||||
/>
|
if (nameError) setNameError(null);
|
||||||
<Button
|
}}
|
||||||
variant="contained"
|
fullWidth
|
||||||
startIcon={<Save />}
|
error={!!nameError}
|
||||||
onClick={handleSave}
|
helperText={nameError}
|
||||||
sx={{ alignSelf: 'flex-start' }}
|
disabled={isLoading}
|
||||||
>
|
/>
|
||||||
Save Changes
|
<TextField
|
||||||
</Button>
|
label="Email"
|
||||||
</Box>
|
value={user?.email || ''}
|
||||||
</CardContent>
|
fullWidth
|
||||||
</Card>
|
disabled
|
||||||
|
helperText="Email cannot be changed"
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
startIcon={isLoading ? <CircularProgress size={20} color="inherit" /> : <Save />}
|
||||||
|
onClick={handleSave}
|
||||||
|
disabled={isLoading}
|
||||||
|
sx={{ alignSelf: 'flex-start' }}
|
||||||
|
>
|
||||||
|
{isLoading ? 'Saving...' : 'Save Changes'}
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
{/* Notification Settings */}
|
{/* Notification Settings */}
|
||||||
<Card sx={{ mb: 3 }}>
|
<motion.div
|
||||||
<CardContent>
|
initial={{ opacity: 0, y: 20 }}
|
||||||
<Typography variant="h6" fontWeight="600" gutterBottom>
|
animate={{ opacity: 1, y: 0 }}
|
||||||
Notifications
|
transition={{ duration: 0.4, delay: 0.1 }}
|
||||||
</Typography>
|
>
|
||||||
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 1, mt: 2 }}>
|
<Card sx={{ mb: 3 }}>
|
||||||
<FormControlLabel
|
<CardContent>
|
||||||
control={
|
<Typography variant="h6" fontWeight="600" gutterBottom>
|
||||||
<Switch
|
Notifications
|
||||||
checked={settings.notifications}
|
</Typography>
|
||||||
onChange={(e) => setSettings({ ...settings, notifications: e.target.checked })}
|
<Typography variant="body2" color="text.secondary" sx={{ mb: 2 }}>
|
||||||
/>
|
Settings are stored locally (backend integration coming soon)
|
||||||
}
|
</Typography>
|
||||||
label="Push Notifications"
|
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 1, mt: 2 }}>
|
||||||
/>
|
<FormControlLabel
|
||||||
<FormControlLabel
|
control={
|
||||||
control={
|
<Switch
|
||||||
<Switch
|
checked={settings.notifications}
|
||||||
checked={settings.emailUpdates}
|
onChange={(e) => setSettings({ ...settings, notifications: e.target.checked })}
|
||||||
onChange={(e) => setSettings({ ...settings, emailUpdates: e.target.checked })}
|
/>
|
||||||
/>
|
}
|
||||||
}
|
label="Push Notifications"
|
||||||
label="Email Updates"
|
/>
|
||||||
/>
|
<FormControlLabel
|
||||||
</Box>
|
control={
|
||||||
</CardContent>
|
<Switch
|
||||||
</Card>
|
checked={settings.emailUpdates}
|
||||||
|
onChange={(e) => setSettings({ ...settings, emailUpdates: e.target.checked })}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
label="Email Updates"
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
{/* Appearance Settings */}
|
{/* Appearance Settings */}
|
||||||
<Card sx={{ mb: 3 }}>
|
<motion.div
|
||||||
<CardContent>
|
initial={{ opacity: 0, y: 20 }}
|
||||||
<Typography variant="h6" fontWeight="600" gutterBottom>
|
animate={{ opacity: 1, y: 0 }}
|
||||||
Appearance
|
transition={{ duration: 0.4, delay: 0.2 }}
|
||||||
</Typography>
|
>
|
||||||
<Box sx={{ mt: 2 }}>
|
<Card sx={{ mb: 3 }}>
|
||||||
<FormControlLabel
|
<CardContent>
|
||||||
control={
|
<Typography variant="h6" fontWeight="600" gutterBottom>
|
||||||
<Switch
|
Appearance
|
||||||
checked={settings.darkMode}
|
</Typography>
|
||||||
onChange={(e) => setSettings({ ...settings, darkMode: e.target.checked })}
|
<Box sx={{ mt: 2 }}>
|
||||||
/>
|
<FormControlLabel
|
||||||
}
|
control={
|
||||||
label="Dark Mode (Coming Soon)"
|
<Switch
|
||||||
disabled
|
checked={settings.darkMode}
|
||||||
/>
|
onChange={(e) => setSettings({ ...settings, darkMode: e.target.checked })}
|
||||||
</Box>
|
/>
|
||||||
</CardContent>
|
}
|
||||||
</Card>
|
label="Dark Mode (Coming Soon)"
|
||||||
|
disabled
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
{/* Account Actions */}
|
{/* Account Actions */}
|
||||||
<Card>
|
<motion.div
|
||||||
<CardContent>
|
initial={{ opacity: 0, y: 20 }}
|
||||||
<Typography variant="h6" fontWeight="600" gutterBottom>
|
animate={{ opacity: 1, y: 0 }}
|
||||||
Account Actions
|
transition={{ duration: 0.4, delay: 0.3 }}
|
||||||
</Typography>
|
>
|
||||||
<Divider sx={{ my: 2 }} />
|
<Card>
|
||||||
<Button
|
<CardContent>
|
||||||
variant="outlined"
|
<Typography variant="h6" fontWeight="600" gutterBottom>
|
||||||
color="error"
|
Account Actions
|
||||||
startIcon={<Logout />}
|
</Typography>
|
||||||
onClick={handleLogout}
|
<Divider sx={{ my: 2 }} />
|
||||||
fullWidth
|
<Button
|
||||||
>
|
variant="outlined"
|
||||||
Logout
|
color="error"
|
||||||
</Button>
|
startIcon={<Logout />}
|
||||||
</CardContent>
|
onClick={handleLogout}
|
||||||
</Card>
|
fullWidth
|
||||||
|
>
|
||||||
|
Logout
|
||||||
|
</Button>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</motion.div>
|
||||||
|
|
||||||
|
{/* Success Snackbar */}
|
||||||
|
<Snackbar
|
||||||
|
open={!!successMessage}
|
||||||
|
autoHideDuration={4000}
|
||||||
|
onClose={() => setSuccessMessage(null)}
|
||||||
|
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
|
||||||
|
>
|
||||||
|
<Alert onClose={() => setSuccessMessage(null)} severity="success" sx={{ width: '100%' }}>
|
||||||
|
{successMessage}
|
||||||
|
</Alert>
|
||||||
|
</Snackbar>
|
||||||
</Box>
|
</Box>
|
||||||
</AppShell>
|
</AppShell>
|
||||||
</ProtectedRoute>
|
</ProtectedRoute>
|
||||||
|
|||||||
@@ -1,89 +1,337 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import {
|
import {
|
||||||
Box,
|
Box,
|
||||||
Typography,
|
Typography,
|
||||||
Button,
|
Button,
|
||||||
Paper,
|
Paper,
|
||||||
TextField,
|
TextField,
|
||||||
|
FormControl,
|
||||||
|
InputLabel,
|
||||||
|
Select,
|
||||||
|
MenuItem,
|
||||||
IconButton,
|
IconButton,
|
||||||
Alert,
|
Alert,
|
||||||
|
CircularProgress,
|
||||||
|
Card,
|
||||||
|
CardContent,
|
||||||
|
Dialog,
|
||||||
|
DialogTitle,
|
||||||
|
DialogContent,
|
||||||
|
DialogContentText,
|
||||||
|
DialogActions,
|
||||||
Chip,
|
Chip,
|
||||||
FormControl,
|
Snackbar,
|
||||||
FormLabel,
|
|
||||||
RadioGroup,
|
|
||||||
FormControlLabel,
|
|
||||||
Radio,
|
|
||||||
ToggleButtonGroup,
|
ToggleButtonGroup,
|
||||||
ToggleButton,
|
ToggleButton,
|
||||||
|
FormLabel,
|
||||||
} from '@mui/material';
|
} from '@mui/material';
|
||||||
import {
|
import {
|
||||||
ArrowBack,
|
ArrowBack,
|
||||||
|
Refresh,
|
||||||
Save,
|
Save,
|
||||||
Mic,
|
Delete,
|
||||||
BabyChangingStation,
|
BabyChangingStation,
|
||||||
|
Warning,
|
||||||
|
CheckCircle,
|
||||||
} from '@mui/icons-material';
|
} from '@mui/icons-material';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { AppShell } from '@/components/layouts/AppShell/AppShell';
|
import { AppShell } from '@/components/layouts/AppShell/AppShell';
|
||||||
import { ProtectedRoute } from '@/components/common/ProtectedRoute';
|
import { ProtectedRoute } from '@/components/common/ProtectedRoute';
|
||||||
|
import { useAuth } from '@/lib/auth/AuthContext';
|
||||||
|
import { trackingApi, Activity } from '@/lib/api/tracking';
|
||||||
|
import { childrenApi, Child } from '@/lib/api/children';
|
||||||
import { motion } from 'framer-motion';
|
import { motion } from 'framer-motion';
|
||||||
import { useForm, Controller } from 'react-hook-form';
|
import { formatDistanceToNow, format } from 'date-fns';
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
|
||||||
import * as z from 'zod';
|
|
||||||
import { format } from 'date-fns';
|
|
||||||
|
|
||||||
const diaperSchema = z.object({
|
interface DiaperData {
|
||||||
type: z.enum(['wet', 'dirty', 'both', 'clean']),
|
diaperType: 'wet' | 'dirty' | 'both' | 'dry';
|
||||||
timestamp: z.string(),
|
conditions: string[];
|
||||||
rash: z.boolean(),
|
hasRash: boolean;
|
||||||
notes: z.string().optional(),
|
rashSeverity?: 'mild' | 'moderate' | 'severe';
|
||||||
});
|
}
|
||||||
|
|
||||||
type DiaperFormData = z.infer<typeof diaperSchema>;
|
|
||||||
|
|
||||||
export default function DiaperTrackPage() {
|
export default function DiaperTrackPage() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const { user } = useAuth();
|
||||||
|
const [children, setChildren] = useState<Child[]>([]);
|
||||||
|
const [selectedChild, setSelectedChild] = useState<string>('');
|
||||||
|
|
||||||
|
// Diaper state
|
||||||
|
const [timestamp, setTimestamp] = useState<string>(
|
||||||
|
format(new Date(), "yyyy-MM-dd'T'HH:mm")
|
||||||
|
);
|
||||||
|
const [diaperType, setDiaperType] = useState<'wet' | 'dirty' | 'both' | 'dry'>('wet');
|
||||||
|
const [conditions, setConditions] = useState<string[]>(['normal']);
|
||||||
|
const [hasRash, setHasRash] = useState<boolean>(false);
|
||||||
|
const [rashSeverity, setRashSeverity] = useState<'mild' | 'moderate' | 'severe'>('mild');
|
||||||
|
|
||||||
|
// Common state
|
||||||
|
const [notes, setNotes] = useState<string>('');
|
||||||
|
const [recentDiapers, setRecentDiapers] = useState<Activity[]>([]);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [childrenLoading, setChildrenLoading] = useState(true);
|
||||||
|
const [diapersLoading, setDiapersLoading] = useState(false);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [success, setSuccess] = useState(false);
|
const [successMessage, setSuccessMessage] = useState<string | null>(null);
|
||||||
|
|
||||||
const {
|
// Delete confirmation dialog
|
||||||
register,
|
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
||||||
handleSubmit,
|
const [activityToDelete, setActivityToDelete] = useState<string | null>(null);
|
||||||
setValue,
|
|
||||||
watch,
|
|
||||||
control,
|
|
||||||
formState: { errors },
|
|
||||||
} = useForm<DiaperFormData>({
|
|
||||||
resolver: zodResolver(diaperSchema),
|
|
||||||
defaultValues: {
|
|
||||||
type: 'wet',
|
|
||||||
rash: false,
|
|
||||||
timestamp: format(new Date(), "yyyy-MM-dd'T'HH:mm"),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const diaperType = watch('type');
|
const familyId = user?.families?.[0]?.familyId;
|
||||||
const rash = watch('rash');
|
|
||||||
|
|
||||||
const setTimeNow = () => {
|
const availableConditions = [
|
||||||
const now = format(new Date(), "yyyy-MM-dd'T'HH:mm");
|
'normal',
|
||||||
setValue('timestamp', now);
|
'soft',
|
||||||
};
|
'hard',
|
||||||
|
'watery',
|
||||||
|
'mucus',
|
||||||
|
'blood',
|
||||||
|
];
|
||||||
|
|
||||||
const onSubmit = async (data: DiaperFormData) => {
|
// Load children
|
||||||
setError(null);
|
useEffect(() => {
|
||||||
|
if (familyId) {
|
||||||
|
loadChildren();
|
||||||
|
}
|
||||||
|
}, [familyId]);
|
||||||
|
|
||||||
|
// Load recent diapers when child is selected
|
||||||
|
useEffect(() => {
|
||||||
|
if (selectedChild) {
|
||||||
|
loadRecentDiapers();
|
||||||
|
}
|
||||||
|
}, [selectedChild]);
|
||||||
|
|
||||||
|
const loadChildren = async () => {
|
||||||
|
if (!familyId) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// TODO: Call API to save diaper data
|
setChildrenLoading(true);
|
||||||
console.log('Diaper data:', data);
|
const childrenData = await childrenApi.getChildren(familyId);
|
||||||
setSuccess(true);
|
setChildren(childrenData);
|
||||||
setTimeout(() => router.push('/'), 2000);
|
if (childrenData.length > 0) {
|
||||||
|
setSelectedChild(childrenData[0].id);
|
||||||
|
}
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
setError(err.message || 'Failed to log diaper change');
|
console.error('Failed to load children:', err);
|
||||||
|
setError(err.response?.data?.message || 'Failed to load children');
|
||||||
|
} finally {
|
||||||
|
setChildrenLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const loadRecentDiapers = async () => {
|
||||||
|
if (!selectedChild) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
setDiapersLoading(true);
|
||||||
|
const activities = await trackingApi.getActivities(selectedChild, 'diaper');
|
||||||
|
// 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);
|
||||||
|
setRecentDiapers(sorted);
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('Failed to load recent diapers:', err);
|
||||||
|
} finally {
|
||||||
|
setDiapersLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const setTimeNow = () => {
|
||||||
|
setTimestamp(format(new Date(), "yyyy-MM-dd'T'HH:mm"));
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleConditionToggle = (condition: string) => {
|
||||||
|
setConditions((prev) => {
|
||||||
|
if (prev.includes(condition)) {
|
||||||
|
// Remove condition, but ensure at least one remains
|
||||||
|
if (prev.length === 1) return prev;
|
||||||
|
return prev.filter((c) => c !== condition);
|
||||||
|
} else {
|
||||||
|
return [...prev, condition];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = async () => {
|
||||||
|
if (!selectedChild) {
|
||||||
|
setError('Please select a child');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validation
|
||||||
|
if (!timestamp) {
|
||||||
|
setError('Please enter timestamp');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (conditions.length === 0) {
|
||||||
|
setError('Please select at least one condition');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
setLoading(true);
|
||||||
|
setError(null);
|
||||||
|
|
||||||
|
const data: DiaperData = {
|
||||||
|
diaperType,
|
||||||
|
conditions,
|
||||||
|
hasRash,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (hasRash) {
|
||||||
|
data.rashSeverity = rashSeverity;
|
||||||
|
}
|
||||||
|
|
||||||
|
await trackingApi.createActivity(selectedChild, {
|
||||||
|
type: 'diaper',
|
||||||
|
timestamp,
|
||||||
|
data,
|
||||||
|
notes: notes || undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
setSuccessMessage('Diaper change logged successfully!');
|
||||||
|
|
||||||
|
// Reset form
|
||||||
|
resetForm();
|
||||||
|
|
||||||
|
// Reload recent diapers
|
||||||
|
await loadRecentDiapers();
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('Failed to save diaper:', err);
|
||||||
|
setError(err.response?.data?.message || 'Failed to save diaper change');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const resetForm = () => {
|
||||||
|
setTimestamp(format(new Date(), "yyyy-MM-dd'T'HH:mm"));
|
||||||
|
setDiaperType('wet');
|
||||||
|
setConditions(['normal']);
|
||||||
|
setHasRash(false);
|
||||||
|
setRashSeverity('mild');
|
||||||
|
setNotes('');
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteClick = (activityId: string) => {
|
||||||
|
setActivityToDelete(activityId);
|
||||||
|
setDeleteDialogOpen(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteConfirm = async () => {
|
||||||
|
if (!activityToDelete) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
setLoading(true);
|
||||||
|
await trackingApi.deleteActivity(activityToDelete);
|
||||||
|
setSuccessMessage('Diaper change deleted successfully');
|
||||||
|
setDeleteDialogOpen(false);
|
||||||
|
setActivityToDelete(null);
|
||||||
|
await loadRecentDiapers();
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('Failed to delete diaper:', err);
|
||||||
|
setError(err.response?.data?.message || 'Failed to delete diaper change');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getDiaperTypeColor = (type: string) => {
|
||||||
|
switch (type) {
|
||||||
|
case 'wet':
|
||||||
|
return '#2196f3'; // blue
|
||||||
|
case 'dirty':
|
||||||
|
return '#795548'; // brown
|
||||||
|
case 'both':
|
||||||
|
return '#ff9800'; // orange
|
||||||
|
case 'dry':
|
||||||
|
return '#4caf50'; // green
|
||||||
|
default:
|
||||||
|
return '#757575'; // grey
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getDiaperTypeIcon = (type: string) => {
|
||||||
|
switch (type) {
|
||||||
|
case 'wet':
|
||||||
|
return '💧';
|
||||||
|
case 'dirty':
|
||||||
|
return '💩';
|
||||||
|
case 'both':
|
||||||
|
return '💧💩';
|
||||||
|
case 'dry':
|
||||||
|
return '✨';
|
||||||
|
default:
|
||||||
|
return '🍼';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getDiaperDetails = (activity: Activity) => {
|
||||||
|
const data = activity.data as DiaperData;
|
||||||
|
const typeLabel = data.diaperType.charAt(0).toUpperCase() + data.diaperType.slice(1);
|
||||||
|
const conditionsLabel = data.conditions.join(', ');
|
||||||
|
|
||||||
|
let details = `${typeLabel} - ${conditionsLabel}`;
|
||||||
|
|
||||||
|
if (data.hasRash) {
|
||||||
|
details += ` - Rash (${data.rashSeverity})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return details;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getRashSeverityColor = (severity: string) => {
|
||||||
|
switch (severity) {
|
||||||
|
case 'mild':
|
||||||
|
return 'warning';
|
||||||
|
case 'moderate':
|
||||||
|
return 'error';
|
||||||
|
case 'severe':
|
||||||
|
return 'error';
|
||||||
|
default:
|
||||||
|
return 'default';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (childrenLoading) {
|
||||||
|
return (
|
||||||
|
<ProtectedRoute>
|
||||||
|
<AppShell>
|
||||||
|
<Box sx={{ display: 'flex', justifyContent: 'center', py: 8 }}>
|
||||||
|
<CircularProgress />
|
||||||
|
</Box>
|
||||||
|
</AppShell>
|
||||||
|
</ProtectedRoute>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!familyId || children.length === 0) {
|
||||||
|
return (
|
||||||
|
<ProtectedRoute>
|
||||||
|
<AppShell>
|
||||||
|
<Box>
|
||||||
|
<Alert severity="warning">
|
||||||
|
Please add a child first before tracking diaper changes.
|
||||||
|
</Alert>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
onClick={() => router.push('/children')}
|
||||||
|
sx={{ mt: 2 }}
|
||||||
|
>
|
||||||
|
Go to Children Page
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
</AppShell>
|
||||||
|
</ProtectedRoute>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ProtectedRoute>
|
<ProtectedRoute>
|
||||||
<AppShell>
|
<AppShell>
|
||||||
@@ -97,14 +345,8 @@ export default function DiaperTrackPage() {
|
|||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
{success && (
|
|
||||||
<Alert severity="success" sx={{ mb: 3 }}>
|
|
||||||
Diaper change logged successfully!
|
|
||||||
</Alert>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{error && (
|
{error && (
|
||||||
<Alert severity="error" sx={{ mb: 3 }}>
|
<Alert severity="error" sx={{ mb: 3 }} onClose={() => setError(null)}>
|
||||||
{error}
|
{error}
|
||||||
</Alert>
|
</Alert>
|
||||||
)}
|
)}
|
||||||
@@ -114,150 +356,299 @@ export default function DiaperTrackPage() {
|
|||||||
animate={{ opacity: 1, y: 0 }}
|
animate={{ opacity: 1, y: 0 }}
|
||||||
transition={{ duration: 0.3 }}
|
transition={{ duration: 0.3 }}
|
||||||
>
|
>
|
||||||
|
{/* Child Selector */}
|
||||||
|
{children.length > 1 && (
|
||||||
|
<Paper sx={{ p: 2, mb: 3 }}>
|
||||||
|
<FormControl fullWidth>
|
||||||
|
<InputLabel>Select Child</InputLabel>
|
||||||
|
<Select
|
||||||
|
value={selectedChild}
|
||||||
|
onChange={(e) => setSelectedChild(e.target.value)}
|
||||||
|
label="Select Child"
|
||||||
|
>
|
||||||
|
{children.map((child) => (
|
||||||
|
<MenuItem key={child.id} value={child.id}>
|
||||||
|
{child.name}
|
||||||
|
</MenuItem>
|
||||||
|
))}
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
</Paper>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Main Form */}
|
||||||
<Paper sx={{ p: 3, mb: 3 }}>
|
<Paper sx={{ p: 3, mb: 3 }}>
|
||||||
<Box component="form" onSubmit={handleSubmit(onSubmit)}>
|
{/* Icon Header */}
|
||||||
{/* Icon Header */}
|
<Box sx={{ display: 'flex', justifyContent: 'center', mb: 3 }}>
|
||||||
<Box sx={{ display: 'flex', justifyContent: 'center', mb: 3 }}>
|
<BabyChangingStation sx={{ fontSize: 64, color: 'primary.main' }} />
|
||||||
<BabyChangingStation sx={{ fontSize: 64, color: 'primary.main' }} />
|
|
||||||
</Box>
|
|
||||||
|
|
||||||
{/* Time */}
|
|
||||||
<Box sx={{ mb: 3 }}>
|
|
||||||
<FormLabel sx={{ mb: 1, display: 'block' }}>Time</FormLabel>
|
|
||||||
<Box sx={{ display: 'flex', gap: 2, alignItems: 'flex-start' }}>
|
|
||||||
<TextField
|
|
||||||
fullWidth
|
|
||||||
type="datetime-local"
|
|
||||||
{...register('timestamp')}
|
|
||||||
error={!!errors.timestamp}
|
|
||||||
helperText={errors.timestamp?.message}
|
|
||||||
InputLabelProps={{ shrink: true }}
|
|
||||||
/>
|
|
||||||
<Button variant="outlined" onClick={setTimeNow} sx={{ minWidth: 100 }}>
|
|
||||||
Now
|
|
||||||
</Button>
|
|
||||||
</Box>
|
|
||||||
</Box>
|
|
||||||
|
|
||||||
{/* Diaper Type */}
|
|
||||||
<FormControl component="fieldset" sx={{ mb: 3, width: '100%' }}>
|
|
||||||
<FormLabel component="legend" sx={{ mb: 2 }}>
|
|
||||||
Diaper Type
|
|
||||||
</FormLabel>
|
|
||||||
<Controller
|
|
||||||
name="type"
|
|
||||||
control={control}
|
|
||||||
render={({ field }) => (
|
|
||||||
<ToggleButtonGroup
|
|
||||||
{...field}
|
|
||||||
exclusive
|
|
||||||
fullWidth
|
|
||||||
onChange={(_, value) => {
|
|
||||||
if (value !== null) {
|
|
||||||
field.onChange(value);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<ToggleButton value="wet" sx={{ py: 2 }}>
|
|
||||||
<Box sx={{ textAlign: 'center' }}>
|
|
||||||
<Typography variant="h5">💧</Typography>
|
|
||||||
<Typography variant="body2">Wet</Typography>
|
|
||||||
</Box>
|
|
||||||
</ToggleButton>
|
|
||||||
<ToggleButton value="dirty" sx={{ py: 2 }}>
|
|
||||||
<Box sx={{ textAlign: 'center' }}>
|
|
||||||
<Typography variant="h5">💩</Typography>
|
|
||||||
<Typography variant="body2">Dirty</Typography>
|
|
||||||
</Box>
|
|
||||||
</ToggleButton>
|
|
||||||
<ToggleButton value="both" sx={{ py: 2 }}>
|
|
||||||
<Box sx={{ textAlign: 'center' }}>
|
|
||||||
<Typography variant="h5">💧💩</Typography>
|
|
||||||
<Typography variant="body2">Both</Typography>
|
|
||||||
</Box>
|
|
||||||
</ToggleButton>
|
|
||||||
<ToggleButton value="clean" sx={{ py: 2 }}>
|
|
||||||
<Box sx={{ textAlign: 'center' }}>
|
|
||||||
<Typography variant="h5">✨</Typography>
|
|
||||||
<Typography variant="body2">Clean</Typography>
|
|
||||||
</Box>
|
|
||||||
</ToggleButton>
|
|
||||||
</ToggleButtonGroup>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
|
|
||||||
{/* Rash Indicator */}
|
|
||||||
<FormControl component="fieldset" sx={{ mb: 3, width: '100%' }}>
|
|
||||||
<FormLabel component="legend" sx={{ mb: 2 }}>
|
|
||||||
Diaper Rash?
|
|
||||||
</FormLabel>
|
|
||||||
<RadioGroup row>
|
|
||||||
<FormControlLabel
|
|
||||||
value="no"
|
|
||||||
control={
|
|
||||||
<Radio
|
|
||||||
checked={!rash}
|
|
||||||
onChange={() => setValue('rash', false)}
|
|
||||||
/>
|
|
||||||
}
|
|
||||||
label="No"
|
|
||||||
/>
|
|
||||||
<FormControlLabel
|
|
||||||
value="yes"
|
|
||||||
control={
|
|
||||||
<Radio
|
|
||||||
checked={rash}
|
|
||||||
onChange={() => setValue('rash', true)}
|
|
||||||
/>
|
|
||||||
}
|
|
||||||
label="Yes"
|
|
||||||
/>
|
|
||||||
</RadioGroup>
|
|
||||||
</FormControl>
|
|
||||||
|
|
||||||
{/* Rash Warning */}
|
|
||||||
{rash && (
|
|
||||||
<Alert severity="warning" sx={{ mb: 3 }}>
|
|
||||||
Consider applying diaper rash cream and consulting your pediatrician if it persists.
|
|
||||||
</Alert>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Notes */}
|
|
||||||
<TextField
|
|
||||||
fullWidth
|
|
||||||
label="Notes (optional)"
|
|
||||||
multiline
|
|
||||||
rows={3}
|
|
||||||
{...register('notes')}
|
|
||||||
sx={{ mb: 3 }}
|
|
||||||
placeholder="Color, consistency, or any concerns..."
|
|
||||||
/>
|
|
||||||
|
|
||||||
{/* Voice Input Button */}
|
|
||||||
<Box sx={{ display: 'flex', justifyContent: 'center', mb: 3 }}>
|
|
||||||
<Chip
|
|
||||||
icon={<Mic />}
|
|
||||||
label="Use Voice Input"
|
|
||||||
onClick={() => {/* TODO: Implement voice input */}}
|
|
||||||
sx={{ cursor: 'pointer' }}
|
|
||||||
/>
|
|
||||||
</Box>
|
|
||||||
|
|
||||||
{/* Submit Button */}
|
|
||||||
<Button
|
|
||||||
fullWidth
|
|
||||||
type="submit"
|
|
||||||
variant="contained"
|
|
||||||
size="large"
|
|
||||||
startIcon={<Save />}
|
|
||||||
>
|
|
||||||
Save Diaper Change
|
|
||||||
</Button>
|
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
|
{/* Timestamp */}
|
||||||
|
<Box sx={{ mb: 3 }}>
|
||||||
|
<Typography variant="subtitle1" fontWeight="600" sx={{ mb: 1 }}>
|
||||||
|
Time
|
||||||
|
</Typography>
|
||||||
|
<Box sx={{ display: 'flex', gap: 2, alignItems: 'flex-start' }}>
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
type="datetime-local"
|
||||||
|
value={timestamp}
|
||||||
|
onChange={(e) => setTimestamp(e.target.value)}
|
||||||
|
InputLabelProps={{ shrink: true }}
|
||||||
|
/>
|
||||||
|
<Button variant="outlined" onClick={setTimeNow} sx={{ minWidth: 100 }}>
|
||||||
|
Now
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
{/* Diaper Type */}
|
||||||
|
<Box sx={{ mb: 3 }}>
|
||||||
|
<Typography variant="subtitle1" fontWeight="600" sx={{ mb: 2 }}>
|
||||||
|
Diaper Type
|
||||||
|
</Typography>
|
||||||
|
<ToggleButtonGroup
|
||||||
|
value={diaperType}
|
||||||
|
exclusive
|
||||||
|
onChange={(_, value) => {
|
||||||
|
if (value !== null) {
|
||||||
|
setDiaperType(value);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
fullWidth
|
||||||
|
>
|
||||||
|
<ToggleButton value="wet" sx={{ py: 2 }}>
|
||||||
|
<Box sx={{ textAlign: 'center' }}>
|
||||||
|
<Typography variant="h5">💧</Typography>
|
||||||
|
<Typography variant="body2">Wet</Typography>
|
||||||
|
</Box>
|
||||||
|
</ToggleButton>
|
||||||
|
<ToggleButton value="dirty" sx={{ py: 2 }}>
|
||||||
|
<Box sx={{ textAlign: 'center' }}>
|
||||||
|
<Typography variant="h5">💩</Typography>
|
||||||
|
<Typography variant="body2">Dirty</Typography>
|
||||||
|
</Box>
|
||||||
|
</ToggleButton>
|
||||||
|
<ToggleButton value="both" sx={{ py: 2 }}>
|
||||||
|
<Box sx={{ textAlign: 'center' }}>
|
||||||
|
<Typography variant="h5">💧💩</Typography>
|
||||||
|
<Typography variant="body2">Both</Typography>
|
||||||
|
</Box>
|
||||||
|
</ToggleButton>
|
||||||
|
<ToggleButton value="dry" sx={{ py: 2 }}>
|
||||||
|
<Box sx={{ textAlign: 'center' }}>
|
||||||
|
<Typography variant="h5">✨</Typography>
|
||||||
|
<Typography variant="body2">Dry</Typography>
|
||||||
|
</Box>
|
||||||
|
</ToggleButton>
|
||||||
|
</ToggleButtonGroup>
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
{/* Condition Selector */}
|
||||||
|
<Box sx={{ mb: 3 }}>
|
||||||
|
<Typography variant="subtitle1" fontWeight="600" sx={{ mb: 1 }}>
|
||||||
|
Condition (select all that apply)
|
||||||
|
</Typography>
|
||||||
|
<Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 1 }}>
|
||||||
|
{availableConditions.map((condition) => (
|
||||||
|
<Chip
|
||||||
|
key={condition}
|
||||||
|
label={condition.charAt(0).toUpperCase() + condition.slice(1)}
|
||||||
|
onClick={() => handleConditionToggle(condition)}
|
||||||
|
color={conditions.includes(condition) ? 'primary' : 'default'}
|
||||||
|
variant={conditions.includes(condition) ? 'filled' : 'outlined'}
|
||||||
|
sx={{ cursor: 'pointer' }}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
{/* Rash Indicator */}
|
||||||
|
<FormControl fullWidth sx={{ mb: 3 }}>
|
||||||
|
<InputLabel>Diaper Rash?</InputLabel>
|
||||||
|
<Select
|
||||||
|
value={hasRash ? 'yes' : 'no'}
|
||||||
|
onChange={(e) => setHasRash(e.target.value === 'yes')}
|
||||||
|
label="Diaper Rash?"
|
||||||
|
>
|
||||||
|
<MenuItem value="no">No</MenuItem>
|
||||||
|
<MenuItem value="yes">Yes</MenuItem>
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
|
||||||
|
{/* Rash Severity */}
|
||||||
|
{hasRash && (
|
||||||
|
<Box sx={{ mb: 3 }}>
|
||||||
|
<Alert severity="warning" sx={{ mb: 2 }}>
|
||||||
|
<Typography variant="body2" sx={{ mb: 1 }}>
|
||||||
|
Diaper rash detected. Consider applying diaper rash cream and consulting your pediatrician if it persists.
|
||||||
|
</Typography>
|
||||||
|
</Alert>
|
||||||
|
<FormControl fullWidth>
|
||||||
|
<InputLabel>Rash Severity</InputLabel>
|
||||||
|
<Select
|
||||||
|
value={rashSeverity}
|
||||||
|
onChange={(e) => setRashSeverity(e.target.value as 'mild' | 'moderate' | 'severe')}
|
||||||
|
label="Rash Severity"
|
||||||
|
>
|
||||||
|
<MenuItem value="mild">Mild</MenuItem>
|
||||||
|
<MenuItem value="moderate">Moderate</MenuItem>
|
||||||
|
<MenuItem value="severe">Severe</MenuItem>
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Notes Field */}
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
label="Notes (optional)"
|
||||||
|
multiline
|
||||||
|
rows={3}
|
||||||
|
value={notes}
|
||||||
|
onChange={(e) => setNotes(e.target.value)}
|
||||||
|
sx={{ mb: 3 }}
|
||||||
|
placeholder="Color, consistency, or any concerns..."
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* Submit Button */}
|
||||||
|
<Button
|
||||||
|
fullWidth
|
||||||
|
type="button"
|
||||||
|
variant="contained"
|
||||||
|
size="large"
|
||||||
|
startIcon={<Save />}
|
||||||
|
onClick={handleSubmit}
|
||||||
|
disabled={loading}
|
||||||
|
>
|
||||||
|
{loading ? 'Saving...' : 'Save Diaper Change'}
|
||||||
|
</Button>
|
||||||
|
</Paper>
|
||||||
|
|
||||||
|
{/* Recent Diapers */}
|
||||||
|
<Paper sx={{ p: 3 }}>
|
||||||
|
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
|
||||||
|
<Typography variant="h6" fontWeight="600">
|
||||||
|
Recent Diaper Changes
|
||||||
|
</Typography>
|
||||||
|
<IconButton onClick={loadRecentDiapers} disabled={diapersLoading}>
|
||||||
|
<Refresh />
|
||||||
|
</IconButton>
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
{diapersLoading ? (
|
||||||
|
<Box sx={{ display: 'flex', justifyContent: 'center', py: 4 }}>
|
||||||
|
<CircularProgress size={30} />
|
||||||
|
</Box>
|
||||||
|
) : recentDiapers.length === 0 ? (
|
||||||
|
<Box sx={{ textAlign: 'center', py: 4 }}>
|
||||||
|
<Typography variant="body2" color="text.secondary">
|
||||||
|
No diaper changes yet
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
) : (
|
||||||
|
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
|
||||||
|
{recentDiapers.map((activity, index) => {
|
||||||
|
const data = activity.data as DiaperData;
|
||||||
|
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, fontSize: '2rem' }}>
|
||||||
|
{getDiaperTypeIcon(data.diaperType)}
|
||||||
|
</Box>
|
||||||
|
<Box sx={{ flex: 1 }}>
|
||||||
|
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1, mb: 0.5, flexWrap: 'wrap' }}>
|
||||||
|
<Typography variant="body1" fontWeight="600">
|
||||||
|
Diaper Change
|
||||||
|
</Typography>
|
||||||
|
<Chip
|
||||||
|
label={data.diaperType.charAt(0).toUpperCase() + data.diaperType.slice(1)}
|
||||||
|
size="small"
|
||||||
|
sx={{
|
||||||
|
bgcolor: getDiaperTypeColor(data.diaperType),
|
||||||
|
color: 'white'
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
{data.hasRash && (
|
||||||
|
<Chip
|
||||||
|
icon={<Warning sx={{ fontSize: 16 }} />}
|
||||||
|
label={`Rash: ${data.rashSeverity}`}
|
||||||
|
size="small"
|
||||||
|
color={getRashSeverityColor(data.rashSeverity || 'mild') as any}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<Chip
|
||||||
|
label={formatDistanceToNow(new Date(activity.timestamp), { addSuffix: true })}
|
||||||
|
size="small"
|
||||||
|
variant="outlined"
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
<Typography variant="body2" color="text.secondary" sx={{ mb: 1 }}>
|
||||||
|
{getDiaperDetails(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>
|
</Paper>
|
||||||
</motion.div>
|
</motion.div>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
|
{/* Delete Confirmation Dialog */}
|
||||||
|
<Dialog
|
||||||
|
open={deleteDialogOpen}
|
||||||
|
onClose={() => setDeleteDialogOpen(false)}
|
||||||
|
>
|
||||||
|
<DialogTitle>Delete Diaper Change?</DialogTitle>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogContentText>
|
||||||
|
Are you sure you want to delete this diaper change? This action cannot be undone.
|
||||||
|
</DialogContentText>
|
||||||
|
</DialogContent>
|
||||||
|
<DialogActions>
|
||||||
|
<Button onClick={() => setDeleteDialogOpen(false)} disabled={loading}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button onClick={handleDeleteConfirm} color="error" disabled={loading}>
|
||||||
|
{loading ? 'Deleting...' : 'Delete'}
|
||||||
|
</Button>
|
||||||
|
</DialogActions>
|
||||||
|
</Dialog>
|
||||||
|
|
||||||
|
{/* Success Snackbar */}
|
||||||
|
<Snackbar
|
||||||
|
open={!!successMessage}
|
||||||
|
autoHideDuration={3000}
|
||||||
|
onClose={() => setSuccessMessage(null)}
|
||||||
|
message={successMessage}
|
||||||
|
/>
|
||||||
</AppShell>
|
</AppShell>
|
||||||
</ProtectedRoute>
|
</ProtectedRoute>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -8,89 +8,331 @@ import {
|
|||||||
Paper,
|
Paper,
|
||||||
TextField,
|
TextField,
|
||||||
FormControl,
|
FormControl,
|
||||||
FormLabel,
|
InputLabel,
|
||||||
RadioGroup,
|
Select,
|
||||||
FormControlLabel,
|
MenuItem,
|
||||||
Radio,
|
|
||||||
IconButton,
|
IconButton,
|
||||||
Alert,
|
Alert,
|
||||||
|
Tabs,
|
||||||
|
Tab,
|
||||||
|
CircularProgress,
|
||||||
|
Card,
|
||||||
|
CardContent,
|
||||||
|
Divider,
|
||||||
|
Dialog,
|
||||||
|
DialogTitle,
|
||||||
|
DialogContent,
|
||||||
|
DialogContentText,
|
||||||
|
DialogActions,
|
||||||
Chip,
|
Chip,
|
||||||
|
Snackbar,
|
||||||
} from '@mui/material';
|
} from '@mui/material';
|
||||||
import {
|
import {
|
||||||
ArrowBack,
|
ArrowBack,
|
||||||
PlayArrow,
|
PlayArrow,
|
||||||
Stop,
|
Stop,
|
||||||
|
Refresh,
|
||||||
Save,
|
Save,
|
||||||
Mic,
|
Restaurant,
|
||||||
|
LocalCafe,
|
||||||
|
Fastfood,
|
||||||
|
Delete,
|
||||||
|
Edit,
|
||||||
} from '@mui/icons-material';
|
} from '@mui/icons-material';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { AppShell } from '@/components/layouts/AppShell/AppShell';
|
import { AppShell } from '@/components/layouts/AppShell/AppShell';
|
||||||
import { ProtectedRoute } from '@/components/common/ProtectedRoute';
|
import { ProtectedRoute } from '@/components/common/ProtectedRoute';
|
||||||
|
import { useAuth } from '@/lib/auth/AuthContext';
|
||||||
|
import { trackingApi, Activity } from '@/lib/api/tracking';
|
||||||
|
import { childrenApi, Child } from '@/lib/api/children';
|
||||||
import { motion } from 'framer-motion';
|
import { motion } from 'framer-motion';
|
||||||
import { useForm } from 'react-hook-form';
|
import { formatDistanceToNow } from 'date-fns';
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
|
||||||
import * as z from 'zod';
|
|
||||||
|
|
||||||
const feedingSchema = z.object({
|
interface FeedingData {
|
||||||
type: z.enum(['breast_left', 'breast_right', 'breast_both', 'bottle', 'solid']),
|
feedingType: 'breast' | 'bottle' | 'solid';
|
||||||
amount: z.number().min(0).optional(),
|
side?: 'left' | 'right' | 'both';
|
||||||
unit: z.enum(['ml', 'oz']).optional(),
|
duration?: number;
|
||||||
notes: z.string().optional(),
|
amount?: number;
|
||||||
});
|
bottleType?: 'formula' | 'breastmilk' | 'other';
|
||||||
|
foodDescription?: string;
|
||||||
type FeedingFormData = z.infer<typeof feedingSchema>;
|
amountDescription?: string;
|
||||||
|
}
|
||||||
|
|
||||||
export default function FeedingTrackPage() {
|
export default function FeedingTrackPage() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const { user } = useAuth();
|
||||||
|
const [children, setChildren] = useState<Child[]>([]);
|
||||||
|
const [selectedChild, setSelectedChild] = useState<string>('');
|
||||||
|
const [feedingType, setFeedingType] = useState<'breast' | 'bottle' | 'solid'>('breast');
|
||||||
|
|
||||||
|
// Breastfeeding state
|
||||||
|
const [side, setSide] = useState<'left' | 'right' | 'both'>('left');
|
||||||
|
const [duration, setDuration] = useState<number>(0);
|
||||||
const [isTimerRunning, setIsTimerRunning] = useState(false);
|
const [isTimerRunning, setIsTimerRunning] = useState(false);
|
||||||
const [duration, setDuration] = useState(0);
|
const [timerSeconds, setTimerSeconds] = useState(0);
|
||||||
|
|
||||||
|
// Bottle feeding state
|
||||||
|
const [amount, setAmount] = useState<string>('');
|
||||||
|
const [bottleType, setBottleType] = useState<'formula' | 'breastmilk' | 'other'>('formula');
|
||||||
|
|
||||||
|
// Solid food state
|
||||||
|
const [foodDescription, setFoodDescription] = useState<string>('');
|
||||||
|
const [amountDescription, setAmountDescription] = useState<string>('');
|
||||||
|
|
||||||
|
// Common state
|
||||||
|
const [notes, setNotes] = useState<string>('');
|
||||||
|
const [recentFeedings, setRecentFeedings] = useState<Activity[]>([]);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [childrenLoading, setChildrenLoading] = useState(true);
|
||||||
|
const [feedingsLoading, setFeedingsLoading] = useState(false);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [success, setSuccess] = useState(false);
|
const [successMessage, setSuccessMessage] = useState<string | null>(null);
|
||||||
|
|
||||||
const {
|
// Delete confirmation dialog
|
||||||
register,
|
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
||||||
handleSubmit,
|
const [activityToDelete, setActivityToDelete] = useState<string | null>(null);
|
||||||
watch,
|
|
||||||
formState: { errors },
|
|
||||||
} = useForm<FeedingFormData>({
|
|
||||||
resolver: zodResolver(feedingSchema),
|
|
||||||
defaultValues: {
|
|
||||||
type: 'breast_left',
|
|
||||||
unit: 'ml',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const feedingType = watch('type');
|
const familyId = user?.families?.[0]?.familyId;
|
||||||
|
|
||||||
|
// Load children
|
||||||
|
useEffect(() => {
|
||||||
|
if (familyId) {
|
||||||
|
loadChildren();
|
||||||
|
}
|
||||||
|
}, [familyId]);
|
||||||
|
|
||||||
|
// Load recent feedings when child is selected
|
||||||
|
useEffect(() => {
|
||||||
|
if (selectedChild) {
|
||||||
|
loadRecentFeedings();
|
||||||
|
}
|
||||||
|
}, [selectedChild]);
|
||||||
|
|
||||||
|
// Timer effect
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let interval: NodeJS.Timeout;
|
let interval: NodeJS.Timeout;
|
||||||
if (isTimerRunning) {
|
if (isTimerRunning) {
|
||||||
interval = setInterval(() => {
|
interval = setInterval(() => {
|
||||||
setDuration((prev) => prev + 1);
|
setTimerSeconds((prev) => prev + 1);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
}
|
}
|
||||||
return () => clearInterval(interval);
|
return () => clearInterval(interval);
|
||||||
}, [isTimerRunning]);
|
}, [isTimerRunning]);
|
||||||
|
|
||||||
|
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 loadRecentFeedings = async () => {
|
||||||
|
if (!selectedChild) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
setFeedingsLoading(true);
|
||||||
|
const activities = await trackingApi.getActivities(selectedChild, 'feeding');
|
||||||
|
// 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);
|
||||||
|
setRecentFeedings(sorted);
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('Failed to load recent feedings:', err);
|
||||||
|
} finally {
|
||||||
|
setFeedingsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const formatDuration = (seconds: number) => {
|
const formatDuration = (seconds: number) => {
|
||||||
const mins = Math.floor(seconds / 60);
|
const mins = Math.floor(seconds / 60);
|
||||||
const secs = seconds % 60;
|
const secs = seconds % 60;
|
||||||
return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
|
return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const onSubmit = async (data: FeedingFormData) => {
|
const startTimer = () => {
|
||||||
setError(null);
|
setIsTimerRunning(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const stopTimer = () => {
|
||||||
|
setIsTimerRunning(false);
|
||||||
|
setDuration(Math.floor(timerSeconds / 60));
|
||||||
|
};
|
||||||
|
|
||||||
|
const resetTimer = () => {
|
||||||
|
setIsTimerRunning(false);
|
||||||
|
setTimerSeconds(0);
|
||||||
|
setDuration(0);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = async () => {
|
||||||
|
if (!selectedChild) {
|
||||||
|
setError('Please select a child');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validation
|
||||||
|
if (feedingType === 'breast' && duration === 0 && timerSeconds === 0) {
|
||||||
|
setError('Please enter duration or use the timer');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (feedingType === 'bottle' && !amount) {
|
||||||
|
setError('Please enter amount');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (feedingType === 'solid' && !foodDescription) {
|
||||||
|
setError('Please enter food description');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// TODO: Call API to save feeding data
|
setLoading(true);
|
||||||
console.log('Feeding data:', { ...data, duration });
|
setError(null);
|
||||||
setSuccess(true);
|
|
||||||
setTimeout(() => router.push('/'), 2000);
|
const data: FeedingData = {
|
||||||
|
feedingType,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (feedingType === 'breast') {
|
||||||
|
data.side = side;
|
||||||
|
data.duration = duration || Math.floor(timerSeconds / 60);
|
||||||
|
} else if (feedingType === 'bottle') {
|
||||||
|
data.amount = parseFloat(amount);
|
||||||
|
data.bottleType = bottleType;
|
||||||
|
} else if (feedingType === 'solid') {
|
||||||
|
data.foodDescription = foodDescription;
|
||||||
|
data.amountDescription = amountDescription;
|
||||||
|
}
|
||||||
|
|
||||||
|
await trackingApi.createActivity(selectedChild, {
|
||||||
|
type: 'feeding',
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
data,
|
||||||
|
notes: notes || undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
setSuccessMessage('Feeding logged successfully!');
|
||||||
|
|
||||||
|
// Reset form
|
||||||
|
resetForm();
|
||||||
|
|
||||||
|
// Reload recent feedings
|
||||||
|
await loadRecentFeedings();
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
setError(err.message || 'Failed to log feeding');
|
console.error('Failed to save feeding:', err);
|
||||||
|
setError(err.response?.data?.message || 'Failed to save feeding');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const resetForm = () => {
|
||||||
|
setSide('left');
|
||||||
|
setDuration(0);
|
||||||
|
setTimerSeconds(0);
|
||||||
|
setIsTimerRunning(false);
|
||||||
|
setAmount('');
|
||||||
|
setBottleType('formula');
|
||||||
|
setFoodDescription('');
|
||||||
|
setAmountDescription('');
|
||||||
|
setNotes('');
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteClick = (activityId: string) => {
|
||||||
|
setActivityToDelete(activityId);
|
||||||
|
setDeleteDialogOpen(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteConfirm = async () => {
|
||||||
|
if (!activityToDelete) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
setLoading(true);
|
||||||
|
await trackingApi.deleteActivity(activityToDelete);
|
||||||
|
setSuccessMessage('Feeding deleted successfully');
|
||||||
|
setDeleteDialogOpen(false);
|
||||||
|
setActivityToDelete(null);
|
||||||
|
await loadRecentFeedings();
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('Failed to delete feeding:', err);
|
||||||
|
setError(err.response?.data?.message || 'Failed to delete feeding');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getFeedingTypeIcon = (type: string) => {
|
||||||
|
switch (type) {
|
||||||
|
case 'breast':
|
||||||
|
return <LocalCafe />;
|
||||||
|
case 'bottle':
|
||||||
|
return <Restaurant />;
|
||||||
|
case 'solid':
|
||||||
|
return <Fastfood />;
|
||||||
|
default:
|
||||||
|
return <Restaurant />;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getFeedingDetails = (activity: Activity) => {
|
||||||
|
const data = activity.data as FeedingData;
|
||||||
|
|
||||||
|
if (data.feedingType === 'breast') {
|
||||||
|
return `${data.side?.toUpperCase()} - ${data.duration || 0} min`;
|
||||||
|
} else if (data.feedingType === 'bottle') {
|
||||||
|
return `${data.amount || 0} ml - ${data.bottleType}`;
|
||||||
|
} else if (data.feedingType === 'solid') {
|
||||||
|
return `${data.foodDescription}${data.amountDescription ? ` - ${data.amountDescription}` : ''}`;
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
};
|
||||||
|
|
||||||
|
if (childrenLoading) {
|
||||||
|
return (
|
||||||
|
<ProtectedRoute>
|
||||||
|
<AppShell>
|
||||||
|
<Box sx={{ display: 'flex', justifyContent: 'center', py: 8 }}>
|
||||||
|
<CircularProgress />
|
||||||
|
</Box>
|
||||||
|
</AppShell>
|
||||||
|
</ProtectedRoute>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!familyId || children.length === 0) {
|
||||||
|
return (
|
||||||
|
<ProtectedRoute>
|
||||||
|
<AppShell>
|
||||||
|
<Box>
|
||||||
|
<Alert severity="warning">
|
||||||
|
Please add a child first before tracking feeding activities.
|
||||||
|
</Alert>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
onClick={() => router.push('/children')}
|
||||||
|
sx={{ mt: 2 }}
|
||||||
|
>
|
||||||
|
Go to Children Page
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
</AppShell>
|
||||||
|
</ProtectedRoute>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ProtectedRoute>
|
<ProtectedRoute>
|
||||||
<AppShell>
|
<AppShell>
|
||||||
@@ -104,14 +346,8 @@ export default function FeedingTrackPage() {
|
|||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
{success && (
|
|
||||||
<Alert severity="success" sx={{ mb: 3 }}>
|
|
||||||
Feeding logged successfully!
|
|
||||||
</Alert>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{error && (
|
{error && (
|
||||||
<Alert severity="error" sx={{ mb: 3 }}>
|
<Alert severity="error" sx={{ mb: 3 }} onClose={() => setError(null)}>
|
||||||
{error}
|
{error}
|
||||||
</Alert>
|
</Alert>
|
||||||
)}
|
)}
|
||||||
@@ -121,133 +357,291 @@ export default function FeedingTrackPage() {
|
|||||||
animate={{ opacity: 1, y: 0 }}
|
animate={{ opacity: 1, y: 0 }}
|
||||||
transition={{ duration: 0.3 }}
|
transition={{ duration: 0.3 }}
|
||||||
>
|
>
|
||||||
<Paper sx={{ p: 3, mb: 3 }}>
|
{/* Child Selector */}
|
||||||
{/* Timer Section */}
|
{children.length > 1 && (
|
||||||
<Box sx={{ textAlign: 'center', mb: 4 }}>
|
<Paper sx={{ p: 2, mb: 3 }}>
|
||||||
<Typography variant="h2" fontWeight="600" sx={{ mb: 2 }}>
|
<FormControl fullWidth>
|
||||||
{formatDuration(duration)}
|
<InputLabel>Select Child</InputLabel>
|
||||||
</Typography>
|
<Select
|
||||||
<Box sx={{ display: 'flex', gap: 2, justifyContent: 'center' }}>
|
value={selectedChild}
|
||||||
{!isTimerRunning ? (
|
onChange={(e) => setSelectedChild(e.target.value)}
|
||||||
<Button
|
label="Select Child"
|
||||||
variant="contained"
|
>
|
||||||
size="large"
|
{children.map((child) => (
|
||||||
startIcon={<PlayArrow />}
|
<MenuItem key={child.id} value={child.id}>
|
||||||
onClick={() => setIsTimerRunning(true)}
|
{child.name}
|
||||||
>
|
</MenuItem>
|
||||||
Start Timer
|
))}
|
||||||
</Button>
|
</Select>
|
||||||
) : (
|
|
||||||
<Button
|
|
||||||
variant="contained"
|
|
||||||
color="error"
|
|
||||||
size="large"
|
|
||||||
startIcon={<Stop />}
|
|
||||||
onClick={() => setIsTimerRunning(false)}
|
|
||||||
>
|
|
||||||
Stop Timer
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</Box>
|
|
||||||
</Box>
|
|
||||||
|
|
||||||
<Box component="form" onSubmit={handleSubmit(onSubmit)}>
|
|
||||||
{/* Feeding Type */}
|
|
||||||
<FormControl component="fieldset" sx={{ mb: 3, width: '100%' }}>
|
|
||||||
<FormLabel component="legend" sx={{ mb: 2 }}>
|
|
||||||
Feeding Type
|
|
||||||
</FormLabel>
|
|
||||||
<RadioGroup row>
|
|
||||||
<FormControlLabel
|
|
||||||
value="breast_left"
|
|
||||||
control={<Radio {...register('type')} />}
|
|
||||||
label="Left Breast"
|
|
||||||
/>
|
|
||||||
<FormControlLabel
|
|
||||||
value="breast_right"
|
|
||||||
control={<Radio {...register('type')} />}
|
|
||||||
label="Right Breast"
|
|
||||||
/>
|
|
||||||
<FormControlLabel
|
|
||||||
value="breast_both"
|
|
||||||
control={<Radio {...register('type')} />}
|
|
||||||
label="Both"
|
|
||||||
/>
|
|
||||||
<FormControlLabel
|
|
||||||
value="bottle"
|
|
||||||
control={<Radio {...register('type')} />}
|
|
||||||
label="Bottle"
|
|
||||||
/>
|
|
||||||
<FormControlLabel
|
|
||||||
value="solid"
|
|
||||||
control={<Radio {...register('type')} />}
|
|
||||||
label="Solid Food"
|
|
||||||
/>
|
|
||||||
</RadioGroup>
|
|
||||||
</FormControl>
|
</FormControl>
|
||||||
|
</Paper>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* Amount (for bottle/solid) */}
|
{/* Main Form */}
|
||||||
{(feedingType === 'bottle' || feedingType === 'solid') && (
|
<Paper sx={{ p: 3, mb: 3 }}>
|
||||||
<Box sx={{ display: 'flex', gap: 2, mb: 3 }}>
|
{/* Feeding Type Tabs */}
|
||||||
<TextField
|
<Tabs
|
||||||
fullWidth
|
value={feedingType}
|
||||||
label="Amount"
|
onChange={(_, newValue) => setFeedingType(newValue)}
|
||||||
type="number"
|
sx={{ mb: 3 }}
|
||||||
{...register('amount', { valueAsNumber: true })}
|
variant="fullWidth"
|
||||||
error={!!errors.amount}
|
>
|
||||||
helperText={errors.amount?.message}
|
<Tab label="Breastfeeding" value="breast" icon={<LocalCafe />} iconPosition="start" />
|
||||||
/>
|
<Tab label="Bottle" value="bottle" icon={<Restaurant />} iconPosition="start" />
|
||||||
<FormControl sx={{ minWidth: 120 }}>
|
<Tab label="Solid Food" value="solid" icon={<Fastfood />} iconPosition="start" />
|
||||||
<RadioGroup row>
|
</Tabs>
|
||||||
<FormControlLabel
|
|
||||||
value="ml"
|
{/* Breastfeeding Form */}
|
||||||
control={<Radio {...register('unit')} />}
|
{feedingType === 'breast' && (
|
||||||
label="ml"
|
<Box>
|
||||||
/>
|
{/* Timer Display */}
|
||||||
<FormControlLabel
|
<Box sx={{ textAlign: 'center', mb: 4 }}>
|
||||||
value="oz"
|
<Typography variant="h2" fontWeight="600" sx={{ mb: 2 }}>
|
||||||
control={<Radio {...register('unit')} />}
|
{formatDuration(timerSeconds)}
|
||||||
label="oz"
|
</Typography>
|
||||||
/>
|
<Box sx={{ display: 'flex', gap: 2, justifyContent: 'center' }}>
|
||||||
</RadioGroup>
|
{!isTimerRunning ? (
|
||||||
</FormControl>
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
size="large"
|
||||||
|
startIcon={<PlayArrow />}
|
||||||
|
onClick={startTimer}
|
||||||
|
>
|
||||||
|
Start Timer
|
||||||
|
</Button>
|
||||||
|
) : (
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
color="error"
|
||||||
|
size="large"
|
||||||
|
startIcon={<Stop />}
|
||||||
|
onClick={stopTimer}
|
||||||
|
>
|
||||||
|
Stop Timer
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
<Button
|
||||||
|
variant="outlined"
|
||||||
|
size="large"
|
||||||
|
startIcon={<Refresh />}
|
||||||
|
onClick={resetTimer}
|
||||||
|
>
|
||||||
|
Reset
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Notes */}
|
{/* Side Selector */}
|
||||||
<TextField
|
<FormControl fullWidth sx={{ mb: 3 }}>
|
||||||
fullWidth
|
<InputLabel>Side</InputLabel>
|
||||||
label="Notes (optional)"
|
<Select
|
||||||
multiline
|
value={side}
|
||||||
rows={3}
|
onChange={(e) => setSide(e.target.value as 'left' | 'right' | 'both')}
|
||||||
{...register('notes')}
|
label="Side"
|
||||||
sx={{ mb: 3 }}
|
>
|
||||||
/>
|
<MenuItem value="left">Left</MenuItem>
|
||||||
|
<MenuItem value="right">Right</MenuItem>
|
||||||
|
<MenuItem value="both">Both</MenuItem>
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
|
||||||
{/* Voice Input Button */}
|
{/* Manual Duration Input */}
|
||||||
<Box sx={{ display: 'flex', justifyContent: 'center', mb: 3 }}>
|
<TextField
|
||||||
<Chip
|
fullWidth
|
||||||
icon={<Mic />}
|
label="Duration (minutes)"
|
||||||
label="Use Voice Input"
|
type="number"
|
||||||
onClick={() => {/* TODO: Implement voice input */}}
|
value={duration || ''}
|
||||||
sx={{ cursor: 'pointer' }}
|
onChange={(e) => setDuration(parseInt(e.target.value) || 0)}
|
||||||
|
sx={{ mb: 3 }}
|
||||||
|
helperText="Or use the timer above"
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* Submit Button */}
|
{/* Bottle Form */}
|
||||||
<Button
|
{feedingType === 'bottle' && (
|
||||||
fullWidth
|
<Box>
|
||||||
type="submit"
|
<TextField
|
||||||
variant="contained"
|
fullWidth
|
||||||
size="large"
|
label="Amount (ml)"
|
||||||
startIcon={<Save />}
|
type="number"
|
||||||
>
|
value={amount}
|
||||||
Save Feeding
|
onChange={(e) => setAmount(e.target.value)}
|
||||||
</Button>
|
sx={{ mb: 3 }}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<FormControl fullWidth sx={{ mb: 3 }}>
|
||||||
|
<InputLabel>Type</InputLabel>
|
||||||
|
<Select
|
||||||
|
value={bottleType}
|
||||||
|
onChange={(e) => setBottleType(e.target.value as 'formula' | 'breastmilk' | 'other')}
|
||||||
|
label="Type"
|
||||||
|
>
|
||||||
|
<MenuItem value="formula">Formula</MenuItem>
|
||||||
|
<MenuItem value="breastmilk">Breast Milk</MenuItem>
|
||||||
|
<MenuItem value="other">Other</MenuItem>
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Solid Food Form */}
|
||||||
|
{feedingType === 'solid' && (
|
||||||
|
<Box>
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
label="Food Description"
|
||||||
|
value={foodDescription}
|
||||||
|
onChange={(e) => setFoodDescription(e.target.value)}
|
||||||
|
sx={{ mb: 3 }}
|
||||||
|
placeholder="e.g., Mashed banana, Rice cereal"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
label="Amount (optional)"
|
||||||
|
value={amountDescription}
|
||||||
|
onChange={(e) => setAmountDescription(e.target.value)}
|
||||||
|
sx={{ mb: 3 }}
|
||||||
|
placeholder="e.g., 2 tablespoons, Half bowl"
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Common Notes Field */}
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
label="Notes (optional)"
|
||||||
|
multiline
|
||||||
|
rows={3}
|
||||||
|
value={notes}
|
||||||
|
onChange={(e) => setNotes(e.target.value)}
|
||||||
|
sx={{ mb: 3 }}
|
||||||
|
placeholder="Any additional notes..."
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* Submit Button */}
|
||||||
|
<Button
|
||||||
|
fullWidth
|
||||||
|
type="button"
|
||||||
|
variant="contained"
|
||||||
|
size="large"
|
||||||
|
startIcon={<Save />}
|
||||||
|
onClick={handleSubmit}
|
||||||
|
disabled={loading}
|
||||||
|
>
|
||||||
|
{loading ? 'Saving...' : 'Save Feeding'}
|
||||||
|
</Button>
|
||||||
|
</Paper>
|
||||||
|
|
||||||
|
{/* Recent Feedings */}
|
||||||
|
<Paper sx={{ p: 3 }}>
|
||||||
|
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
|
||||||
|
<Typography variant="h6" fontWeight="600">
|
||||||
|
Recent Feedings
|
||||||
|
</Typography>
|
||||||
|
<IconButton onClick={loadRecentFeedings} disabled={feedingsLoading}>
|
||||||
|
<Refresh />
|
||||||
|
</IconButton>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
|
{feedingsLoading ? (
|
||||||
|
<Box sx={{ display: 'flex', justifyContent: 'center', py: 4 }}>
|
||||||
|
<CircularProgress size={30} />
|
||||||
|
</Box>
|
||||||
|
) : recentFeedings.length === 0 ? (
|
||||||
|
<Box sx={{ textAlign: 'center', py: 4 }}>
|
||||||
|
<Typography variant="body2" color="text.secondary">
|
||||||
|
No feeding activities yet
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
) : (
|
||||||
|
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
|
||||||
|
{recentFeedings.map((activity, index) => {
|
||||||
|
const data = activity.data as FeedingData;
|
||||||
|
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 }}>
|
||||||
|
{getFeedingTypeIcon(data.feedingType)}
|
||||||
|
</Box>
|
||||||
|
<Box sx={{ flex: 1 }}>
|
||||||
|
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1, mb: 0.5 }}>
|
||||||
|
<Typography variant="body1" fontWeight="600">
|
||||||
|
{data.feedingType.charAt(0).toUpperCase() + data.feedingType.slice(1)}
|
||||||
|
</Typography>
|
||||||
|
<Chip
|
||||||
|
label={formatDistanceToNow(new Date(activity.timestamp), { addSuffix: true })}
|
||||||
|
size="small"
|
||||||
|
variant="outlined"
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
<Typography variant="body2" color="text.secondary" sx={{ mb: 1 }}>
|
||||||
|
{getFeedingDetails(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>
|
</Paper>
|
||||||
</motion.div>
|
</motion.div>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
|
{/* Delete Confirmation Dialog */}
|
||||||
|
<Dialog
|
||||||
|
open={deleteDialogOpen}
|
||||||
|
onClose={() => setDeleteDialogOpen(false)}
|
||||||
|
>
|
||||||
|
<DialogTitle>Delete Feeding Activity?</DialogTitle>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogContentText>
|
||||||
|
Are you sure you want to delete this feeding activity? This action cannot be undone.
|
||||||
|
</DialogContentText>
|
||||||
|
</DialogContent>
|
||||||
|
<DialogActions>
|
||||||
|
<Button onClick={() => setDeleteDialogOpen(false)} disabled={loading}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button onClick={handleDeleteConfirm} color="error" disabled={loading}>
|
||||||
|
{loading ? 'Deleting...' : 'Delete'}
|
||||||
|
</Button>
|
||||||
|
</DialogActions>
|
||||||
|
</Dialog>
|
||||||
|
|
||||||
|
{/* Success Snackbar */}
|
||||||
|
<Snackbar
|
||||||
|
open={!!successMessage}
|
||||||
|
autoHideDuration={3000}
|
||||||
|
onClose={() => setSuccessMessage(null)}
|
||||||
|
message={successMessage}
|
||||||
|
/>
|
||||||
</AppShell>
|
</AppShell>
|
||||||
</ProtectedRoute>
|
</ProtectedRoute>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,105 +1,352 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import {
|
import {
|
||||||
Box,
|
Box,
|
||||||
Typography,
|
Typography,
|
||||||
Button,
|
Button,
|
||||||
Paper,
|
Paper,
|
||||||
TextField,
|
TextField,
|
||||||
|
FormControl,
|
||||||
|
InputLabel,
|
||||||
|
Select,
|
||||||
|
MenuItem,
|
||||||
IconButton,
|
IconButton,
|
||||||
Alert,
|
Alert,
|
||||||
|
CircularProgress,
|
||||||
|
Card,
|
||||||
|
CardContent,
|
||||||
|
Dialog,
|
||||||
|
DialogTitle,
|
||||||
|
DialogContent,
|
||||||
|
DialogContentText,
|
||||||
|
DialogActions,
|
||||||
Chip,
|
Chip,
|
||||||
FormControl,
|
Snackbar,
|
||||||
FormLabel,
|
|
||||||
RadioGroup,
|
|
||||||
FormControlLabel,
|
|
||||||
Radio,
|
|
||||||
} from '@mui/material';
|
} from '@mui/material';
|
||||||
import {
|
import {
|
||||||
ArrowBack,
|
ArrowBack,
|
||||||
Bedtime,
|
Refresh,
|
||||||
WbSunny,
|
|
||||||
Save,
|
Save,
|
||||||
Mic,
|
Delete,
|
||||||
|
Bedtime,
|
||||||
|
Hotel,
|
||||||
|
DirectionsCar,
|
||||||
|
Chair,
|
||||||
|
Home,
|
||||||
} from '@mui/icons-material';
|
} from '@mui/icons-material';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { AppShell } from '@/components/layouts/AppShell/AppShell';
|
import { AppShell } from '@/components/layouts/AppShell/AppShell';
|
||||||
import { ProtectedRoute } from '@/components/common/ProtectedRoute';
|
import { ProtectedRoute } from '@/components/common/ProtectedRoute';
|
||||||
|
import { useAuth } from '@/lib/auth/AuthContext';
|
||||||
|
import { trackingApi, Activity } from '@/lib/api/tracking';
|
||||||
|
import { childrenApi, Child } from '@/lib/api/children';
|
||||||
import { motion } from 'framer-motion';
|
import { motion } from 'framer-motion';
|
||||||
import { useForm } from 'react-hook-form';
|
import { formatDistanceToNow, format } from 'date-fns';
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
|
||||||
import * as z from 'zod';
|
|
||||||
import { format } from 'date-fns';
|
|
||||||
|
|
||||||
const sleepSchema = z.object({
|
interface SleepData {
|
||||||
startTime: z.string(),
|
startTime: string;
|
||||||
endTime: z.string(),
|
endTime?: string;
|
||||||
quality: z.enum(['excellent', 'good', 'fair', 'poor']),
|
quality: 'excellent' | 'good' | 'fair' | 'poor';
|
||||||
notes: z.string().optional(),
|
location: string;
|
||||||
}).refine((data) => new Date(data.endTime) > new Date(data.startTime), {
|
isOngoing?: boolean;
|
||||||
message: 'End time must be after start time',
|
}
|
||||||
path: ['endTime'],
|
|
||||||
});
|
|
||||||
|
|
||||||
type SleepFormData = z.infer<typeof sleepSchema>;
|
|
||||||
|
|
||||||
export default function SleepTrackPage() {
|
export default function SleepTrackPage() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const { user } = useAuth();
|
||||||
|
const [children, setChildren] = useState<Child[]>([]);
|
||||||
|
const [selectedChild, setSelectedChild] = useState<string>('');
|
||||||
|
|
||||||
|
// Sleep state
|
||||||
|
const [startTime, setStartTime] = useState<string>(
|
||||||
|
format(new Date(), "yyyy-MM-dd'T'HH:mm")
|
||||||
|
);
|
||||||
|
const [endTime, setEndTime] = useState<string>(
|
||||||
|
format(new Date(), "yyyy-MM-dd'T'HH:mm")
|
||||||
|
);
|
||||||
|
const [quality, setQuality] = useState<'excellent' | 'good' | 'fair' | 'poor'>('good');
|
||||||
|
const [location, setLocation] = useState<string>('crib');
|
||||||
|
const [isOngoing, setIsOngoing] = useState<boolean>(false);
|
||||||
|
|
||||||
|
// Common state
|
||||||
|
const [notes, setNotes] = useState<string>('');
|
||||||
|
const [recentSleeps, setRecentSleeps] = useState<Activity[]>([]);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [childrenLoading, setChildrenLoading] = useState(true);
|
||||||
|
const [sleepsLoading, setSleepsLoading] = useState(false);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [success, setSuccess] = useState(false);
|
const [successMessage, setSuccessMessage] = useState<string | null>(null);
|
||||||
|
|
||||||
const {
|
// Delete confirmation dialog
|
||||||
register,
|
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
||||||
handleSubmit,
|
const [activityToDelete, setActivityToDelete] = useState<string | null>(null);
|
||||||
setValue,
|
|
||||||
watch,
|
|
||||||
formState: { errors },
|
|
||||||
} = useForm<SleepFormData>({
|
|
||||||
resolver: zodResolver(sleepSchema),
|
|
||||||
defaultValues: {
|
|
||||||
quality: 'good',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const startTime = watch('startTime');
|
const familyId = user?.families?.[0]?.familyId;
|
||||||
const endTime = watch('endTime');
|
|
||||||
|
// Load children
|
||||||
|
useEffect(() => {
|
||||||
|
if (familyId) {
|
||||||
|
loadChildren();
|
||||||
|
}
|
||||||
|
}, [familyId]);
|
||||||
|
|
||||||
|
// Load recent sleeps when child is selected
|
||||||
|
useEffect(() => {
|
||||||
|
if (selectedChild) {
|
||||||
|
loadRecentSleeps();
|
||||||
|
}
|
||||||
|
}, [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 loadRecentSleeps = async () => {
|
||||||
|
if (!selectedChild) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
setSleepsLoading(true);
|
||||||
|
const activities = await trackingApi.getActivities(selectedChild, 'sleep');
|
||||||
|
// 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);
|
||||||
|
setRecentSleeps(sorted);
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('Failed to load recent sleeps:', err);
|
||||||
|
} finally {
|
||||||
|
setSleepsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const formatDuration = (start: string, end?: string) => {
|
||||||
|
const startDate = new Date(start);
|
||||||
|
const endDate = end ? new Date(end) : new Date();
|
||||||
|
const diffMs = endDate.getTime() - startDate.getTime();
|
||||||
|
|
||||||
|
if (diffMs < 0) return 'Invalid duration';
|
||||||
|
|
||||||
|
const hours = Math.floor(diffMs / (1000 * 60 * 60));
|
||||||
|
const minutes = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60));
|
||||||
|
|
||||||
|
if (hours === 0) {
|
||||||
|
return `${minutes} minute${minutes !== 1 ? 's' : ''}`;
|
||||||
|
} else if (minutes === 0) {
|
||||||
|
return `${hours} hour${hours !== 1 ? 's' : ''}`;
|
||||||
|
} else {
|
||||||
|
return `${hours} hour${hours !== 1 ? 's' : ''} ${minutes} minute${minutes !== 1 ? 's' : ''}`;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const calculateDuration = () => {
|
const calculateDuration = () => {
|
||||||
if (!startTime || !endTime) return null;
|
if (!startTime) return null;
|
||||||
|
if (isOngoing) {
|
||||||
|
return formatDuration(startTime);
|
||||||
|
}
|
||||||
|
if (!endTime) return null;
|
||||||
|
|
||||||
const start = new Date(startTime);
|
const start = new Date(startTime);
|
||||||
const end = new Date(endTime);
|
const end = new Date(endTime);
|
||||||
const diff = end.getTime() - start.getTime();
|
|
||||||
if (diff < 0) return null;
|
|
||||||
|
|
||||||
const hours = Math.floor(diff / (1000 * 60 * 60));
|
if (end <= start) return null;
|
||||||
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
|
|
||||||
return `${hours}h ${minutes}m`;
|
return formatDuration(startTime, endTime);
|
||||||
};
|
};
|
||||||
|
|
||||||
const setStartNow = () => {
|
const setStartNow = () => {
|
||||||
const now = format(new Date(), "yyyy-MM-dd'T'HH:mm");
|
setStartTime(format(new Date(), "yyyy-MM-dd'T'HH:mm"));
|
||||||
setValue('startTime', now);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const setEndNow = () => {
|
const setEndNow = () => {
|
||||||
const now = format(new Date(), "yyyy-MM-dd'T'HH:mm");
|
setEndTime(format(new Date(), "yyyy-MM-dd'T'HH:mm"));
|
||||||
setValue('endTime', now);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const onSubmit = async (data: SleepFormData) => {
|
const handleSubmit = async () => {
|
||||||
setError(null);
|
if (!selectedChild) {
|
||||||
|
setError('Please select a child');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validation
|
||||||
|
if (!startTime) {
|
||||||
|
setError('Please enter start time');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isOngoing && !endTime) {
|
||||||
|
setError('Please enter end time or mark as ongoing');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isOngoing && endTime) {
|
||||||
|
const start = new Date(startTime);
|
||||||
|
const end = new Date(endTime);
|
||||||
|
if (end <= start) {
|
||||||
|
setError('End time must be after start time');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// TODO: Call API to save sleep data
|
setLoading(true);
|
||||||
console.log('Sleep data:', data);
|
setError(null);
|
||||||
setSuccess(true);
|
|
||||||
setTimeout(() => router.push('/'), 2000);
|
const data: SleepData = {
|
||||||
|
startTime,
|
||||||
|
quality,
|
||||||
|
location,
|
||||||
|
isOngoing,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!isOngoing && endTime) {
|
||||||
|
data.endTime = endTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
await trackingApi.createActivity(selectedChild, {
|
||||||
|
type: 'sleep',
|
||||||
|
timestamp: startTime,
|
||||||
|
data,
|
||||||
|
notes: notes || undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
setSuccessMessage('Sleep logged successfully!');
|
||||||
|
|
||||||
|
// Reset form
|
||||||
|
resetForm();
|
||||||
|
|
||||||
|
// Reload recent sleeps
|
||||||
|
await loadRecentSleeps();
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
setError(err.message || 'Failed to log sleep');
|
console.error('Failed to save sleep:', err);
|
||||||
|
setError(err.response?.data?.message || 'Failed to save sleep');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const resetForm = () => {
|
||||||
|
setStartTime(format(new Date(), "yyyy-MM-dd'T'HH:mm"));
|
||||||
|
setEndTime(format(new Date(), "yyyy-MM-dd'T'HH:mm"));
|
||||||
|
setQuality('good');
|
||||||
|
setLocation('crib');
|
||||||
|
setIsOngoing(false);
|
||||||
|
setNotes('');
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteClick = (activityId: string) => {
|
||||||
|
setActivityToDelete(activityId);
|
||||||
|
setDeleteDialogOpen(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteConfirm = async () => {
|
||||||
|
if (!activityToDelete) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
setLoading(true);
|
||||||
|
await trackingApi.deleteActivity(activityToDelete);
|
||||||
|
setSuccessMessage('Sleep deleted successfully');
|
||||||
|
setDeleteDialogOpen(false);
|
||||||
|
setActivityToDelete(null);
|
||||||
|
await loadRecentSleeps();
|
||||||
|
} catch (err: any) {
|
||||||
|
console.error('Failed to delete sleep:', err);
|
||||||
|
setError(err.response?.data?.message || 'Failed to delete sleep');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getLocationIcon = (loc: string) => {
|
||||||
|
switch (loc) {
|
||||||
|
case 'crib':
|
||||||
|
return <Hotel />;
|
||||||
|
case 'bed':
|
||||||
|
return <Bedtime />;
|
||||||
|
case 'stroller':
|
||||||
|
return <DirectionsCar />;
|
||||||
|
case 'carrier':
|
||||||
|
return <Chair />;
|
||||||
|
case 'other':
|
||||||
|
return <Home />;
|
||||||
|
default:
|
||||||
|
return <Hotel />;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getQualityColor = (qual: string) => {
|
||||||
|
switch (qual) {
|
||||||
|
case 'excellent':
|
||||||
|
return 'success';
|
||||||
|
case 'good':
|
||||||
|
return 'primary';
|
||||||
|
case 'fair':
|
||||||
|
return 'warning';
|
||||||
|
case 'poor':
|
||||||
|
return 'error';
|
||||||
|
default:
|
||||||
|
return 'default';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getSleepDetails = (activity: Activity) => {
|
||||||
|
const data = activity.data as SleepData;
|
||||||
|
const duration = data.endTime
|
||||||
|
? formatDuration(data.startTime, data.endTime)
|
||||||
|
: data.isOngoing
|
||||||
|
? `Ongoing - ${formatDuration(data.startTime)}`
|
||||||
|
: 'No end time';
|
||||||
|
|
||||||
|
return `${duration} - ${data.location.charAt(0).toUpperCase() + data.location.slice(1)}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (childrenLoading) {
|
||||||
|
return (
|
||||||
|
<ProtectedRoute>
|
||||||
|
<AppShell>
|
||||||
|
<Box sx={{ display: 'flex', justifyContent: 'center', py: 8 }}>
|
||||||
|
<CircularProgress />
|
||||||
|
</Box>
|
||||||
|
</AppShell>
|
||||||
|
</ProtectedRoute>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!familyId || children.length === 0) {
|
||||||
|
return (
|
||||||
|
<ProtectedRoute>
|
||||||
|
<AppShell>
|
||||||
|
<Box>
|
||||||
|
<Alert severity="warning">
|
||||||
|
Please add a child first before tracking sleep activities.
|
||||||
|
</Alert>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
onClick={() => router.push('/children')}
|
||||||
|
sx={{ mt: 2 }}
|
||||||
|
>
|
||||||
|
Go to Children Page
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
</AppShell>
|
||||||
|
</ProtectedRoute>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ProtectedRoute>
|
<ProtectedRoute>
|
||||||
<AppShell>
|
<AppShell>
|
||||||
@@ -113,14 +360,8 @@ export default function SleepTrackPage() {
|
|||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
{success && (
|
|
||||||
<Alert severity="success" sx={{ mb: 3 }}>
|
|
||||||
Sleep logged successfully!
|
|
||||||
</Alert>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{error && (
|
{error && (
|
||||||
<Alert severity="error" sx={{ mb: 3 }}>
|
<Alert severity="error" sx={{ mb: 3 }} onClose={() => setError(null)}>
|
||||||
{error}
|
{error}
|
||||||
</Alert>
|
</Alert>
|
||||||
)}
|
)}
|
||||||
@@ -130,46 +371,74 @@ export default function SleepTrackPage() {
|
|||||||
animate={{ opacity: 1, y: 0 }}
|
animate={{ opacity: 1, y: 0 }}
|
||||||
transition={{ duration: 0.3 }}
|
transition={{ duration: 0.3 }}
|
||||||
>
|
>
|
||||||
<Paper sx={{ p: 3, mb: 3 }}>
|
{/* Child Selector */}
|
||||||
<Box component="form" onSubmit={handleSubmit(onSubmit)}>
|
{children.length > 1 && (
|
||||||
{/* Start Time */}
|
<Paper sx={{ p: 2, mb: 3 }}>
|
||||||
<Box sx={{ mb: 3 }}>
|
<FormControl fullWidth>
|
||||||
<Box sx={{ display: 'flex', alignItems: 'center', gap: 2, mb: 1 }}>
|
<InputLabel>Select Child</InputLabel>
|
||||||
<Bedtime color="primary" />
|
<Select
|
||||||
<Typography variant="h6" fontWeight="600">
|
value={selectedChild}
|
||||||
Sleep Start
|
onChange={(e) => setSelectedChild(e.target.value)}
|
||||||
</Typography>
|
label="Select Child"
|
||||||
</Box>
|
>
|
||||||
<Box sx={{ display: 'flex', gap: 2, alignItems: 'flex-start' }}>
|
{children.map((child) => (
|
||||||
<TextField
|
<MenuItem key={child.id} value={child.id}>
|
||||||
fullWidth
|
{child.name}
|
||||||
type="datetime-local"
|
</MenuItem>
|
||||||
{...register('startTime')}
|
))}
|
||||||
error={!!errors.startTime}
|
</Select>
|
||||||
helperText={errors.startTime?.message}
|
</FormControl>
|
||||||
InputLabelProps={{ shrink: true }}
|
</Paper>
|
||||||
/>
|
)}
|
||||||
<Button variant="outlined" onClick={setStartNow} sx={{ minWidth: 100 }}>
|
|
||||||
Now
|
|
||||||
</Button>
|
|
||||||
</Box>
|
|
||||||
</Box>
|
|
||||||
|
|
||||||
{/* End Time */}
|
{/* Main Form */}
|
||||||
|
<Paper sx={{ p: 3, mb: 3 }}>
|
||||||
|
{/* Start Time */}
|
||||||
|
<Box sx={{ mb: 3 }}>
|
||||||
|
<Typography variant="subtitle1" fontWeight="600" sx={{ mb: 1 }}>
|
||||||
|
Sleep Start Time
|
||||||
|
</Typography>
|
||||||
|
<Box sx={{ display: 'flex', gap: 2, alignItems: 'flex-start' }}>
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
type="datetime-local"
|
||||||
|
value={startTime}
|
||||||
|
onChange={(e) => setStartTime(e.target.value)}
|
||||||
|
InputLabelProps={{ shrink: true }}
|
||||||
|
/>
|
||||||
|
<Button variant="outlined" onClick={setStartNow} sx={{ minWidth: 100 }}>
|
||||||
|
Now
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
{/* Ongoing Checkbox */}
|
||||||
|
<Box sx={{ mb: 3 }}>
|
||||||
|
<FormControl fullWidth>
|
||||||
|
<InputLabel>Sleep Status</InputLabel>
|
||||||
|
<Select
|
||||||
|
value={isOngoing ? 'ongoing' : 'completed'}
|
||||||
|
onChange={(e) => setIsOngoing(e.target.value === 'ongoing')}
|
||||||
|
label="Sleep Status"
|
||||||
|
>
|
||||||
|
<MenuItem value="completed">Completed (has end time)</MenuItem>
|
||||||
|
<MenuItem value="ongoing">Ongoing (still sleeping)</MenuItem>
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
{/* End Time */}
|
||||||
|
{!isOngoing && (
|
||||||
<Box sx={{ mb: 3 }}>
|
<Box sx={{ mb: 3 }}>
|
||||||
<Box sx={{ display: 'flex', alignItems: 'center', gap: 2, mb: 1 }}>
|
<Typography variant="subtitle1" fontWeight="600" sx={{ mb: 1 }}>
|
||||||
<WbSunny color="warning" />
|
Wake Up Time
|
||||||
<Typography variant="h6" fontWeight="600">
|
</Typography>
|
||||||
Wake Up
|
|
||||||
</Typography>
|
|
||||||
</Box>
|
|
||||||
<Box sx={{ display: 'flex', gap: 2, alignItems: 'flex-start' }}>
|
<Box sx={{ display: 'flex', gap: 2, alignItems: 'flex-start' }}>
|
||||||
<TextField
|
<TextField
|
||||||
fullWidth
|
fullWidth
|
||||||
type="datetime-local"
|
type="datetime-local"
|
||||||
{...register('endTime')}
|
value={endTime}
|
||||||
error={!!errors.endTime}
|
onChange={(e) => setEndTime(e.target.value)}
|
||||||
helperText={errors.endTime?.message}
|
|
||||||
InputLabelProps={{ shrink: true }}
|
InputLabelProps={{ shrink: true }}
|
||||||
/>
|
/>
|
||||||
<Button variant="outlined" onClick={setEndNow} sx={{ minWidth: 100 }}>
|
<Button variant="outlined" onClick={setEndNow} sx={{ minWidth: 100 }}>
|
||||||
@@ -177,82 +446,189 @@ export default function SleepTrackPage() {
|
|||||||
</Button>
|
</Button>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* Duration Display */}
|
{/* Duration Display */}
|
||||||
{calculateDuration() && (
|
{calculateDuration() && (
|
||||||
<Box sx={{ mb: 3, textAlign: 'center' }}>
|
<Box sx={{ mb: 3, textAlign: 'center' }}>
|
||||||
<Chip
|
|
||||||
label={`Duration: ${calculateDuration()}`}
|
|
||||||
color="primary"
|
|
||||||
sx={{ fontSize: '1rem', py: 3 }}
|
|
||||||
/>
|
|
||||||
</Box>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Sleep Quality */}
|
|
||||||
<FormControl component="fieldset" sx={{ mb: 3, width: '100%' }}>
|
|
||||||
<FormLabel component="legend" sx={{ mb: 2 }}>
|
|
||||||
Sleep Quality
|
|
||||||
</FormLabel>
|
|
||||||
<RadioGroup row>
|
|
||||||
<FormControlLabel
|
|
||||||
value="excellent"
|
|
||||||
control={<Radio {...register('quality')} />}
|
|
||||||
label="Excellent"
|
|
||||||
/>
|
|
||||||
<FormControlLabel
|
|
||||||
value="good"
|
|
||||||
control={<Radio {...register('quality')} />}
|
|
||||||
label="Good"
|
|
||||||
/>
|
|
||||||
<FormControlLabel
|
|
||||||
value="fair"
|
|
||||||
control={<Radio {...register('quality')} />}
|
|
||||||
label="Fair"
|
|
||||||
/>
|
|
||||||
<FormControlLabel
|
|
||||||
value="poor"
|
|
||||||
control={<Radio {...register('quality')} />}
|
|
||||||
label="Poor"
|
|
||||||
/>
|
|
||||||
</RadioGroup>
|
|
||||||
</FormControl>
|
|
||||||
|
|
||||||
{/* Notes */}
|
|
||||||
<TextField
|
|
||||||
fullWidth
|
|
||||||
label="Notes (optional)"
|
|
||||||
multiline
|
|
||||||
rows={3}
|
|
||||||
{...register('notes')}
|
|
||||||
sx={{ mb: 3 }}
|
|
||||||
placeholder="Any disruptions, dreams, or observations..."
|
|
||||||
/>
|
|
||||||
|
|
||||||
{/* Voice Input Button */}
|
|
||||||
<Box sx={{ display: 'flex', justifyContent: 'center', mb: 3 }}>
|
|
||||||
<Chip
|
<Chip
|
||||||
icon={<Mic />}
|
label={`Duration: ${calculateDuration()}`}
|
||||||
label="Use Voice Input"
|
color="primary"
|
||||||
onClick={() => {/* TODO: Implement voice input */}}
|
sx={{ fontSize: '1rem', py: 3 }}
|
||||||
sx={{ cursor: 'pointer' }}
|
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* Submit Button */}
|
{/* Sleep Quality */}
|
||||||
<Button
|
<FormControl fullWidth sx={{ mb: 3 }}>
|
||||||
fullWidth
|
<InputLabel>Sleep Quality</InputLabel>
|
||||||
type="submit"
|
<Select
|
||||||
variant="contained"
|
value={quality}
|
||||||
size="large"
|
onChange={(e) => setQuality(e.target.value as 'excellent' | 'good' | 'fair' | 'poor')}
|
||||||
startIcon={<Save />}
|
label="Sleep Quality"
|
||||||
>
|
>
|
||||||
Save Sleep Session
|
<MenuItem value="excellent">Excellent</MenuItem>
|
||||||
</Button>
|
<MenuItem value="good">Good</MenuItem>
|
||||||
|
<MenuItem value="fair">Fair</MenuItem>
|
||||||
|
<MenuItem value="poor">Poor</MenuItem>
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
|
||||||
|
{/* Location */}
|
||||||
|
<FormControl fullWidth sx={{ mb: 3 }}>
|
||||||
|
<InputLabel>Location</InputLabel>
|
||||||
|
<Select
|
||||||
|
value={location}
|
||||||
|
onChange={(e) => setLocation(e.target.value)}
|
||||||
|
label="Location"
|
||||||
|
>
|
||||||
|
<MenuItem value="crib">Crib</MenuItem>
|
||||||
|
<MenuItem value="bed">Bed</MenuItem>
|
||||||
|
<MenuItem value="stroller">Stroller</MenuItem>
|
||||||
|
<MenuItem value="carrier">Carrier</MenuItem>
|
||||||
|
<MenuItem value="other">Other</MenuItem>
|
||||||
|
</Select>
|
||||||
|
</FormControl>
|
||||||
|
|
||||||
|
{/* Common Notes Field */}
|
||||||
|
<TextField
|
||||||
|
fullWidth
|
||||||
|
label="Notes (optional)"
|
||||||
|
multiline
|
||||||
|
rows={3}
|
||||||
|
value={notes}
|
||||||
|
onChange={(e) => setNotes(e.target.value)}
|
||||||
|
sx={{ mb: 3 }}
|
||||||
|
placeholder="Any disruptions, dreams, or observations..."
|
||||||
|
/>
|
||||||
|
|
||||||
|
{/* Submit Button */}
|
||||||
|
<Button
|
||||||
|
fullWidth
|
||||||
|
type="button"
|
||||||
|
variant="contained"
|
||||||
|
size="large"
|
||||||
|
startIcon={<Save />}
|
||||||
|
onClick={handleSubmit}
|
||||||
|
disabled={loading}
|
||||||
|
>
|
||||||
|
{loading ? 'Saving...' : 'Save Sleep'}
|
||||||
|
</Button>
|
||||||
|
</Paper>
|
||||||
|
|
||||||
|
{/* Recent Sleeps */}
|
||||||
|
<Paper sx={{ p: 3 }}>
|
||||||
|
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
|
||||||
|
<Typography variant="h6" fontWeight="600">
|
||||||
|
Recent Sleep Activities
|
||||||
|
</Typography>
|
||||||
|
<IconButton onClick={loadRecentSleeps} disabled={sleepsLoading}>
|
||||||
|
<Refresh />
|
||||||
|
</IconButton>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
|
{sleepsLoading ? (
|
||||||
|
<Box sx={{ display: 'flex', justifyContent: 'center', py: 4 }}>
|
||||||
|
<CircularProgress size={30} />
|
||||||
|
</Box>
|
||||||
|
) : recentSleeps.length === 0 ? (
|
||||||
|
<Box sx={{ textAlign: 'center', py: 4 }}>
|
||||||
|
<Typography variant="body2" color="text.secondary">
|
||||||
|
No sleep activities yet
|
||||||
|
</Typography>
|
||||||
|
</Box>
|
||||||
|
) : (
|
||||||
|
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
|
||||||
|
{recentSleeps.map((activity, index) => {
|
||||||
|
const data = activity.data as SleepData;
|
||||||
|
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 }}>
|
||||||
|
{getLocationIcon(data.location)}
|
||||||
|
</Box>
|
||||||
|
<Box sx={{ flex: 1 }}>
|
||||||
|
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1, mb: 0.5, flexWrap: 'wrap' }}>
|
||||||
|
<Typography variant="body1" fontWeight="600">
|
||||||
|
Sleep
|
||||||
|
</Typography>
|
||||||
|
<Chip
|
||||||
|
label={data.quality.charAt(0).toUpperCase() + data.quality.slice(1)}
|
||||||
|
size="small"
|
||||||
|
color={getQualityColor(data.quality) as any}
|
||||||
|
/>
|
||||||
|
<Chip
|
||||||
|
label={formatDistanceToNow(new Date(activity.timestamp), { addSuffix: true })}
|
||||||
|
size="small"
|
||||||
|
variant="outlined"
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
<Typography variant="body2" color="text.secondary" sx={{ mb: 1 }}>
|
||||||
|
{getSleepDetails(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>
|
</Paper>
|
||||||
</motion.div>
|
</motion.div>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
|
{/* Delete Confirmation Dialog */}
|
||||||
|
<Dialog
|
||||||
|
open={deleteDialogOpen}
|
||||||
|
onClose={() => setDeleteDialogOpen(false)}
|
||||||
|
>
|
||||||
|
<DialogTitle>Delete Sleep Activity?</DialogTitle>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogContentText>
|
||||||
|
Are you sure you want to delete this sleep activity? This action cannot be undone.
|
||||||
|
</DialogContentText>
|
||||||
|
</DialogContent>
|
||||||
|
<DialogActions>
|
||||||
|
<Button onClick={() => setDeleteDialogOpen(false)} disabled={loading}>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button onClick={handleDeleteConfirm} color="error" disabled={loading}>
|
||||||
|
{loading ? 'Deleting...' : 'Delete'}
|
||||||
|
</Button>
|
||||||
|
</DialogActions>
|
||||||
|
</Dialog>
|
||||||
|
|
||||||
|
{/* Success Snackbar */}
|
||||||
|
<Snackbar
|
||||||
|
open={!!successMessage}
|
||||||
|
autoHideDuration={3000}
|
||||||
|
onClose={() => setSuccessMessage(null)}
|
||||||
|
message={successMessage}
|
||||||
|
/>
|
||||||
</AppShell>
|
</AppShell>
|
||||||
</ProtectedRoute>
|
</ProtectedRoute>
|
||||||
);
|
);
|
||||||
|
|||||||
23
maternal-web/lib/api/users.ts
Normal file
23
maternal-web/lib/api/users.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import apiClient from './client';
|
||||||
|
|
||||||
|
export interface UpdateProfileData {
|
||||||
|
name?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UserProfile {
|
||||||
|
id: string;
|
||||||
|
email: string;
|
||||||
|
name: string;
|
||||||
|
role: string;
|
||||||
|
locale: string;
|
||||||
|
emailVerified: boolean;
|
||||||
|
families?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export const usersApi = {
|
||||||
|
// Update user profile
|
||||||
|
updateProfile: async (data: UpdateProfileData): Promise<UserProfile> => {
|
||||||
|
const response = await apiClient.patch('/api/v1/auth/profile', data);
|
||||||
|
return response.data.data;
|
||||||
|
},
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user