import React, { useState } from 'react'; import { IonButton, IonButtons, IonContent, IonHeader, IonIcon, IonPage, IonSkeletonText, IonTitle, IonToolbar, useIonViewWillEnter, } from '@ionic/react'; import { alertCircleOutline, chevronBackOutline, chevronForwardOutline, notificationsOutline, } from 'ionicons/icons'; import { useHistory } from 'react-router-dom'; import { supabase } from '../supabase'; import { useAuth } from '../contexts/AuthContext'; import '../styles/activity.css'; import '../styles/recipients.css'; type NotificationRow = { id: string; title: string; body: string; priority: string; type: string; read_at: string | null; created_at: string; order_id: string | null; voucher_id: string | null; }; const NotificationsPage: React.FC = () => { const history = useHistory(); const { user } = useAuth(); const [notifications, setNotifications] = useState([]); const [loading, setLoading] = useState(true); const [markingAll, setMarkingAll] = useState(false); const [error, setError] = useState(null); useIonViewWillEnter(() => { void loadNotifications(); }); const showError = (message: string) => { setError(message); setTimeout(() => setError(null), 4000); }; const loadNotifications = async () => { if (!user) return; setLoading(true); setError(null); const { data, error: loadError } = await supabase .from('notifications') .select( 'id,title,body,priority,type,read_at,created_at,order_id,voucher_id' ) .eq('user_id', user.id) .order('created_at', { ascending: false }); if (loadError) { showError(loadError.message); setLoading(false); return; } setNotifications((data ?? []) as NotificationRow[]); setLoading(false); }; const markRead = async (notificationId: string) => { const readAt = new Date().toISOString(); setNotifications((current) => current.map((item) => item.id === notificationId ? { ...item, read_at: readAt } : item ) ); await supabase.functions.invoke('mark-notification-read', { body: { notificationId }, }); }; const handleOpenNotification = async (notification: NotificationRow) => { if (!notification.read_at) { await markRead(notification.id); } if (notification.voucher_id) { history.push(`/voucher/${notification.voucher_id}`); } else if (notification.order_id) { history.push(`/activity/${notification.order_id}`); } }; const handleMarkAllRead = async () => { if (!user) return; setMarkingAll(true); const readAt = new Date().toISOString(); const { error: updateError } = await supabase .from('notifications') .update({ read_at: readAt }) .eq('user_id', user.id) .is('read_at', null); if (updateError) { showError(updateError.message); } else { setNotifications((current) => current.map((item) => ({ ...item, read_at: item.read_at ?? readAt })) ); } setMarkingAll(false); }; return ( history.length > 1 ? history.goBack() : history.replace('/profile') } aria-label="Go back" > Notifications Mark all {error && (

{error}

)} {loading ? (
{[1, 2, 3].map((item) => (
))}
) : notifications.length === 0 ? (

No notifications yet

Important payment and voucher updates will appear here.

) : (
{notifications.map((notification) => ( ))}
)}
); }; export default NotificationsPage;