From d743e0056353a4fca445a7089e3afc1fd4e8c219 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Tue, 29 Oct 2024 07:49:05 +0100 Subject: [PATCH] fix(frontend): fix script and flow renaming --- .../src/lib/components/FlowBuilder.svelte | 64 +++++----- .../src/lib/components/ScriptBuilder.svelte | 112 ++++++++++-------- 2 files changed, 95 insertions(+), 81 deletions(-) diff --git a/frontend/src/lib/components/FlowBuilder.svelte b/frontend/src/lib/components/FlowBuilder.svelte index 6da81b66d3..63e1655a77 100644 --- a/frontend/src/lib/components/FlowBuilder.svelte +++ b/frontend/src/lib/components/FlowBuilder.svelte @@ -119,13 +119,21 @@ if (version === undefined) { return } - const flowVersion = await FlowService.getFlowLatestVersion({ - workspace: $workspaceStore!, - path: $pathStore - }) - - onLatest = version === flowVersion?.id + try { + if (initialPath && initialPath != '') { + const flowVersion = await FlowService.getFlowLatestVersion({ + workspace: $workspaceStore!, + path: initialPath + }) + onLatest = version === flowVersion?.id + } else { + onLatest = true + } + } catch (err) { + console.error('Error comparing versions', err) + onLatest = false + } } const dispatch = createEventDispatcher() @@ -277,9 +285,8 @@ } async function handleSaveFlow(deploymentMsg?: string) { - - await compareVersions(); - if (onLatest) { + await compareVersions() + if (onLatest || initialPath == '') { // Handle directly await saveFlow(deploymentMsg) } else { @@ -294,28 +301,27 @@ open = true } } - async function syncWithDeployed(){ - const flow = await FlowService.getFlowByPath({ - workspace: $workspaceStore!, - path: $pathStore, - withStarredInfo: true - }) - deployedValue = { - ...flow, - starred: undefined, - id: undefined, - edited_at: undefined, - edited_by: undefined, - workspace_id: undefined, - archived: undefined, - same_worker: undefined, - visible_to_runner_only: undefined, - ws_error_handler_muted: undefined, - } - deployedBy = flow.edited_by + async function syncWithDeployed() { + const flow = await FlowService.getFlowByPath({ + workspace: $workspaceStore!, + path: initialPath, + withStarredInfo: true + }) + deployedValue = { + ...flow, + starred: undefined, + id: undefined, + edited_at: undefined, + edited_by: undefined, + workspace_id: undefined, + archived: undefined, + same_worker: undefined, + visible_to_runner_only: undefined, + ws_error_handler_muted: undefined + } + deployedBy = flow.edited_by } - async function saveFlow(deploymentMsg?: string): Promise { loadingSave = true try { diff --git a/frontend/src/lib/components/ScriptBuilder.svelte b/frontend/src/lib/components/ScriptBuilder.svelte index 45a48e66d8..e96fd824ee 100644 --- a/frontend/src/lib/components/ScriptBuilder.svelte +++ b/frontend/src/lib/components/ScriptBuilder.svelte @@ -18,9 +18,7 @@ encodeState, formatCron, orderedJsonStringify, - type Value - } from '$lib/utils' import Path from './Path.svelte' import ScriptEditor from './ScriptEditor.svelte' @@ -92,7 +90,6 @@ let confirmCallback: () => void = () => {} // What happens when user clicks `override` in warning let open: boolean = false // Is confirmation modal open - let metadataOpen = !neverShowMeta && (showMeta || @@ -251,62 +248,74 @@ } } - async function handleEditScript(stay: boolean, deployMsg?: string): Promise{ - // Fetch latest version and fetch entire script after if needed - let actual_parent_hash = (await ScriptService.getScriptLatestVersion({ - workspace: $workspaceStore!, - path: script.path, - }))?.script_hash; + async function handleEditScript(stay: boolean, deployMsg?: string): Promise { + // Fetch latest version and fetch entire script after if needed + let actual_parent_hash: string | undefined = undefined - - // Usually when we create new script, we put current hash as a parent_hash - // But if we specify parent_hash that is already used, than we get error - // In order to fix it we make sure that client's understanding of parent_hash - // is aligns with understanding of backend. - if (script.parent_hash == actual_parent_hash) { - // Handle directly - await editScript(stay, actual_parent_hash, deployMsg); - } else { - - // Fetch entire script, since we need it to show Diff - await syncWithDeployed() - - // Handle through confirmation modal - confirmCallback = async () => { - open = false - await editScript(stay, actual_parent_hash, deployMsg); - } - // Open confirmation modal - open = true + try { + if (initialPath && initialPath != '') { + actual_parent_hash = ( + await ScriptService.getScriptLatestVersion({ + workspace: $workspaceStore!, + path: initialPath + }) + )?.script_hash } + } catch (error) { + // + } + + // Usually when we create new script, we put current hash as a parent_hash + // But if we specify parent_hash that is already used, than we get error + // In order to fix it we make sure that client's understanding of parent_hash + // is aligns with understanding of backend. + if (actual_parent_hash == undefined || script.parent_hash == actual_parent_hash) { + // Handle directly + await editScript(stay, script.parent_hash!, deployMsg) + } else { + // Fetch entire script, since we need it to show Diff + await syncWithDeployed() + + // Handle through confirmation modal + confirmCallback = async () => { + open = false + await editScript(stay, actual_parent_hash, deployMsg) + } + // Open confirmation modal + open = true + } } - async function syncWithDeployed(){ - const latestScript = await ScriptService.getScriptByPath({ - workspace: $workspaceStore!, - path: script.path, - withStarredInfo: true - }); + async function syncWithDeployed() { + const latestScript = await ScriptService.getScriptByPath({ + workspace: $workspaceStore!, + path: initialPath, + withStarredInfo: true + }) - deployedValue = { - ...latestScript, - starred: undefined, - workspace_id: undefined, - archived: undefined, - created_at: undefined, - created_by: undefined, - deleted: undefined, - extra_perms: undefined, - is_template: undefined, - lock: undefined, - lock_error_logs: undefined, - parent_hashes: undefined, - }; + deployedValue = { + ...latestScript, + starred: undefined, + workspace_id: undefined, + archived: undefined, + created_at: undefined, + created_by: undefined, + deleted: undefined, + extra_perms: undefined, + is_template: undefined, + lock: undefined, + lock_error_logs: undefined, + parent_hashes: undefined + } - deployedBy = latestScript.created_by; + deployedBy = latestScript.created_by } - async function editScript(stay: boolean, parentHash: string, deploymentMsg?: string, ): Promise { + async function editScript( + stay: boolean, + parentHash: string, + deploymentMsg?: string + ): Promise { loadingSave = true try { try { @@ -354,7 +363,6 @@ } }) - console.log('initialPath', initialPath) const scheduleExists = initialPath != '' && (await ScheduleService.existsSchedule({