import React, { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import * as z from "zod"; import { toast } from "sonner"; import { Bell } from "lucide-react"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { DialogFooter } from "@/components/ui/dialog"; import { AddSSLCertificateDto } from "@/types/ssl.types"; import { alertConfigService, AlertConfiguration } from "@/services/alertConfigService"; import { useLanguage } from "@/contexts/LanguageContext"; const formSchema = z.object({ domain: z.string().min(1, "Domain is required"), warning_threshold: z.coerce.number().int().min(1).max(365), expiry_threshold: z.coerce.number().int().min(1).max(30), notification_channel: z.string().optional(), // Make it optional to allow empty string for "None" check_interval: z.coerce.number().int().min(1).max(30).optional() }); interface AddSSLCertificateFormProps { onSubmit: (data: AddSSLCertificateDto) => Promise; onCancel: () => void; isPending?: boolean; } export const AddSSLCertificateForm = ({ onSubmit, onCancel, isPending = false }: AddSSLCertificateFormProps) => { const { t } = useLanguage(); const [alertConfigs, setAlertConfigs] = useState([]); const [isLoading, setIsLoading] = useState(false); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { domain: "", warning_threshold: 30, expiry_threshold: 7, notification_channel: "none", check_interval: 1 } }); // Fetch notification channels when form loads useEffect(() => { const fetchNotificationChannels = async () => { setIsLoading(true); try { const configs = await alertConfigService.getAlertConfigurations(); console.log("Fetched notification channels:", configs); // Only include enabled channels const enabledConfigs = configs.filter(config => { // Handle the possibility of enabled being a string if (typeof config.enabled === 'string') { return config.enabled === "true"; } // Otherwise treat as boolean return config.enabled === true; }); setAlertConfigs(enabledConfigs); } catch (error) { console.error("Error fetching notification channels:", error); toast.error(t('failedToLoadCertificates')); } finally { setIsLoading(false); } }; fetchNotificationChannels(); }, [form, t]); const handleSubmit = async (values: z.infer) => { try { // Convert the form values to the required DTO format with required properties const certData: AddSSLCertificateDto = { domain: values.domain, warning_threshold: values.warning_threshold, expiry_threshold: values.expiry_threshold, notification_channel: values.notification_channel === "none" ? "" : (values.notification_channel || ""), // Convert "none" to empty string check_interval: values.check_interval }; await onSubmit(certData); form.reset(); } catch (error) { console.error("Error adding SSL certificate:", error); toast.error(t('failedToAddCertificate')); } }; return (
( {t('domain')} )} />
( {t('warningThreshold')} {t('getNotifiedExpiration')} )} /> ( {t('expiryThreshold')} {t('getNotifiedCritical')} )} />
( Check Interval (Days) How often to check the SSL certificate (in days) )} /> ( {t('notificationChannel')} {t('whereToSend')} )} /> ); };