// EmailPreferencesSection - Gestione preferenze notifiche email // Component for managing email notification preferences function EmailPreferencesSection() { const toast = useToast(); const [preferences, setPreferences] = React.useState([]); const [loading, setLoading] = React.useState(true); const [updating, setUpdating] = React.useState(null); // Track which preference is being updated const [bulkUpdating, setBulkUpdating] = React.useState(false); // Fetch preferences on mount React.useEffect(() => { fetchPreferences(); }, []); const fetchPreferences = async () => { setLoading(true); try { const data = await ApiUtils.getEmailPreferences(); setPreferences(data.preferences || []); } catch (error) { console.error('Error fetching email preferences:', error); toast('Errore nel caricamento delle preferenze email', 'error'); } finally { setLoading(false); } }; const handleToggle = async (notificationType, currentEnabled) => { setUpdating(notificationType); try { await ApiUtils.updateSingleEmailPreference(notificationType, !currentEnabled); setPreferences(prev => prev.map(pref => pref.notification_type === notificationType ? { ...pref, email_enabled: !currentEnabled } : pref ) ); toast('Preferenza aggiornata', 'success'); } catch (error) { console.error('Error updating preference:', error); toast('Errore nell\'aggiornamento della preferenza', 'error'); } finally { setUpdating(null); } }; const handleEnableAll = async () => { setBulkUpdating(true); try { await ApiUtils.enableAllEmailPreferences(); setPreferences(prev => prev.map(pref => ({ ...pref, email_enabled: true }))); toast('Tutte le notifiche email attivate', 'success'); } catch (error) { console.error('Error enabling all preferences:', error); toast('Errore nell\'attivazione delle notifiche', 'error'); } finally { setBulkUpdating(false); } }; const handleDisableAll = async () => { setBulkUpdating(true); try { await ApiUtils.disableAllEmailPreferences(); setPreferences(prev => prev.map(pref => ({ ...pref, email_enabled: false }))); toast('Tutte le notifiche email disattivate', 'success'); } catch (error) { console.error('Error disabling all preferences:', error); toast('Errore nella disattivazione delle notifiche', 'error'); } finally { setBulkUpdating(false); } }; // Count enabled preferences const enabledCount = preferences.filter(p => p.email_enabled).length; const allEnabled = enabledCount === preferences.length && preferences.length > 0; const allDisabled = enabledCount === 0; if (loading) { return (
Gestisci quali notifiche ricevere via email
Nessuna preferenza email disponibile
{pref.display_name}
{getNotificationDescription(pref.notification_type)}
Nota
Le notifiche in-app continueranno ad essere ricevute indipendentemente da queste impostazioni. Queste preferenze controllano solo le notifiche via email.