diff --git a/src/app/(app)/notifications/page.tsx b/src/app/(app)/notifications/page.tsx index daffdbc..86eb931 100644 --- a/src/app/(app)/notifications/page.tsx +++ b/src/app/(app)/notifications/page.tsx @@ -1,60 +1,149 @@ "use client"; -import { useState } from "react"; +import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; +import { useFamily } from "@/app/FamilyProvider"; +import { fmtDate } from "@/lib/date-ist"; interface Notification { id: string; + type: string; title: string; message: string; - time: string; - read: boolean; + dueDate?: string; + status?: string; + childName?: string; +} + +const READ_KEY = "tia_read_notifications"; + +function getReadSet(): Set { + try { + const raw = localStorage.getItem(READ_KEY); + return new Set(raw ? JSON.parse(raw) : []); + } catch { return new Set(); } +} + +function saveReadSet(ids: Set) { + localStorage.setItem(READ_KEY, JSON.stringify([...ids])); } export default function NotificationsPage() { const router = useRouter(); - const [notifications] = useState([ - { id: "1", title: "Reminder", message: "Time to log today's feed", time: "2 hours ago", read: false }, - { id: "2", title: "Growth Update", message: "New growth data saved", time: "Yesterday", read: true }, - ]); + const { childId } = useFamily(); + const [notifications, setNotifications] = useState([]); + const [readIds, setReadIds] = useState>(new Set()); + const [loading, setLoading] = useState(true); + + useEffect(() => { + setReadIds(getReadSet()); + }, []); + + useEffect(() => { + if (!childId) return; + fetch(`/api/notifications?childId=${childId}`) + .then(r => r.ok ? r.json() : { notifications: [] }) + .then(d => setNotifications(d.notifications || [])) + .catch(() => setNotifications([])) + .finally(() => setLoading(false)); + }, [childId]); + + const markRead = (id: string) => { + setReadIds(prev => { + const next = new Set(prev); + next.add(id); + saveReadSet(next); + return next; + }); + }; + + const markAllRead = () => { + const allIds = new Set(notifications.map(n => n.id)); + setReadIds(allIds); + saveReadSet(allIds); + }; + + const unreadCount = notifications.filter(n => !readIds.has(n.id)).length; return (
-
- + {/* Header */} +
+

Notifications

+ {unreadCount > 0 && ( + + {unreadCount} + + )} + {unreadCount > 0 && ( + + )}
- {notifications.length === 0 ? ( + {loading ? ( +
+
πŸ””
+

Loading…

+
+ ) : notifications.length === 0 ? (
πŸ””
-

No notifications

+

All caught up!

+

No overdue or due-today vaccinations

) : ( - notifications.map((notif) => ( -
-
-
-
{notif.title}
-
{notif.message}
-
{notif.time}
+ notifications.map(notif => { + const isRead = readIds.has(notif.id); + const isOverdue = notif.status === "overdue"; + return ( + + ); + }) )}
{notifications.length > 0 && ( -
- +
+

+ Showing overdue & due-today IAP vaccinations for {notifications[0]?.childName || "your child"} +

)}
); -} \ No newline at end of file +}