From 760934c63a363fb77fea25ea4a8a0b8de6792cc7 Mon Sep 17 00:00:00 2001 From: Nataliia Karmazina Date: Tue, 17 Oct 2023 17:59:36 +0200 Subject: [PATCH 1/2] Fixed a bug in centralized blocks 1. Deleting a block reference works correctly. 2. Some functions and comments have been renamed for better reading and understanding. --- frontend/src/app/Services/block.service.ts | 52 +++++++++++++------ frontend/src/app/delete-toast.ts | 1 - .../scenario-editor.component.ts | 2 +- .../story-editor/story-editor.component.ts | 10 ++-- 4 files changed, 42 insertions(+), 23 deletions(-) diff --git a/frontend/src/app/Services/block.service.ts b/frontend/src/app/Services/block.service.ts index 2985c551b..38effdde4 100644 --- a/frontend/src/app/Services/block.service.ts +++ b/frontend/src/app/Services/block.service.ts @@ -48,7 +48,11 @@ export class BlockService { */ public unpackBlockEvent = new EventEmitter(); - blocks: Block[] = []; + /** + * Stores an array of references blocks, before the reference is deleted + */ + referencesBlocks: Block[] = []; + referenceStories: Story[]; referenceScenarios: Scenario[]; block: Block; @@ -188,11 +192,11 @@ export class BlockService { .filter((story, index, arr) => story && arr.indexOf(story) === index); } /** - * Add/Delete for reference steps in scenario. + * delete a reference and update Block */ - stepAsReference() { - if(this.blocks.length !== 0){ - for (const block of this.blocks) { + deleteUpdateReferenceForBlock() { + if(this.referencesBlocks.length !== 0){ + for (const block of this.referencesBlocks) { if (block.usedAsReference === false) { delete block.usedAsReference; this.updateBlock(block) @@ -201,18 +205,18 @@ export class BlockService { }); } } - this.blocks = []; + this.referencesBlocks = []; } else return 0; } /** - * Remove the reference property for the steps + * Сhecking whether the block is used as a reference * @param blockReferenceId * @param blocks * @param stories */ - removeReferenceForStep(blocks:Block[], stories: Story[], blockReferenceId){ + checkBlockOnReference(blocks:Block[], stories: Story[], blockReferenceId){ this.searchReferences(stories); let blockNoLongerRef = true; for (const scen of this.referenceScenarios){ @@ -228,28 +232,43 @@ export class BlockService { for (const block of blocks){ if(block._id === blockReferenceId){ block.usedAsReference = false; - this.blocks.push(block); + this.referencesBlocks.push(block); } } } } /** - * Check reference and delete in all repository + * Check reference and unpack block in all relevant stories * @param block * @param stories */ - deteleBlockReference(block, stories: Story[]) { + deleteBlockReference(block, stories: Story[]) { this.searchReferences(stories); + let scenariosToUpdate = []; this.referenceScenarios.forEach((scenario) => { - this.unpackScenarioWithBlock(block, scenario); - }); - this.referenceStories.forEach((story) => { - this.storyService.updateStory(story).subscribe(_resp => {}); + for(const s in scenario.stepDefinitions){ + scenario.stepDefinitions[s].forEach((element) => { + if(element._blockReferenceId == block._id){ + this.unpackScenarioWithBlock(block, scenario); + scenariosToUpdate.push(scenario); + } + }) + } }); + //update relevant stories after unpacking + if(scenariosToUpdate.length > 0){ + this.referenceStories.forEach((story) => { + scenariosToUpdate.forEach((scenario) => { + if (story.scenarios.includes(scenario)) { + this.storyService.updateStory(story).subscribe(_resp => {}); + } + }); + }); + } } /** - * Unpack references. Wenn delete block unpack all reference in repository + * Unpack steps from block. Wenn delete block unpack all reference in repository * @param block * @param scenario */ @@ -261,6 +280,7 @@ export class BlockService { step.checked = false; scenario.stepDefinitions[s].push(JSON.parse(JSON.stringify(step))); }); + //remove the block reference among the steps const index = scenario.stepDefinitions[s].findIndex((element) => element._blockReferenceId == block._id); if (index > -1) { scenario.stepDefinitions[s].splice(index, 1); diff --git a/frontend/src/app/delete-toast.ts b/frontend/src/app/delete-toast.ts index 8e7b1ba40..36a0791cd 100644 --- a/frontend/src/app/delete-toast.ts +++ b/frontend/src/app/delete-toast.ts @@ -146,7 +146,6 @@ import { StoryService } from './Services/story.service'; */ deleteToast(event: Event){ event.stopPropagation(); - this.nameComponent = this.apiService.getNameOfComponent(); switch(this.nameComponent){ case 'scenario': this.scenarioService.deleteScenarioEmitter(); break; diff --git a/frontend/src/app/scenario-editor/scenario-editor.component.ts b/frontend/src/app/scenario-editor/scenario-editor.component.ts index be6f3e855..bf2fa27cb 100644 --- a/frontend/src/app/scenario-editor/scenario-editor.component.ts +++ b/frontend/src/app/scenario-editor/scenario-editor.component.ts @@ -267,7 +267,7 @@ export class ScenarioEditorComponent implements OnInit{ } //If the reference was deleted if(stepsReferences.length == 0){ - this.blockService.stepAsReference(); + this.blockService.deleteUpdateReferenceForBlock(); } return this.blocks; diff --git a/frontend/src/app/story-editor/story-editor.component.ts b/frontend/src/app/story-editor/story-editor.component.ts index fe0fcd031..83342d974 100644 --- a/frontend/src/app/story-editor/story-editor.component.ts +++ b/frontend/src/app/story-editor/story-editor.component.ts @@ -436,20 +436,20 @@ export class StoryEditorComponent implements OnInit, OnDestroy{ const id = localStorage.getItem('id'); this.blockService.getBlocks(id).subscribe((resp) => { this.blocks = resp; - this.blockService.removeReferenceForStep(this.blocks, this.stories, blockReferenceId) + this.blockService.checkBlockOnReference(this.blocks, this.stories, blockReferenceId) }); }); - //Event when the entire reference block is deleted. Unpacking steps in all stories + //Event when the entire reference block is deleted. Unpacking steps in all relevant stories this.deleteReferenceObservable = this.blockService.deleteReferenceEvent.subscribe(block => { - this.blockService.deteleBlockReference(block, this.stories); + this.blockService.deleteBlockReference(block, this.stories); }); - //Event when unpacking steps + //Event when unpacking block this.unpackBlockObservable = this.blockService.unpackBlockEvent.subscribe((block) => { this.blockService.unpackScenarioWithBlock(block, this.selectedScenario); const id = localStorage.getItem('id'); this.blockService.getBlocks(id).subscribe((resp) => { this.blocks = resp; - this.blockService.removeReferenceForStep(this.blocks, this.stories, block._id) + this.blockService.checkBlockOnReference(this.blocks, this.stories, block._id) }); this.selectedScenario.saved = false; }); From 88576ae3ea1312b43399e4e47009477bf9bb9027 Mon Sep 17 00:00:00 2001 From: Nataliia Karmazina Date: Thu, 19 Oct 2023 11:46:17 +0200 Subject: [PATCH 2/2] code smells delete --- frontend/src/app/Services/block.service.ts | 46 +++++++++++++++------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/frontend/src/app/Services/block.service.ts b/frontend/src/app/Services/block.service.ts index 38effdde4..e404382e8 100644 --- a/frontend/src/app/Services/block.service.ts +++ b/frontend/src/app/Services/block.service.ts @@ -210,30 +210,46 @@ export class BlockService { else return 0; } - /** - * Сhecking whether the block is used as a reference - * @param blockReferenceId - * @param blocks - * @param stories - */ + /** + * Сhecking whether the block is used as a reference + * @param blockReferenceId + * @param blocks + * @param stories + */ checkBlockOnReference(blocks:Block[], stories: Story[], blockReferenceId){ + if (!this.isBlockReferencedInScenarios(blockReferenceId,stories)) { + this.updateBlockReferenceStatus(blockReferenceId, blocks); + } + } + + /** + * check if a block is referenced in any scenario step definitions + * @param blockReferenceId + * @param stories + */ + isBlockReferencedInScenarios(blockReferenceId: string, stories): boolean { this.searchReferences(stories); - let blockNoLongerRef = true; - for (const scen of this.referenceScenarios){ + for (const scen of this.referenceScenarios) { for (const prop in scen.stepDefinitions) { for (let i = scen.stepDefinitions[prop].length - 1; i >= 0; i--) { if (scen.stepDefinitions[prop][i]._blockReferenceId == blockReferenceId) { - blockNoLongerRef = false; + return true; } } } } - if (blockNoLongerRef){ - for (const block of blocks){ - if(block._id === blockReferenceId){ - block.usedAsReference = false; - this.referencesBlocks.push(block); - } + return false; + } + /** + * update block reference status + * @param blockReferenceId + * @param blocks + */ + updateBlockReferenceStatus(blockReferenceId: string, blocks): void { + for (const block of blocks) { + if (block._id === blockReferenceId) { + block.usedAsReference = false; + this.referencesBlocks.push(block); } } }