import { AlertBlock } from "@/components/shared/alert-block"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Switch } from "@/components/ui/switch"; import { api } from "@/utils/api"; import { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { domain } from "@/server/db/validations"; import { zodResolver } from "@hookform/resolvers/zod"; import type z from "zod"; type Domain = z.infer; interface Props { applicationId: string; domainId?: string; children: React.ReactNode; } export const AddDomain = ({ applicationId, domainId = "", children, }: Props) => { const [isOpen, setIsOpen] = useState(false); const utils = api.useUtils(); const { data, refetch } = api.domain.one.useQuery( { domainId, }, { enabled: !!domainId, }, ); const { mutateAsync, isError, error, isLoading } = domainId ? api.domain.update.useMutation() : api.domain.create.useMutation(); const form = useForm({ resolver: zodResolver(domain), }); useEffect(() => { if (data) { form.reset({ ...data, /* Convert null to undefined */ path: data?.path || undefined, port: data?.port || undefined, }); } if (!domainId) { form.reset({}); } }, [form, form.reset, data, isLoading]); const dictionary = { success: domainId ? "Domain Updated" : "Domain Created", error: domainId ? "Error to update the domain" : "Error to create the domain", submit: domainId ? "Update" : "Create", dialogDescription: domainId ? "In this section you can edit a domain" : "In this section you can add domains", }; const onSubmit = async (data: Domain) => { await mutateAsync({ domainId, applicationId, ...data, }) .then(async () => { toast.success(dictionary.success); await utils.domain.byApplicationId.invalidate({ applicationId, }); await utils.application.readTraefikConfig.invalidate({ applicationId }); if (domainId) { refetch(); } setIsOpen(false); }) .catch(() => { toast.error(dictionary.error); }); }; return ( {children} Domain {dictionary.dialogDescription} {isError && {error?.message}}
( Host )} /> { return ( Path ); }} /> { return ( Container Port { field.onChange(Number.parseInt(e.target.value)); }} /> ); }} /> {form.getValues().https && ( ( Certificate )} /> )} (
HTTPS Automatically provision SSL Certificate.
)} />
); };