'use client'; import { useState, useEffect } from 'react'; import { Box, Typography, CircularProgress, Alert, ToggleButtonGroup, ToggleButton } from '@mui/material'; import { BarChart, Bar, LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer, PieChart, Pie, Cell, } from 'recharts'; import { format, subDays } from 'date-fns'; import apiClient from '@/lib/api/client'; interface FeedingData { date: string; breastfeeding: number; bottle: number; solids: number; total: number; } interface FeedingTypeData { name: string; value: number; color: string; [key: string]: string | number; } const COLORS = { breastfeeding: '#FFB6C1', bottle: '#FFA5B0', solids: '#FF94A5', }; export default function FeedingFrequencyGraph() { const [data, setData] = useState([]); const [typeData, setTypeData] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [chartType, setChartType] = useState<'bar' | 'line'>('bar'); useEffect(() => { fetchFeedingData(); }, []); const fetchFeedingData = async () => { try { setIsLoading(true); const endDate = new Date(); const startDate = subDays(endDate, 6); const response = await apiClient.get('/api/v1/activities/feeding', { params: { startDate: startDate.toISOString(), endDate: endDate.toISOString(), }, }); const feedingActivities = response.data.data; const dailyData: { [key: string]: FeedingData } = {}; // Initialize 7 days of data for (let i = 0; i < 7; i++) { const date = subDays(endDate, 6 - i); const dateStr = format(date, 'MMM dd'); dailyData[dateStr] = { date: dateStr, breastfeeding: 0, bottle: 0, solids: 0, total: 0, }; } // Count feeding types by day const typeCounts = { breastfeeding: 0, bottle: 0, solids: 0, }; feedingActivities.forEach((activity: any) => { const dateStr = format(new Date(activity.timestamp), 'MMM dd'); if (dailyData[dateStr]) { const type = activity.type?.toLowerCase() || 'bottle'; if (type === 'breastfeeding' || type === 'breast') { dailyData[dateStr].breastfeeding += 1; typeCounts.breastfeeding += 1; } else if (type === 'bottle' || type === 'formula') { dailyData[dateStr].bottle += 1; typeCounts.bottle += 1; } else if (type === 'solids' || type === 'solid') { dailyData[dateStr].solids += 1; typeCounts.solids += 1; } dailyData[dateStr].total += 1; } }); setData(Object.values(dailyData)); // Prepare pie chart data const pieData: FeedingTypeData[] = [ { name: 'Breastfeeding', value: typeCounts.breastfeeding, color: COLORS.breastfeeding, }, { name: 'Bottle', value: typeCounts.bottle, color: COLORS.bottle, }, { name: 'Solids', value: typeCounts.solids, color: COLORS.solids, }, ].filter((item) => item.value > 0); setTypeData(pieData); } catch (err: any) { console.error('Failed to fetch feeding data:', err); setError(err.response?.data?.message || 'Failed to load feeding data'); } finally { setIsLoading(false); } }; const handleChartTypeChange = ( event: React.MouseEvent, newType: 'bar' | 'line' | null ) => { if (newType !== null) { setChartType(newType); } }; if (isLoading) { return ( ); } if (error) { return ( {error} ); } return ( Weekly Feeding Patterns Track feeding frequency and types over the past 7 days Bar Line {/* Feeding Frequency Chart */} Daily Feeding Frequency by Type {chartType === 'bar' ? ( ) : ( )} {/* Feeding Type Distribution */} {typeData.length > 0 && ( Feeding Type Distribution (7 days) `${name}: ${(percent * 100).toFixed(0)}%`} outerRadius={100} fill="#8884d8" dataKey="value" > {typeData.map((entry, index) => ( ))} )} ); }