Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bug in centralized blocks #485

Merged
merged 2 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 65 additions & 29 deletions frontend/src/app/Services/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -201,55 +205,86 @@ export class BlockService {
});
}
}
this.blocks = [];
this.referencesBlocks = [];
}
else return 0;
}

/**
* Remove the reference property for the steps
* @param blockReferenceId
* @param blocks
* @param stories
*/
removeReferenceForStep(blocks:Block[], stories: Story[], blockReferenceId){
/**
* С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.blocks.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);
}
}
}
/**
* 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
*/
Expand All @@ -261,6 +296,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);
Expand Down
1 change: 0 additions & 1 deletion frontend/src/app/delete-toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 5 additions & 5 deletions frontend/src/app/story-editor/story-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
Loading