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 { api } from "@/utils/api"; import { AlertBlock } from "@/components/shared/alert-block"; import { zodResolver } from "@hookform/resolvers/zod"; import { Pencil } from "lucide-react"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; import { Textarea } from "@/components/ui/textarea"; const mountSchema = z.object({ mountPath: z.string().min(1, "Mount path required"), }); const mySchema = z.discriminatedUnion("type", [ z .object({ type: z.literal("bind"), hostPath: z.string().min(1, "Host path required"), }) .merge(mountSchema), z .object({ type: z.literal("volume"), volumeName: z.string().min(1, "Volume name required"), }) .merge(mountSchema), z .object({ type: z.literal("file"), content: z.string().optional(), }) .merge(mountSchema), ]); type UpdateMount = z.infer; interface Props { mountId: string; type: "bind" | "volume" | "file"; refetch: () => void; } export const UpdateVolume = ({ mountId, type, refetch }: Props) => { const utils = api.useUtils(); const { data } = api.mounts.one.useQuery( { mountId, }, { enabled: !!mountId, }, ); const { mutateAsync, isLoading, error, isError } = api.mounts.update.useMutation(); const form = useForm({ defaultValues: { type, hostPath: "", mountPath: "", }, resolver: zodResolver(mySchema), }); const typeForm = form.watch("type"); useEffect(() => { if (data) { if (typeForm === "bind") { form.reset({ hostPath: data.hostPath || "", mountPath: data.mountPath, type: "bind", }); } else if (typeForm === "volume") { form.reset({ volumeName: data.volumeName || "", mountPath: data.mountPath, type: "volume", }); } else if (typeForm === "file") { form.reset({ content: data.content || "", mountPath: data.mountPath, type: "file", }); } } }, [form, form.reset, data]); const onSubmit = async (data: UpdateMount) => { if (data.type === "bind") { await mutateAsync({ hostPath: data.hostPath, mountPath: data.mountPath, type: data.type, mountId, }) .then(() => { toast.success("Mount Update"); }) .catch(() => { toast.error("Error to update the Bind mount"); }); } else if (data.type === "volume") { await mutateAsync({ volumeName: data.volumeName, mountPath: data.mountPath, type: data.type, mountId, }) .then(() => { toast.success("Mount Update"); }) .catch(() => { toast.error("Error to update the Volume mount"); }); } else if (data.type === "file") { await mutateAsync({ content: data.content, mountPath: data.mountPath, type: data.type, mountId, }) .then(() => { toast.success("Mount Update"); }) .catch(() => { toast.error("Error to update the File mount"); }); } refetch(); }; return ( Update Update the mount {isError && {error?.message}}
{type === "bind" && ( ( Host Path )} /> )} {type === "volume" && ( ( Volume Name )} /> )} {type === "file" && ( ( Content