"use client"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { useRouter } from "next/navigation"; import { motion, AnimatePresence } from "framer-motion"; import { Bell, CheckCircle, XCircle, Calendar, MessageCircle, CheckCheck, Loader2 } from "lucide-react"; import AuthService from "@/app/services/AuthService"; import { getUserNotifications } from "@/app/utils/api"; import Loading from "../loading"; export default function NotificationsPage() { const { t } = useTranslation(); const router = useRouter(); const [notifications, setNotifications] = useState([]); const [unreadCount, setUnreadCount] = useState(0); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { if (!AuthService.isAuthenticated()) { router.push("/login"); return; } fetchNotifications(); }, [router]); const fetchNotifications = async () => { setIsLoading(true); setError(null); try { const data = await getUserNotifications(); const items = Array.isArray(data) ? data : []; setNotifications(items); setUnreadCount(items.length); } catch (err) { console.error("Error fetching notifications:", err); setError(err.message || t("fetch-notifications-failed")); } finally { setIsLoading(false); } }; const markAsRead = (id) => { setNotifications((prev) => prev.map((n) => (n.id === id ? { ...n, read: true } : n))); setUnreadCount((prev) => Math.max(0, prev - 1)); }; const markAllAsRead = () => { setNotifications((prev) => prev.map((n) => ({ ...n, read: true }))); setUnreadCount(0); }; if (isLoading) { return (
); } if (error) { return (

{t("loading-error")}

{error}

{t("retry")}
); } return (

{t("notifications")}

{unreadCount > 0 ? t("unread-notifications", { count: unreadCount }) : t("all-notifications-read")}

{unreadCount > 0 && ( {t("mark-all-read")} )}
{notifications.length === 0 ? (

{t("no-notifications")}

{t("notifications-empty-hint")}

) : (
{notifications.map((notification, index) => ( markAsRead(notification.id)} className={`bg-white rounded-2xl shadow-sm border transition-all hover:shadow-md cursor-pointer ${!notification.read ? "border-amber-200 bg-amber-50/50" : "border-gray-200"}`} >
{!notification.read ? : }

{notification.title}

{notification.message &&

{notification.message}

}
{notification.date && ( {notification.date} )} {notification.type && ( {notification.type} )}
{!notification.read && }
))}
)}
); }