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 { PenBoxIcon, Pencil } from "lucide-react"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; const UpdatePortSchema = 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", invalid_type_error: "Protocol must be a valid protocol", }), }); type UpdatePort = z.infer; interface Props { portId: string; } export const UpdatePort = ({ portId }: Props) => { const utils = api.useUtils(); const { data } = api.port.one.useQuery( { portId, }, { enabled: !!portId, }, ); const { mutateAsync, isLoading, error, isError } = api.port.update.useMutation(); const form = useForm({ defaultValues: {}, resolver: zodResolver(UpdatePortSchema), }); useEffect(() => { if (data) { form.reset({ publishedPort: data.publishedPort, targetPort: data.targetPort, protocol: data.protocol, }); } }, [form, form.reset, data]); const onSubmit = async (data: UpdatePort) => { await mutateAsync({ portId, publishedPort: data.publishedPort, targetPort: data.targetPort, protocol: data.protocol, }) .then(async (response) => { toast.success("Port Updated"); await utils.application.one.invalidate({ applicationId: response?.applicationId, }); }) .catch(() => { toast.error("Error to update the port"); }); }; return ( Update Update the port {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 ); }} />
); };