From 58f4d3561ee7c49f8e36b50cbdf8e85f237beb2c Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sun, 9 Mar 2025 18:29:20 -0600 Subject: [PATCH] feat(compose): enhance template import with improved error handling and user experience - Refactor import process to use dedicated `import` mutation - Add warning alert about configuration replacement - Implement form reset on successful import - Improve error handling and user feedback - Remove unnecessary console logs and update UI text --- .../advanced/import/show-import.tsx | 306 ++++++++++-------- apps/dokploy/server/api/routers/compose.ts | 122 ++++++- 2 files changed, 288 insertions(+), 140 deletions(-) diff --git a/apps/dokploy/components/dashboard/application/advanced/import/show-import.tsx b/apps/dokploy/components/dashboard/application/advanced/import/show-import.tsx index 47606595..2a3f2f43 100644 --- a/apps/dokploy/components/dashboard/application/advanced/import/show-import.tsx +++ b/apps/dokploy/components/dashboard/application/advanced/import/show-import.tsx @@ -28,10 +28,11 @@ import { Textarea } from "@/components/ui/textarea"; import { api } from "@/utils/api"; import { zodResolver } from "@hookform/resolvers/zod"; import { Code2, Globe2, HardDrive } from "lucide-react"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; +import { AlertBlock } from "@/components/shared/alert-block"; const ImportSchema = z.object({ base64: z.string(), @@ -68,10 +69,13 @@ export const ShowImport = ({ composeId }: Props) => { } | null>(null); const utils = api.useUtils(); - const { mutateAsync: processTemplate } = + const { mutateAsync: processTemplate, isLoading: isLoadingTemplate } = api.compose.processTemplate.useMutation(); - const { mutateAsync: updateCompose, isLoading } = - api.compose.update.useMutation(); + const { + mutateAsync: importTemplate, + isLoading: isImporting, + isSuccess: isImportSuccess, + } = api.compose.import.useMutation(); const form = useForm({ defaultValues: { @@ -80,21 +84,32 @@ export const ShowImport = ({ composeId }: Props) => { resolver: zodResolver(ImportSchema), }); + useEffect(() => { + form.reset({ + base64: "", + }); + }, [isImportSuccess]); + const onSubmit = async () => { - await updateCompose({ - composeId, - composeFile: templateInfo?.compose || "", - }) - .then(async () => { - toast.success("Import configuration updated"); - await utils.compose.one.invalidate({ - composeId, - }); - setShowModal(false); - }) - .catch(() => { - toast.error("Error updating the import configuration"); + const base64 = form.getValues("base64"); + if (!base64) { + toast.error("Please enter a base64 template"); + return; + } + + try { + await importTemplate({ + composeId, + base64, }); + toast.success("Template imported successfully"); + await utils.compose.one.invalidate({ + composeId, + }); + setShowModal(false); + } catch (_error) { + toast.error("Error importing template"); + } }; const handleLoadTemplate = async () => { @@ -129,11 +144,13 @@ export const ShowImport = ({ composeId }: Props) => { Import - - Import your Docker Compose configuration - + Import your Template configuration + + Warning: Importing a template will remove all existing environment + variables, mounts, and domains from this service. +
{ name="base64" render={({ field }) => ( - Docker Compose (Base64) + Configuration (Base64)