'use client'; import { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import { useAuth } from '@/contexts/AuthContext'; import type { BookedAppointment, NotificationSettings } from '@/lib/types'; import toast from 'react-hot-toast'; const formatTimestamp = (value: any) => { if (!value) return ''; if (typeof value === 'string') return new Date(value).toLocaleString(); if (value?.seconds) return new Date(value.seconds * 1000).toLocaleString(); return value.toString(); }; const getFallbackSettings = (userId: string): NotificationSettings => ({ userId, notifyOnSuccess: true, notifyOnFailure: true, emailSummary: false, autoAddToCalendar: false, googleCalendarConnected: false, iCloudCalendarConnected: false, createdAt: null as unknown as NotificationSettings['createdAt'], updatedAt: null as unknown as NotificationSettings['updatedAt'], }); export default function NotificationsPage() { const { user, isLoading } = useAuth(); const router = useRouter(); const [settings, setSettings] = useState(null); const [appointments, setAppointments] = useState([]); const [isSaving, setIsSaving] = useState(false); useEffect(() => { if (!isLoading && !user) { router.push('/auth'); } }, [user, isLoading, router]); const fetchSettings = async () => { try { const response = await fetch('/api/notifications'); if (!response.ok) { toast.error('Unable to load notification settings.'); setSettings(getFallbackSettings(user?.uid ?? 'test-user-id')); return; } const data = await response.json(); setSettings(data.settings ?? getFallbackSettings(user?.uid ?? 'test-user-id')); } catch (error) { toast.error('Unable to load notification settings.'); setSettings(getFallbackSettings(user?.uid ?? 'test-user-id')); } }; const fetchAppointments = async () => { try { const response = await fetch('/api/appointments'); if (!response.ok) { throw new Error('Failed to load appointments'); } const data = await response.json(); setAppointments(data.appointments || []); } catch (error) { console.error('Error loading appointments', error); toast.error('Unable to load appointments.'); } }; useEffect(() => { if (user) { fetchSettings(); fetchAppointments(); } }, [user]); const toggleSetting = (key: 'notifyOnSuccess' | 'notifyOnFailure' | 'emailSummary') => { if (!settings) return; setSettings({ ...settings, [key]: !settings[key] }); }; const handleSave = async () => { if (!settings || isSaving) return; setIsSaving(true); try { const payload = { notifyOnSuccess: settings.notifyOnSuccess, notifyOnFailure: settings.notifyOnFailure, emailSummary: settings.emailSummary, autoAddToCalendar: settings.autoAddToCalendar, googleCalendarConnected: settings.googleCalendarConnected, iCloudCalendarConnected: settings.iCloudCalendarConnected, }; const response = await fetch('/api/notifications', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }); if (!response.ok) throw new Error('Failed'); await fetchSettings(); toast.success('Notification settings saved.'); } catch (error) { console.error('Error saving settings', error); toast.error('Unable to save settings.'); } finally { setIsSaving(false); } }; if (isLoading || !settings) { return (
Loading...
); } return (

Notifications & Calendar

Preferences

{[ { key: 'notifyOnSuccess', label: 'Successful booking' }, { key: 'notifyOnFailure', label: 'Call failure' }, { key: 'emailSummary', label: 'Email summary' }, ].map((item) => (

{item.label}

))}

Integrations

Upcoming

{appointments.length === 0 && (
No upcoming appointments yet.
)} {appointments.map((appointment) => (

{formatTimestamp(appointment.dateTime)}

{appointment.title}

{appointment.location && (

{appointment.location}

)}
{appointment.calendarEventId ? 'GOOGLE' : 'ICLOUD'}
))}
); }