From 068147251c831d3ab8564ccb909ad72ef2e32e74 Mon Sep 17 00:00:00 2001 From: Faton Ramadani Date: Wed, 17 Aug 2022 16:53:41 +0200 Subject: [PATCH] fix(frontend): Fix loops pickable properties (#441) --- frontend/src/lib/components/ModuleStep.svelte | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/frontend/src/lib/components/ModuleStep.svelte b/frontend/src/lib/components/ModuleStep.svelte index bd9a6d5f60..189070d323 100644 --- a/frontend/src/lib/components/ModuleStep.svelte +++ b/frontend/src/lib/components/ModuleStep.svelte @@ -23,6 +23,7 @@ import { flowStateStore, type FlowModuleSchema, type FlowState } from './flows/flowState' import { stepOpened } from './flows/stepOpenedStore' import { buildExtraLib, objectToTsType, schemaToObject, schemaToTsType } from '$lib/utils' + import { rotateRight } from 'svelte-awesome/icons' export let indexes: number[] export let mod: FlowModule @@ -37,29 +38,50 @@ let pickableProperties: Object | undefined = undefined let bigEditor = false + const i = indexes[0] + type PickableProperties = { flow_input?: Object previous_result?: Object step?: Object[] } - const i = indexes[0] + function hasElements(arr: any) { + return Array.isArray(arr) && arr.length > 0 + } - function getPickableProperties(flow: Flow): PickableProperties { + function getLast(arr: T[]): T { + return arr[arr.length - 1] + } + + function getPickableProperties(flow: Flow, flowState: FlowState): PickableProperties { const flowInputAsObject = schemaToObject(flow.schema, args) if (indexes.length > 1) { - const prev = $flowStateStore[i - 1].previewResults - const prevValues = prev[prev.length - 1] + const [parentIndex] = indexes + + const stepBeforeLoop = flowState[parentIndex - 1] + flowInputAsObject['iter'] = { - value: prevValues?.[prevValues.length - 1] ?? "iteration's value", - index: `iteration's index (0 to ${prevValues ? prevValues.length - 1 : '..'})` + value: "iteration's value", + index: "iteration's index" + } + + if (hasElements(stepBeforeLoop.previewResults)) { + const lastResults = getLast(stepBeforeLoop.previewResults) + + if (hasElements(lastResults)) { + flowInputAsObject['iter'] = { + value: getLast(lastResults), + index: `iteration's index (0 to ${lastResults.length - 1})` + } + } } } - const hasResults = previousStepPreviewResults.length > 0 + const hasResults = hasElements(previousStepPreviewResults) + const last = hasResults ? getLast(previousStepPreviewResults) : {} - const last = hasResults ? previousStepPreviewResults[previousStepPreviewResults.length - 1] : {} return { flow_input: flowInputAsObject, previous_result: last, @@ -68,7 +90,7 @@ } $: shouldPick = 'path' in mod.value && mod.value.path === '' && !('language' in mod.value) - $: pickableProperties = getPickableProperties($flowStore) + $: pickableProperties = getPickableProperties($flowStore, $flowStateStore) $: extraLib = buildExtraLib( schemaToTsType($flowStore?.schema), i === 0 ? schemaToTsType($flowStore?.schema) : objectToTsType(previousStepPreviewResults) @@ -140,8 +162,8 @@ {#if opened} {#if shouldPick} 1 || i == 0} + shouldDisableTriggerScripts={indexes[0] > 0} + shouldDisableLoopCreation={indexes.length > 1 || indexes[0] == 0} on:pick={(e) => apply(pickScript, e.detail.path)} on:new={(e) => apply(createInlineScriptModule, {