/** * PreferencesForm Component * User notification preferences management */ import React, { useState, useEffect } from 'react'; interface NotificationPreferences { email: boolean; push: boolean; inApp: boolean; plantReminders: boolean; transportAlerts: boolean; farmAlerts: boolean; harvestAlerts: boolean; demandMatches: boolean; weeklyDigest: boolean; quietHoursStart?: string; quietHoursEnd?: string; timezone?: string; } interface PreferencesFormProps { userId?: string; onSave?: (preferences: NotificationPreferences) => void; } export function PreferencesForm({ userId = 'demo-user', onSave }: PreferencesFormProps) { const [preferences, setPreferences] = useState({ email: true, push: true, inApp: true, plantReminders: true, transportAlerts: true, farmAlerts: true, harvestAlerts: true, demandMatches: true, weeklyDigest: true }); const [isLoading, setIsLoading] = useState(true); const [isSaving, setIsSaving] = useState(false); const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null); useEffect(() => { fetchPreferences(); }, [userId]); async function fetchPreferences() { try { const response = await fetch(`/api/notifications/preferences?userId=${userId}`); const data = await response.json(); if (data.success) { setPreferences(data.data); } } catch (error) { console.error('Failed to fetch preferences:', error); } finally { setIsLoading(false); } } async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setIsSaving(true); setMessage(null); try { const response = await fetch('/api/notifications/preferences', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ...preferences, userId }) }); const data = await response.json(); if (data.success) { setMessage({ type: 'success', text: 'Preferences saved successfully!' }); onSave?.(data.data); } else { setMessage({ type: 'error', text: data.error || 'Failed to save preferences' }); } } catch (error) { setMessage({ type: 'error', text: 'Failed to save preferences' }); } finally { setIsSaving(false); } } function handleToggle(key: keyof NotificationPreferences) { setPreferences(prev => ({ ...prev, [key]: !prev[key] })); } if (isLoading) { return (

Loading preferences...

); } return (
{message && (
{message.text}
)} {/* Notification Channels */}

Notification Channels

Choose how you want to receive notifications

handleToggle('email')} /> handleToggle('push')} /> handleToggle('inApp')} />
{/* Notification Types */}

Notification Types

Choose which types of notifications you want to receive

handleToggle('plantReminders')} /> handleToggle('transportAlerts')} /> handleToggle('farmAlerts')} /> handleToggle('harvestAlerts')} /> handleToggle('demandMatches')} /> handleToggle('weeklyDigest')} />
{/* Quiet Hours */}

Quiet Hours

Set times when you don't want to receive notifications

setPreferences(prev => ({ ...prev, quietHoursStart: e.target.value })) } className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500" />
setPreferences(prev => ({ ...prev, quietHoursEnd: e.target.value })) } className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500" />
{/* Submit */}
); } interface ToggleRowProps { label: string; description: string; enabled: boolean; onChange: () => void; } function ToggleRow({ label, description, enabled, onChange }: ToggleRowProps) { return (

{label}

{description}

); }