From c19854290450e35edcf33c46e74132048d1f264b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Gerrit=20G=C3=B6bel?= <86782124+jggoebel@users.noreply.github.com> Date: Fri, 16 Feb 2024 16:49:46 +0100 Subject: [PATCH] Update category filter when scenarios update (#197) --- src/app/data/scenario.service.ts | 25 +++++++++++++++---- .../filter-scenarios.component.ts | 16 +++++++++--- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/app/data/scenario.service.ts b/src/app/data/scenario.service.ts index 211d8d27..6d07f259 100644 --- a/src/app/data/scenario.service.ts +++ b/src/app/data/scenario.service.ts @@ -109,10 +109,10 @@ export class ScenarioService { ); } - public update(iScenario: Scenario) { - var s = deepCopy(iScenario); + public update(s: Scenario) { + var steps = deepCopy(s.steps); // step by step, re-encode to b64 - s.steps.forEach((st: Step) => { + steps.forEach((st: Step) => { st.title = utoa(st.title); st.content = utoa(st.content); }); @@ -120,14 +120,29 @@ export class ScenarioService { var params = new HttpParams({ encoder: new CustomEncoder() }) .set('name', utoa(s.name)) .set('description', utoa(s.description)) - .set('steps', JSON.stringify(s.steps)) + .set('steps', JSON.stringify(steps)) .set('categories', JSON.stringify(s.categories)) .set('tags', JSON.stringify(s.tags)) .set('virtualmachines', JSON.stringify(s.virtualmachines)) .set('pause_duration', s.pause_duration) .set('keepalive_duration', s.keepalive_duration); - return this.http.put(environment.server + '/a/scenario/' + s.id, params); + return this.http + .put(environment.server + '/a/scenario/' + s.id, params) + .pipe( + tap(() => { + // Find the index of the scenario in the cached list + const index = this.cachedScenarioList.findIndex( + (scenario) => scenario.id === s.id + ); + if (index !== -1) { + // Replace the old scenario with the updated one + this.cachedScenarioList[index] = s; + } + // Update the cache with the new list + this.set(this.cachedScenarioList); + }) + ); } public create(s: Scenario) { diff --git a/src/app/filter-scenarios/filter-scenarios.component.ts b/src/app/filter-scenarios/filter-scenarios.component.ts index fef3b778..24b4286c 100644 --- a/src/app/filter-scenarios/filter-scenarios.component.ts +++ b/src/app/filter-scenarios/filter-scenarios.component.ts @@ -86,11 +86,12 @@ export class FilterScenariosComponent implements OnInit { this.selectRbac = allowed; }); + this.scenarioService.watch().subscribe((s: Scenario[]) => { + this.updateScenarios(s); + }); + this.scenarioService.list().subscribe((s: Scenario[]) => { - this.scenarios = s; - this.calculateCategories(); - this.filterScenarioList(); - this.emitScenarios(this.scenarios); + this.updateScenarios(s); }); this.rbacService @@ -111,4 +112,11 @@ export class FilterScenariosComponent implements OnInit { this.emitScenarios(this.filteredScenarios); }); } + + updateScenarios(s: Scenario[]) { + this.scenarios = s; + this.calculateCategories(); + this.filterScenarioList(); + this.emitScenarios(this.scenarios); + } }