'use client' import { useEffect, useState } from 'react' import { useRouter } from 'next/navigation' import { RenterAuthError, loadRenterNotifications, markAllRenterNotificationsRead, markRenterNotificationRead, type RenterNotification, } from '@/lib/renter' import { useMarketplacePreferences } from '@/components/MarketplaceShell' export default function RenterNotificationsPage() { const router = useRouter() const { language } = useMarketplacePreferences() const dict = { en: { load: 'Could not load notifications.', updateOne: 'Could not update the notification.', updateAll: 'Could not update notifications.', renter: 'Renter', title: 'Notifications', markAll: 'Mark all read', back: 'Back to dashboard', loading: 'Loading notifications…', empty: 'No renter notifications yet.', read: 'Read', markRead: 'Mark read', }, fr: { load: 'Impossible de charger les notifications.', updateOne: 'Impossible de mettre à jour la notification.', updateAll: 'Impossible de mettre à jour les notifications.', renter: 'Client', title: 'Notifications', markAll: 'Tout marquer comme lu', back: 'Retour au tableau de bord', loading: 'Chargement des notifications…', empty: 'Aucune notification client pour le moment.', read: 'Lu', markRead: 'Marquer comme lu', }, ar: { load: 'تعذر تحميل الإشعارات.', updateOne: 'تعذر تحديث الإشعار.', updateAll: 'تعذر تحديث الإشعارات.', renter: 'المستأجر', title: 'الإشعارات', markAll: 'تعيين الكل كمقروء', back: 'العودة إلى بوابة المستأجر', loading: 'جارٍ تحميل الإشعارات…', empty: 'لا توجد إشعارات للمستأجر حالياً.', read: 'مقروء', markRead: 'تعيين كمقروء', }, }[language] const [notifications, setNotifications] = useState([]) const [errorMsg, setErrorMsg] = useState('') const [loading, setLoading] = useState(true) const [updating, setUpdating] = useState(false) useEffect(() => { loadRenterNotifications() .then((items) => setNotifications(items)) .catch((err) => { if (err instanceof RenterAuthError) { router.replace('/renter/sign-in') return } setErrorMsg(err instanceof Error ? err.message : dict.load) }) .finally(() => setLoading(false)) }, [router]) async function markRead(id: string) { setUpdating(true) try { await markRenterNotificationRead(id) setNotifications((current) => current.map((item) => (item.id === id ? { ...item, readAt: item.readAt ?? new Date().toISOString() } : item)), ) } catch (err) { setErrorMsg(err instanceof Error ? err.message : dict.updateOne) } finally { setUpdating(false) } } async function markAllRead() { setUpdating(true) try { await markAllRenterNotificationsRead() setNotifications((current) => current.map((item) => ({ ...item, readAt: item.readAt ?? new Date().toISOString() }))) } catch (err) { setErrorMsg(err instanceof Error ? err.message : dict.updateAll) } finally { setUpdating(false) } } return (

{dict.title}

{loading ?
{dict.loading}
: null} {!loading && errorMsg ?
{errorMsg}
: null} {!loading && !errorMsg && notifications.length === 0 ? (
{dict.empty}
) : null} {!loading && !errorMsg && notifications.length > 0 ? (
{notifications.map((notification) => (

{notification.type.replaceAll('_', ' ')}

{notification.title}

{notification.readAt ? ( {dict.read} ) : ( )}

{notification.body}

{new Date(notification.createdAt).toLocaleString()}

))}
) : null}
) }