From 7773c1bc667a3f2266eafbb161fac1400bfca341 Mon Sep 17 00:00:00 2001 From: Tola Leng Date: Sun, 11 May 2025 20:52:39 +0800 Subject: [PATCH] features: Implement domain validation in SSL form Integrate domain validation logic into the SSL Domain Form, including checks for warnings, expiry, and overall domain validity. --- .../ssl-domain/AddSSLCertificateForm.tsx | 165 ++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 application/src/components/ssl-domain/AddSSLCertificateForm.tsx diff --git a/application/src/components/ssl-domain/AddSSLCertificateForm.tsx b/application/src/components/ssl-domain/AddSSLCertificateForm.tsx new file mode 100644 index 0000000..90a5581 --- /dev/null +++ b/application/src/components/ssl-domain/AddSSLCertificateForm.tsx @@ -0,0 +1,165 @@ + +import React from "react"; +import { useForm } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; +import * as z from "zod"; +import { toast } from "sonner"; + +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"; + +const formSchema = z.object({ + domain: z.string().min(1, "Domain is required"), + port: z.coerce.number().int().min(1).max(65535), + warning_threshold: z.coerce.number().int().min(1).max(365), + expiry_threshold: z.coerce.number().int().min(1).max(30), + notification_channel: z.string().min(1, "Notification channel is required") +}); + +interface AddSSLCertificateFormProps { + onSubmit: (data: AddSSLCertificateDto) => Promise; + onCancel: () => void; + isPending?: boolean; +} + +export const AddSSLCertificateForm = ({ + onSubmit, + onCancel, + isPending = false +}: AddSSLCertificateFormProps) => { + const form = useForm>({ + resolver: zodResolver(formSchema), + defaultValues: { + domain: "", + port: 443, + warning_threshold: 30, + expiry_threshold: 7, + notification_channel: "default" + } + }); + + 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, + port: values.port, + warning_threshold: values.warning_threshold, + expiry_threshold: values.expiry_threshold, + notification_channel: values.notification_channel + }; + + await onSubmit(certData); + form.reset(); + } catch (error) { + console.error("Error adding SSL certificate:", error); + toast.error("Failed to add SSL certificate"); + } + }; + + return ( +
+ + ( + + Domain + + + + + + )} + /> + + ( + + Port + + + + + + )} + /> + + ( + + Warning Threshold (days) + + + + + Get notified when certificates are about to expire + + + + )} + /> + + ( + + Expiry Threshold (days) + + + + + Get notified when certificates are critically close to expiring + + + + )} + /> + + ( + + Notification Channel + + + Choose where to receive SSL certificate alerts + + + + )} + /> + + + + + + + + ); +}; \ No newline at end of file