From 87deb2f77da2aab2c2f7310ff29d26382b6d533c Mon Sep 17 00:00:00 2001
From: AndiMajore <andi.majore@googlemail.com>
Date: Wed, 12 Apr 2023 18:24:22 +0200
Subject: [PATCH] added loading results of views and analysis when clicking on
 toast

---
 src/app/components/toast/toast.component.html |  2 +-
 src/app/components/toast/toast.component.ts   |  6 +++-
 src/app/interfaces.ts                         |  3 +-
 .../explorer-page/explorer-page.component.ts  | 16 +++++++++++
 src/app/services/analysis/analysis.service.ts | 28 ++++++++++++++++---
 src/app/services/toast/toast.service.ts       |  6 ++++
 6 files changed, 54 insertions(+), 7 deletions(-)

diff --git a/src/app/components/toast/toast.component.html b/src/app/components/toast/toast.component.html
index f1e62739..f1e23f42 100644
--- a/src/app/components/toast/toast.component.html
+++ b/src/app/components/toast/toast.component.html
@@ -1,6 +1,6 @@
 <div class="toast-holder">
   <div *ngFor="let toast of toasts | keyvalue">
-    <div class="toast {{ getDrugstoneClass(toast.value.type) }}">
+    <div class="toast {{ getDrugstoneClass(toast.value.type) }}" (click)="click(toast.key)">
       <a (click)="close(toast.key)" aria-label="close" class="close">
         <app-fa-solid-icon
           title="Close analysis"
diff --git a/src/app/components/toast/toast.component.ts b/src/app/components/toast/toast.component.ts
index 0f0f5731..45b35d17 100644
--- a/src/app/components/toast/toast.component.ts
+++ b/src/app/components/toast/toast.component.ts
@@ -30,5 +30,9 @@ export class ToastComponent implements OnInit {
   public close(id: number) {
     this.toast.deleteToast(id);
   }
-  
+
+  public click(id: number) {
+    this.toast.toastClicked(id);
+  }
+
 }
diff --git a/src/app/interfaces.ts b/src/app/interfaces.ts
index 64f1b369..becf4218 100644
--- a/src/app/interfaces.ts
+++ b/src/app/interfaces.ts
@@ -266,7 +266,8 @@ export interface Algorithm {
 
 export interface Toast {
   message: string;
-  type: 'success' | 'info' | 'warning' | 'danger'
+  type: 'success' | 'info' | 'warning' | 'danger';
+  callback?: () => void;
 }
 
 export interface LiveToasts {
diff --git a/src/app/pages/explorer-page/explorer-page.component.ts b/src/app/pages/explorer-page/explorer-page.component.ts
index 32e73db3..c616916e 100644
--- a/src/app/pages/explorer-page/explorer-page.component.ts
+++ b/src/app/pages/explorer-page/explorer-page.component.ts
@@ -172,6 +172,20 @@ export class ExplorerPageComponent implements OnInit, AfterViewInit {
     return this.selectedToken;
   }
 
+  public setViewToken(token: string | null) {
+    this.selectedViewToken = token;
+  }
+
+  public setTaskToken(token: string | null) {
+    this.selectedAnalysisToken = token;
+  }
+
+
+
+  public bind(f: (token: (string | null)) => void) {
+    return f.bind(this);
+  }
+
   @Input() set taskId(token: string | null) {
     if (token == null || token.length === 0) {
       this.selectedAnalysisToken = null;
@@ -232,6 +246,8 @@ export class ExplorerPageComponent implements OnInit, AfterViewInit {
 
   ngOnInit() {
     this.setWindowWidth(document.getElementById('appWindow').getBoundingClientRect().width);
+    this.analysis.setViewTokenCallback(this.setViewToken.bind(this));
+    this.analysis.setTaskTokenCallback(this.setTaskToken.bind(this));
   }
 
   async ngAfterViewInit() {
diff --git a/src/app/services/analysis/analysis.service.ts b/src/app/services/analysis/analysis.service.ts
index 963fbbdb..3afeaa51 100644
--- a/src/app/services/analysis/analysis.service.ts
+++ b/src/app/services/analysis/analysis.service.ts
@@ -76,6 +76,10 @@ export class AnalysisService {
 
   private tissues: Tissue[] = [];
 
+  private viewTokenCallback: (task: (string | null)) => void;
+
+  private taskTokenCallback: (task: (string | null)) => void;
+
   constructor(
     public toast: ToastService,
     private http: HttpClient,
@@ -104,6 +108,14 @@ export class AnalysisService {
     });
   }
 
+  setViewTokenCallback(f): void {
+    this.viewTokenCallback = f;
+  }
+
+  setTaskTokenCallback(f): void {
+    this.taskTokenCallback = f;
+  }
+
   setViewInfos(): void {
     this.netex.getViewInfos(this.viewTokens).then(res => {
       // @ts-ignore
@@ -364,9 +376,11 @@ export class AnalysisService {
     localStorage.setItem(this.selectionsCookieKey, JSON.stringify(this.viewTokens));
 
     this.toast.setNewToast({
-      message: 'Analysis task started. This may take a while. ' +
-        `Once the computation finished you can view the results in the task list to the ${this.drugstoneConfig.config.showSidebar}.`,
-      type: 'success'
+      message: 'New network view based of the selection has been created. Load the new view by clicking here or on the entry in the \'Views\' list to the ' + this.drugstoneConfig.config.showSidebar,
+      type: 'success',
+      callback: () => {
+        this.viewTokenCallback(resp.token);
+      }
     });
     // @ts-ignore
     return resp.token;
@@ -494,9 +508,14 @@ export class AnalysisService {
   showToast(task: Task, status: 'DONE' | 'FAILED') {
     let toastMessage;
     let toastType;
+    let onClick = () => {
+    };
     if (status === 'DONE') {
-      toastMessage = 'Computation finished successfully. Click the task in the task list to view the results.';
+      toastMessage = 'Computation finished successfully. Click here or the task in the task list to view the results.';
       toastType = 'success';
+      onClick = () => {
+        this.taskTokenCallback(task.token);
+      };
     } else if (status === 'FAILED') {
       toastMessage = 'Computation failed.';
       toastType = 'danger';
@@ -505,6 +524,7 @@ export class AnalysisService {
     this.toast.setNewToast({
       message: toastMessage,
       type: toastType,
+      callback: onClick
     });
   }
 
diff --git a/src/app/services/toast/toast.service.ts b/src/app/services/toast/toast.service.ts
index 83748b89..9916f7ed 100644
--- a/src/app/services/toast/toast.service.ts
+++ b/src/app/services/toast/toast.service.ts
@@ -36,6 +36,12 @@ export class ToastService {
     }
   }
 
+  public toastClicked(id: number) {
+    if (this.liveToasts.hasOwnProperty(id)) {
+      this.liveToasts[id].callback();
+    }
+  }
+
   get getToasts$ () {
     return this.getToasts.asObservable();
   }
-- 
GitLab