Some foreach block fixes

This commit is contained in:
Peter Clement 2022-05-04 10:24:28 +01:00
parent 972817e5b9
commit 48804c5414
2 changed files with 28 additions and 15 deletions

View File

@ -53,6 +53,18 @@
x => x.blockToLoop === block.id x => x.blockToLoop === block.id
) )
async function removeLooping() {
loopingSelected = false
let loopBlock =
$automationStore.selectedAutomation?.automation.definition.steps.find(
x => x.blockToLoop === block.id
)
automationStore.actions.deleteAutomationBlock(loopBlock)
await automationStore.actions.save(
$automationStore.selectedAutomation?.automation
)
}
async function deleteStep() { async function deleteStep() {
let loopBlock = let loopBlock =
$automationStore.selectedAutomation?.automation.definition.steps.find( $automationStore.selectedAutomation?.automation.definition.steps.find(
@ -151,9 +163,7 @@
{#if !showLooping} {#if !showLooping}
<div class="blockSection"> <div class="blockSection">
<div class="block-options"> <div class="block-options">
<div class="delete-padding" on:click={() => deleteStep()}> <ActionButton on:click={() => removeLooping()} icon="DeleteOutline" />
<Icon name="DeleteOutline" />
</div>
</div> </div>
<Layout noPadding gap="S"> <Layout noPadding gap="S">
<AutomationBlockSetup <AutomationBlockSetup

View File

@ -107,7 +107,6 @@ class Orchestrator {
let loopSteps = [] let loopSteps = []
for (let step of automation.definition.steps) { for (let step of automation.definition.steps) {
stepCount++ stepCount++
let input
if (step.stepId === LOOP_STEP_ID) { if (step.stepId === LOOP_STEP_ID) {
loopStep = step loopStep = step
loopStepNumber = stepCount loopStepNumber = stepCount
@ -115,17 +114,21 @@ class Orchestrator {
} }
if (loopStep) { if (loopStep) {
input = await processObject(loopStep.inputs, this._context) // lets first of all handle the input
if (
typeof loopStep.inputs.binding === "string" &&
loopStep.inputs.option === "String"
) {
loopStep.inputs.binding = loopStep.inputs.binding.split("\n")
}
} }
let iterations = loopStep ? input.binding.length : 1 let iterations = loopStep ? loopStep.inputs.binding.length : 1
let iterationCount = 0 let iterationCount = 0
for (let index = 0; index < iterations; index++) { for (let index = 0; index < iterations; index++) {
let originalStepInput = cloneDeep(step.inputs) let originalStepInput = cloneDeep(step.inputs)
// Handle if the user has set a max iteration count or if it reaches the max limit set by us // Handle if the user has set a max iteration count or if it reaches the max limit set by us
if (loopStep) { if (loopStep) {
// lets first of all handle the input
// if the input is array then use it, if it is a string then split it on every new line
let newInput = await processObject( let newInput = await processObject(
loopStep.inputs, loopStep.inputs,
cloneDeep(this._context) cloneDeep(this._context)
@ -134,16 +137,13 @@ class Orchestrator {
newInput, newInput,
loopStep.schema.inputs loopStep.schema.inputs
) )
this._context.steps[loopStepNumber] = {
currentItem: newInput.binding[index],
}
let tempOutput = { items: loopSteps, iterations: iterationCount } let tempOutput = { items: loopSteps, iterations: iterationCount }
if ( if (
(loopStep.inputs.option === "Array" && (originalStepInput.option === "Array" &&
!Array.isArray(newInput.binding)) || !Array.isArray(originalStepInput.binding)) ||
(loopStep.inputs.option === "String" && (originalStepInput.option === "String" &&
typeof newInput.binding !== "string") typeof originalStepInput.binding !== "string")
) { ) {
this.updateContextAndOutput(loopStepNumber, step, tempOutput, { this.updateContextAndOutput(loopStepNumber, step, tempOutput, {
status: AutomationErrors.INCORRECT_TYPE, status: AutomationErrors.INCORRECT_TYPE,
@ -153,6 +153,9 @@ class Orchestrator {
loopStep = null loopStep = null
break break
} }
this._context.steps[loopStepNumber] = {
currentItem: newInput.binding[index],
}
// The "Loop" binding in the front end is "fake", so replace it here so the context can understand it // The "Loop" binding in the front end is "fake", so replace it here so the context can understand it
// Pretty hacky because we need to account for the row object // Pretty hacky because we need to account for the row object