import { useState, useEffect } from 'react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Switch } from '@/components/ui/switch'; import { Label } from '@/components/ui/label'; import { Bell, Mail, MessageSquare, Loader2, CheckCircle, AlertCircle } from 'lucide-react'; import { getNotificationPreferences, updateNotificationPreferences, NotificationPreferences } from '@/services/userPreferenceApi'; export function NotificationPreferencesCard() { const [preferences, setPreferences] = useState({ emailNotificationsEnabled: true, pushNotificationsEnabled: true, inAppNotificationsEnabled: true }); const [loading, setLoading] = useState(true); const [updating, setUpdating] = useState(null); const [successMessage, setSuccessMessage] = useState(null); const [error, setError] = useState(null); useEffect(() => { loadPreferences(); }, []); const loadPreferences = async () => { try { setLoading(true); setError(null); const data = await getNotificationPreferences(); setPreferences(data); } catch (err: any) { console.error('[NotificationPreferences] Failed to load preferences:', err); setError(err.response?.data?.message || 'Failed to load notification preferences'); } finally { setLoading(false); } }; const handleToggle = async (key: keyof NotificationPreferences, value: boolean) => { try { setUpdating(key); setError(null); setSuccessMessage(null); const updateData = { [key]: value }; const updated = await updateNotificationPreferences(updateData); setPreferences(updated); // Show success message const prefName = key === 'emailNotificationsEnabled' ? 'Email' : key === 'pushNotificationsEnabled' ? 'Push' : 'In-App'; setSuccessMessage(`${prefName} notifications ${value ? 'enabled' : 'disabled'}`); setTimeout(() => setSuccessMessage(null), 3000); } catch (err: any) { console.error('[NotificationPreferences] Failed to update preference:', err); setError(err.response?.data?.message || 'Failed to update notification preference'); // Revert the UI change loadPreferences(); } finally { setUpdating(null); } }; if (loading) { return ( ); } return (
Notification Preferences Control how you receive notifications
{/* Success Message */} {successMessage && (

{successMessage}

)} {/* Error Message */} {error && (

{error}

)} {/* Email Notifications */}

Receive notifications via email

{updating === 'emailNotificationsEnabled' && ( )} handleToggle('emailNotificationsEnabled', checked)} disabled={updating === 'emailNotificationsEnabled'} />
{/* Push Notifications */}

Receive browser push notifications

{updating === 'pushNotificationsEnabled' && ( )} handleToggle('pushNotificationsEnabled', checked)} disabled={updating === 'pushNotificationsEnabled'} />
{/* In-App Notifications */}

Show notifications within the application

{updating === 'inAppNotificationsEnabled' && ( )} handleToggle('inAppNotificationsEnabled', checked)} disabled={updating === 'inAppNotificationsEnabled'} />

These preferences control which notification channels you receive alerts through. You'll still receive critical notifications regardless of these settings.

); }