'use client'; import { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import api from '@/lib/api'; import { useAuthStore } from '@/stores/authStore'; import { LoadingSpinner } from '@/components/ui/LoadingSpinner'; import { ErrorMessage } from '@/components/ui/ErrorMessage'; import type { Beneficiary } from '@/types'; /** * Dashboard Page * * Main dashboard showing overview of beneficiaries and sensor status. * Displays: * - Summary statistics (total beneficiaries, active sensors, etc.) * - List of all beneficiaries with their status * - Quick actions (add beneficiary, view equipment) */ export default function DashboardPage() { const router = useRouter(); const { isAuthenticated, user } = useAuthStore(); const [beneficiaries, setBeneficiaries] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); // Fetch beneficiaries on mount useEffect(() => { if (!isAuthenticated) { router.push('/login'); return; } const fetchBeneficiaries = async () => { setIsLoading(true); setError(null); try { const response = await api.getAllBeneficiaries(); if (response.ok && response.data) { setBeneficiaries(response.data); } else { setError(response.error?.message || 'Failed to load beneficiaries'); } } catch (err) { setError(err instanceof Error ? err.message : 'An unexpected error occurred'); } finally { setIsLoading(false); } }; fetchBeneficiaries(); }, [isAuthenticated, router]); // Calculate summary statistics const totalBeneficiaries = beneficiaries.length; const withEquipment = beneficiaries.filter(b => b.hasDevices).length; const activeSubscriptions = beneficiaries.filter(b => b.subscription?.status === 'active').length; const handleAddBeneficiary = () => { router.push('/add-loved-one'); }; const handleViewBeneficiary = (id: number) => { router.push(`/beneficiaries/${id}`); }; if (isLoading) { return (
); } if (error) { return ( window.location.reload()} /> ); } return (
{/* Header */}

Welcome{user?.firstName ? `, ${user.firstName}` : ''}

Monitor your loved ones and manage their health sensors

{/* Summary Cards */}
{/* Beneficiaries List */}

Your Loved Ones

{beneficiaries.length === 0 ? (
👤

No beneficiaries yet

Add your first loved one to start monitoring their health and safety.

) : (
{beneficiaries.map((beneficiary) => ( handleViewBeneficiary(beneficiary.id)} /> ))}
)}
); } /** * Summary Card Component */ interface SummaryCardProps { title: string; value: number; icon: string; color: 'blue' | 'green' | 'purple'; } function SummaryCard({ title, value, icon, color }: SummaryCardProps) { const colorClasses = { blue: 'bg-blue-50 text-blue-600', green: 'bg-green-50 text-green-600', purple: 'bg-purple-50 text-purple-600', }; return (

{title}

{value}

{icon}
); } /** * Beneficiary Card Component */ interface BeneficiaryCardProps { beneficiary: Beneficiary; onClick: () => void; } function BeneficiaryCard({ beneficiary, onClick }: BeneficiaryCardProps) { const getEquipmentStatusBadge = () => { if (!beneficiary.equipmentStatus) { return { text: 'No Equipment', color: 'bg-slate-100 text-slate-700' }; } const statusMap: Record = { none: { text: 'No Equipment', color: 'bg-slate-100 text-slate-700' }, ordered: { text: 'Ordered', color: 'bg-blue-100 text-blue-700' }, shipped: { text: 'Shipped', color: 'bg-yellow-100 text-yellow-700' }, delivered: { text: 'Delivered', color: 'bg-green-100 text-green-700' }, active: { text: 'Active', color: 'bg-green-100 text-green-700' }, demo: { text: 'Demo', color: 'bg-purple-100 text-purple-700' }, }; return statusMap[beneficiary.equipmentStatus] || statusMap.none; }; const getSubscriptionBadge = () => { if (!beneficiary.subscription) { return null; } const statusMap: Record = { active: { text: 'Active', color: 'bg-green-100 text-green-700' }, trial: { text: 'Trial', color: 'bg-blue-100 text-blue-700' }, expired: { text: 'Expired', color: 'bg-red-100 text-red-700' }, cancelled: { text: 'Cancelled', color: 'bg-slate-100 text-slate-700' }, }; const badge = statusMap[beneficiary.subscription.status]; if (!badge) return null; return ( {badge.text} ); }; const equipmentBadge = getEquipmentStatusBadge(); const subscriptionBadge = getSubscriptionBadge(); return ( ); }