From 52c286306d270b6a6b9714de4ce53f0cff462ac4 Mon Sep 17 00:00:00 2001 From: EiffelFly <57251712+EiffelFly@users.noreply.github.com> Date: Wed, 11 Oct 2023 23:57:50 +0800 Subject: [PATCH] fix: fix rename pipeline error --- packages/toolkit/package.json | 2 +- .../src/view/pipeline-builder/FlowControl.tsx | 16 ++--- .../components/PipelineNameForm.tsx | 58 ++++++++++++++++++- 3 files changed, 64 insertions(+), 12 deletions(-) diff --git a/packages/toolkit/package.json b/packages/toolkit/package.json index bd2c339f..37c88a0d 100644 --- a/packages/toolkit/package.json +++ b/packages/toolkit/package.json @@ -1,6 +1,6 @@ { "name": "@instill-ai/toolkit", - "version": "0.68.3-rc.20", + "version": "0.68.3-rc.22", "description": "Instill AI's frontend toolkit", "repository": "https://github.com/instill-ai/design-system.git", "bugs": "https://github.com/instill-ai/design-system/issues", diff --git a/packages/toolkit/src/view/pipeline-builder/FlowControl.tsx b/packages/toolkit/src/view/pipeline-builder/FlowControl.tsx index 45bdf0a3..7db4937f 100644 --- a/packages/toolkit/src/view/pipeline-builder/FlowControl.tsx +++ b/packages/toolkit/src/view/pipeline-builder/FlowControl.tsx @@ -126,7 +126,7 @@ export const FlowControl = (props: FlowControlProps) => { setIsSaving(true); - if (!pipelineIsNew) { + if (!pipelineIsNew && pipelineRecipeIsDirty) { const payload: UpdateUserPipelinePayload = { name: `users/${entity}/pipelines/${pipelineId}`, description: pipelineDescription ?? undefined, @@ -149,15 +149,11 @@ export const FlowControl = (props: FlowControlProps) => { const { nodes, edges } = createInitialGraphData(res.pipeline.recipe); - createGraphLayout(nodes, edges) - .then((graphData) => { - updateNodes(() => graphData.nodes); - updateEdges(() => graphData.edges); - updateSelectResourceDialogIsOpen(() => false); - }) - .catch((err) => { - console.log(err); - }); + const graph = await createGraphLayout(nodes, edges); + + updateNodes(() => graph.nodes); + updateEdges(() => graph.edges); + updateSelectResourceDialogIsOpen(() => false); } catch (error) { if (isAxiosError(error)) { toast({ diff --git a/packages/toolkit/src/view/pipeline-builder/components/PipelineNameForm.tsx b/packages/toolkit/src/view/pipeline-builder/components/PipelineNameForm.tsx index e37f362c..3c64cabc 100644 --- a/packages/toolkit/src/view/pipeline-builder/components/PipelineNameForm.tsx +++ b/packages/toolkit/src/view/pipeline-builder/components/PipelineNameForm.tsx @@ -17,12 +17,17 @@ import { CreateUserPipelinePayload, Nullable, RenameUserPipelinePayload, + UpdateUserPipelinePayload, getInstillApiErrorMessage, useCreateUserPipeline, useRenameUserPipeline, useUpdateUserPipeline, } from "../../../lib"; -import { constructPipelineRecipe } from "../lib"; +import { + constructPipelineRecipe, + createGraphLayout, + createInitialGraphData, +} from "../lib"; import { AutoresizeInputWrapper } from "../../../components"; const pipelineBuilderSelector = (state: PipelineBuilderStore) => ({ @@ -31,11 +36,14 @@ const pipelineBuilderSelector = (state: PipelineBuilderStore) => ({ setPipelineUid: state.setPipelineUid, setPipelineName: state.setPipelineName, nodes: state.nodes, + updateNodes: state.updateNodes, + updateEdges: state.updateEdges, pipelineIsNew: state.pipelineIsNew, testModeEnabled: state.testModeEnabled, updatePipelineIsNew: state.updatePipelineIsNew, pipelineRecipeIsDirty: state.pipelineRecipeIsDirty, updatePipelineRecipeIsDirty: state.updatePipelineRecipeIsDirty, + updateSelectResourceDialogIsOpen: state.updateSelectResourceDialogIsOpen, }); export type PipelineNameFormProps = { @@ -73,9 +81,12 @@ export const PipelineNameForm = (props: PipelineNameFormProps) => { pipelineIsNew, testModeEnabled, nodes, + updateNodes, + updateEdges, updatePipelineIsNew, pipelineRecipeIsDirty, updatePipelineRecipeIsDirty, + updateSelectResourceDialogIsOpen, } = usePipelineBuilderStore(pipelineBuilderSelector, shallow); React.useEffect(() => { @@ -92,6 +103,7 @@ export const PipelineNameForm = (props: PipelineNameFormProps) => { }, [pipelineId, form]); const createUserPipeline = useCreateUserPipeline(); + const updateUserPipeline = useUpdateUserPipeline(); const renameUserPipeline = useRenameUserPipeline(); async function handleRenamePipeline(newId: string) { @@ -99,6 +111,8 @@ export const PipelineNameForm = (props: PipelineNameFormProps) => { return; } + // If pipeline is new, we dircetly create the pipeline + if (pipelineIsNew) { const payload: CreateUserPipelinePayload = { id: newId, @@ -149,6 +163,48 @@ export const PipelineNameForm = (props: PipelineNameFormProps) => { return; } + // If the pipeline recipe is dirty, we should update the pipeline recipe + // first then rename the pipeline + + if (pipelineRecipeIsDirty) { + const payload: UpdateUserPipelinePayload = { + name: `users/${entity}/pipelines/${pipelineId}`, + recipe: constructPipelineRecipe(nodes), + }; + + try { + const res = await updateUserPipeline.mutateAsync({ + payload, + accessToken, + }); + + updatePipelineRecipeIsDirty(() => false); + + const { nodes, edges } = createInitialGraphData(res.pipeline.recipe); + + const graph = await createGraphLayout(nodes, edges); + + updateNodes(() => graph.nodes); + updateEdges(() => graph.edges); + } catch (error) { + if (isAxiosError(error)) { + toast({ + title: "Something went wrong when save the pipeline", + description: getInstillApiErrorMessage(error), + variant: "alert-error", + size: "large", + }); + } else { + toast({ + title: "Something went wrong when save the pipeline", + variant: "alert-error", + size: "large", + }); + } + return; + } + } + const payload: RenameUserPipelinePayload = { name: `users/${entity}/pipelines/${pipelineId}`, new_pipeline_id: newId,