// 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 (
Caricamento preferenze...
); } return (

Notifiche Email

Gestisci quali notifiche ricevere via email

{enabledCount}/{preferences.length} attive
{/* Bulk Actions */}
{/* Preferences List */} {preferences.length === 0 ? (

Nessuna preferenza email disponibile

) : (
{preferences.map((pref) => (
{getNotificationIcon(pref.notification_type)}

{pref.display_name}

{getNotificationDescription(pref.notification_type)}

))}
)} {/* Info Note */}

Nota

Le notifiche in-app continueranno ad essere ricevute indipendentemente da queste impostazioni. Queste preferenze controllano solo le notifiche via email.

); } // Helper function to get notification icon based on type function getNotificationIcon(type) { const icons = { project_created: ( ), project_assigned: ( ), project_status_changed: ( ), step_status_changed: ( ), project_due_soon: ( ), comment_added: ( ), project_comment_added: ( ), user_mentioned: ( ), }; // Default icon for unknown types return icons[type] || ( ); } // Helper function to get notification description based on type function getNotificationDescription(type) { const descriptions = { project_created: 'Ricevi email quando viene creata una nuova pratica', project_assigned: 'Ricevi email quando ti viene assegnata una pratica', project_status_changed: 'Ricevi email quando cambia lo stato di una pratica', step_status_changed: 'Ricevi email quando cambia lo stato di un passaggio', project_due_soon: 'Ricevi email quando una pratica sta per scadere', comment_added: 'Ricevi email quando viene aggiunto un commento ad uno step', project_comment_added: 'Ricevi email quando viene aggiunto un commento alla pratica', user_mentioned: 'Ricevi email quando qualcuno ti menziona (@) in un commento o nota', }; return descriptions[type] || 'Ricevi notifiche per questo tipo di evento'; } // Export to global scope window.EmailPreferencesSection = EmailPreferencesSection;