import { FormControl, FormField, FormItem, FormLabel, FormDescription } from "@/components/ui/form"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Switch } from "@/components/ui/switch"; import { Badge } from "@/components/ui/badge"; import { X } from "lucide-react"; import { UseFormReturn } from "react-hook-form"; import { ServiceFormData } from "./types"; import { useQuery } from "@tanstack/react-query"; import { alertConfigService, AlertConfiguration } from "@/services/alertConfigService"; import { serviceNotificationTemplateService, ServiceNotificationTemplate } from "@/services/serviceNotificationTemplateService"; import { useState, useEffect } from "react"; interface ServiceNotificationFieldsProps { form: UseFormReturn; } export function ServiceNotificationFields({ form }: ServiceNotificationFieldsProps) { const [alertConfigs, setAlertConfigs] = useState([]); // Get the current form values for debugging const notificationStatus = form.watch("notificationStatus"); const notificationChannels = form.watch("notificationChannels") || []; const alertTemplate = form.watch("alertTemplate"); // Fetch alert configurations for notification channels const { data: alertConfigsData } = useQuery({ queryKey: ['alertConfigs'], queryFn: () => alertConfigService.getAlertConfigurations(), }); // Fetch service notification templates const { data: serviceTemplates, isLoading: isLoadingTemplates } = useQuery({ queryKey: ['serviceNotificationTemplates'], queryFn: () => serviceNotificationTemplateService.getTemplates(), }); // Update alert configs when data is loaded useEffect(() => { if (alertConfigsData) { // Only show enabled channels const enabledChannels = alertConfigsData.filter(config => config.enabled); setAlertConfigs(enabledChannels); // Debug log to check what alert configs are loaded } }, [alertConfigsData]); // Debug log for service templates useEffect(() => { if (serviceTemplates) { // console.log("Loaded service notification templates:", serviceTemplates); } }, [serviceTemplates]); // Log when form values change to debug useEffect(() => { // console.log("Notification values changed:", { // notificationStatus: form.getValues("notificationStatus"), // notificationChannels: form.getValues("notificationChannels") // }); }, [form.watch("notificationStatus"), form.watch("notificationChannels")]); const handleChannelAdd = (channelId: string) => { const currentChannels = form.getValues("notificationChannels") || []; if (!currentChannels.includes(channelId)) { form.setValue("notificationChannels", [...currentChannels, channelId]); } }; const handleChannelRemove = (channelId: string) => { const currentChannels = form.getValues("notificationChannels") || []; form.setValue("notificationChannels", currentChannels.filter(id => id !== channelId)); }; const getSelectedChannelNames = () => { return (notificationChannels || []).map(channelId => { const config = alertConfigs.find(c => c.id === channelId); return config ? `${config.notify_name} (${config.notification_type})` : channelId; }); }; return ( <> (
Enable Notifications Enable or disable notifications for this service
{ field.onChange(checked ? "enabled" : "disabled"); // Clear notification channels when disabled if (!checked) { form.setValue("notificationChannels", []); } }} />
)} /> ( Notification Channels {notificationStatus === "enabled" ? "Select notification channels for this service" : "Enable notifications first to select channels"} {/* Display selected channels as badges */} {notificationChannels && notificationChannels.length > 0 && (
{getSelectedChannelNames().map((channelName, index) => ( {channelName} handleChannelRemove(notificationChannels[index])} /> ))}
)}
)} /> { // console.log("Rendering alert template field with value:", field.value); return ( Alert Template {notificationStatus === "enabled" ? "Choose a template for alert messages" : "Enable notifications first to select template"} ); }} /> ); }