import React, { useEffect, useState } from 'react'; import { useFormContext } from 'react-hook-form'; import { useLanguage } from '@/contexts/LanguageContext'; import { alertConfigService, AlertConfiguration } from '@/services/alertConfigService'; import { MaintenanceFormValues } from '../hooks/useMaintenanceForm'; import { FormField, FormItem, FormLabel, FormControl, FormDescription } from '@/components/ui/form'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Switch } from '@/components/ui/switch'; import { useToast } from '@/hooks/use-toast'; import { Skeleton } from '@/components/ui/skeleton'; import { Bell, BellOff } from 'lucide-react'; export const MaintenanceNotificationSettingsField = () => { const { t } = useLanguage(); const { toast } = useToast(); const [notificationChannels, setNotificationChannels] = useState([]); const [isLoading, setIsLoading] = useState(true); const { control, watch, setValue, getValues } = useFormContext(); // Watch the notification toggle state const notifySubscribers = watch('notify_subscribers'); const notificationChannelId = watch('notification_channel_id'); useEffect(() => { const fetchNotificationChannels = async () => { try { setIsLoading(true); const channels = await alertConfigService.getAlertConfigurations(); // console.log("Fetched notification channels for form:", channels); // Only show enabled channels const enabledChannels = channels.filter(channel => channel.enabled); setNotificationChannels(enabledChannels); // If we have notification channels and notification is enabled but no channel is selected, // select the first one by default const currentChannel = getValues('notification_channel_id'); const shouldNotify = getValues('notify_subscribers'); // console.log("Current notification values:", { // currentChannel, // shouldNotify, // availableChannels: enabledChannels.length // }); if (shouldNotify && (!currentChannel || currentChannel === 'none') && enabledChannels.length > 0) { // console.log("Setting default notification channel:", enabledChannels[0].id); setValue('notification_channel_id', enabledChannels[0].id); } } catch (error) { // console.error('Error fetching notification channels:', error); toast({ title: t('error'), description: t('errorFetchingNotificationChannels'), variant: 'destructive', }); } finally { setIsLoading(false); } }; fetchNotificationChannels(); }, [t, toast, setValue, getValues]); // Log value changes for debugging // useEffect(() => { // console.log("Current notification settings:", { // channel_id: getValues('notification_channel_id'), // notify: notifySubscribers // }); // }, [notifySubscribers, notificationChannelId, getValues]); return (

{t('notificationSettings')}

(
{field.value ? : } {field.value ? t('notifySubscribers') : t('mutedNotifications')}
{field.value ? t('notifySubscribersDesc') : t('notificationsAreMuted')}
{ field.onChange(checked); // console.log("Notification toggle changed to:", checked); // If notifications are disabled, also clear the notification channel if (!checked) { setValue('notification_channel_id', ''); } else if (notificationChannels.length > 0) { // If enabled and channels available, select the first one by default setValue('notification_channel_id', notificationChannels[0].id); } }} />
)} /> { // Make sure to handle both empty string and "none" as special cases const displayValue = field.value || ""; // console.log("Rendering notification channel field with value:", { // fieldValue: field.value, // displayValue // }); return ( {t('notificationChannel')} {isLoading ? ( ) : ( )} {notifySubscribers ? t('selectChannelForNotifications') : t('enableNotificationsFirst')} ); }} />
); };