'use client'; import { useState, useEffect, useCallback } from 'react'; import { useSession } from 'next-auth/react'; import api from '@/services/api'; interface NotificationSetting { id: string; label: string; description: string; enabled: boolean; } interface NotificationsFormProps { onSave?: (data: { emailNotifications: NotificationSetting[]; pushNotifications: NotificationSetting[]; }) => void; } // Default notification settings (all disabled by default) const DEFAULT_EMAIL_NOTIFICATIONS: NotificationSetting[] = [ { id: 'new_leads', label: 'New Leads', description: 'Get notified when you receive new leads', enabled: false, }, { id: 'messages', label: 'Messages', description: 'Get notified when you receive new messages', enabled: false, }, { id: 'connection_requests', label: 'Connection Requests', description: 'Get notified about new connection requests', enabled: false, }, { id: 'property_updates', label: 'Property Updates', description: 'Get notified about property status changes', enabled: false, }, { id: 'marketing', label: 'Marketing & Promotions', description: 'Receive marketing emails and promotions', enabled: false, }, ]; const DEFAULT_PUSH_NOTIFICATIONS: NotificationSetting[] = [ { id: 'push_messages', label: 'Messages', description: 'Push notifications for new messages', enabled: false, }, { id: 'push_leads', label: 'New Leads', description: 'Push notifications for new leads', enabled: false, }, { id: 'push_reminders', label: 'Reminders', description: 'Push notifications for scheduled reminders', enabled: false, }, ]; export function NotificationsForm({ onSave }: NotificationsFormProps) { const { data: session } = useSession(); const [isLoading, setIsLoading] = useState(true); const [isSaving, setIsSaving] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); const [emailNotifications, setEmailNotifications] = useState(DEFAULT_EMAIL_NOTIFICATIONS); const [pushNotifications, setPushNotifications] = useState(DEFAULT_PUSH_NOTIFICATIONS); // Fetch saved preferences on mount const fetchPreferences = useCallback(async () => { if (!session) return; try { setIsLoading(true); const role = (session?.user as any)?.role; const endpoint = role === 'AGENT' ? '/agents/preferences/notifications' : '/users/preferences/notifications'; const response = await api.get(endpoint); const savedPrefs = response.data?.data || response.data || {}; // Merge saved preferences with defaults if (savedPrefs.email) { setEmailNotifications((prev) => prev.map((item) => ({ ...item, enabled: savedPrefs.email[item.id] ?? item.enabled, })) ); } if (savedPrefs.push) { setPushNotifications((prev) => prev.map((item) => ({ ...item, enabled: savedPrefs.push[item.id] ?? item.enabled, })) ); } } catch (err) { // If no saved preferences, use defaults (all disabled) console.log('No saved notification preferences found, using defaults'); } finally { setIsLoading(false); } }, [session]); useEffect(() => { fetchPreferences(); }, [fetchPreferences]); const toggleEmailNotification = (id: string) => { setEmailNotifications((prev) => prev.map((item) => item.id === id ? { ...item, enabled: !item.enabled } : item ) ); }; const togglePushNotification = (id: string) => { setPushNotifications((prev) => prev.map((item) => item.id === id ? { ...item, enabled: !item.enabled } : item ) ); }; const handleSave = async () => { setIsSaving(true); setError(null); setSuccess(null); try { const role = (session?.user as any)?.role; const endpoint = role === 'AGENT' ? '/agents/preferences/notifications' : '/users/preferences/notifications'; // Transform settings to API format const preferences = { email: emailNotifications.reduce((acc, item) => ({ ...acc, [item.id]: item.enabled }), {}), push: pushNotifications.reduce((acc, item) => ({ ...acc, [item.id]: item.enabled }), {}), }; await api.put(endpoint, preferences); setSuccess('Notification preferences saved successfully!'); if (onSave) { onSave({ emailNotifications, pushNotifications }); } // Clear success message after 3 seconds setTimeout(() => setSuccess(null), 3000); } catch (err: unknown) { const error = err as { response?: { data?: { message?: string } } }; setError(error.response?.data?.message || 'Failed to save notification preferences. Please try again.'); } finally { setIsSaving(false); } }; const ToggleSwitch = ({ enabled, onToggle, }: { enabled: boolean; onToggle: () => void; }) => ( ); if (isLoading) { return (
); } return ( <> {/* Error Message */} {error && (
{error}
)} {/* Success Message */} {success && (
{success}
)} {/* Email Notifications */}

Email Notifications

Manage your email notification preferences

{emailNotifications.map((item) => (

{item.label}

{item.description}

toggleEmailNotification(item.id)} />
))}
{/* Push Notifications */}

Push Notifications

Manage your push notification preferences

{pushNotifications.map((item) => (

{item.label}

{item.description}

togglePushNotification(item.id)} />
))}
{/* Save Button */}
); }