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 { cn } from "@/lib/utils"; import { api } from "@/utils/api"; import { zodResolver } from "@hookform/resolvers/zod"; import { AlertTriangle, PenBoxIcon } from "lucide-react"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; const updateRegistry = z.object({ registryName: z.string().min(1, { message: "Registry name is required", }), username: z.string().min(1, { message: "Username is required", }), password: z.string(), registryUrl: z.string().min(1, { message: "Registry URL is required", }), imagePrefix: z.string(), }); type UpdateRegistry = z.infer; interface Props { registryId: string; } export const UpdateDockerRegistry = ({ registryId }: Props) => { const utils = api.useUtils(); const { mutateAsync: testRegistry, isLoading } = api.registry.testRegistry.useMutation(); const { data, refetch } = api.registry.one.useQuery( { registryId, }, { enabled: !!registryId, }, ); const isCloud = data?.registryType === "cloud"; const { mutateAsync, isError, error } = api.registry.update.useMutation(); const form = useForm({ defaultValues: { imagePrefix: "", registryName: "", username: "", password: "", registryUrl: "", }, resolver: zodResolver(updateRegistry), }); const password = form.watch("password"); const username = form.watch("username"); const registryUrl = form.watch("registryUrl"); const registryName = form.watch("registryName"); const imagePrefix = form.watch("imagePrefix"); useEffect(() => { if (data) { form.reset({ imagePrefix: data.imagePrefix || "", registryName: data.registryName || "", username: data.username || "", password: "", registryUrl: data.registryUrl || "", }); } }, [form, form.reset, data]); const onSubmit = async (data: UpdateRegistry) => { await mutateAsync({ registryId, ...(data.password ? { password: data.password } : {}), registryName: data.registryName, username: data.username, registryUrl: data.registryUrl, imagePrefix: data.imagePrefix, }) .then(async (data) => { toast.success("Registry Updated"); await refetch(); await utils.registry.all.invalidate(); }) .catch(() => { toast.error("Error to update the registry"); }); }; return ( Registry Update the registry information {isError && (
{error?.message}
)}
( Registry Name )} /> ( Username )} /> ( Password )} /> {isCloud && ( ( Image Prefix )} /> )} ( Registry URL )} />
{isCloud && ( )}
); };