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, 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 { api } from "@/utils/api"; import { zodResolver } from "@hookform/resolvers/zod"; import { PlusIcon } from "lucide-react"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; const AddPortSchema = z.object({ publishedPort: z.number().int().min(1).max(65535), targetPort: z.number().int().min(1).max(65535), protocol: z.enum(["tcp", "udp"], { required_error: "Protocol is required", }), }); type AddPort = z.infer; interface Props { applicationId: string; children?: React.ReactNode; } export const AddPort = ({ applicationId, children = , }: Props) => { const utils = api.useUtils(); const { mutateAsync, isLoading, error, isError } = api.port.create.useMutation(); const form = useForm({ defaultValues: { publishedPort: 0, targetPort: 0, }, resolver: zodResolver(AddPortSchema), }); useEffect(() => { form.reset({ publishedPort: 0, targetPort: 0, }); }, [form, form.reset, form.formState.isSubmitSuccessful]); const onSubmit = async (data: AddPort) => { await mutateAsync({ applicationId, ...data, }) .then(async () => { toast.success("Port Created"); await utils.application.one.invalidate({ applicationId, }); }) .catch(() => { toast.error("Error to create the port"); }); }; return ( Ports Ports are used to expose your application to the internet. {isError && {error?.message}}
( Published Port { const value = e.target.value; if (value === "") { field.onChange(0); } else { const number = Number.parseInt(value, 10); if (!Number.isNaN(number)) { field.onChange(number); } } }} /> )} /> ( Target Port { const value = e.target.value; if (value === "") { field.onChange(0); } else { const number = Number.parseInt(value, 10); if (!Number.isNaN(number)) { field.onChange(number); } } }} /> )} /> { return ( Protocol ); }} />
); };