'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'; 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 (
{t('loading')}
{error}
{unreadCount > 0 ? t('unread-notifications', { count: unreadCount }) : t('all-notifications-read')}
{t('notifications-empty-hint')}
{notification.message}
)}