From 76ded4f8ebe1e87ae9cbb6573ea1594f8b49678a Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Fri, 4 Nov 2022 10:30:44 +0100
Subject: [PATCH] FIX #21979 TIME 0:10 add commit
 a620778ccf98572636102ec7c4b43801ccfcb632

---
 .../app/service/notification.service.ts       |  7 ++--
 .../signature-method-modal.component.html     |  2 +-
 .../signature-method-modal.component.ts       | 36 +++++++++++++++----
 .../app/service/signatures.service.ts         |  2 ++
 4 files changed, 38 insertions(+), 9 deletions(-)

diff --git a/src/frontend/app/service/notification.service.ts b/src/frontend/app/service/notification.service.ts
index ccd235e1ff..a1b0fa175b 100644
--- a/src/frontend/app/service/notification.service.ts
+++ b/src/frontend/app/service/notification.service.ts
@@ -34,8 +34,11 @@ export class NotificationService {
         toast.present();
     }
 
-    async error(message: string) {
-        const msg = message.includes('lang.') ? this.translate.instant(message) : message;
+    async error(message: any) {
+        if (typeof message === 'object') {
+            message = message.toString();
+        }
+        const msg = message.indexOf('lang.') > -1 ? this.translate.instant(message) : message;
         const toast = await this.toastController.create({
             cssClass: 'notif-error',
             duration: 3000,
diff --git a/src/frontend/app/service/signature-method/signature-method-modal.component.html b/src/frontend/app/service/signature-method/signature-method-modal.component.html
index b779d18068..52c27d6b6f 100644
--- a/src/frontend/app/service/signature-method/signature-method-modal.component.html
+++ b/src/frontend/app/service/signature-method/signature-method-modal.component.html
@@ -1,4 +1,4 @@
 <ion-content>
-    <peculiar-fortify-certificates style="height: 100%;width: 100%;" language="fr" [filters]="filters"
+    <peculiar-fortify-certificates *ngIf="!signaturesService.mustRefreshCerts" style="height: 100%;width: 100%;" language="fr" [filters]="filters"
         (selectionSuccess)="certificateChosen($event)" (selectionCancel)="cancelSign()" hide-footer></peculiar-fortify-certificates>
 </ion-content>
diff --git a/src/frontend/app/service/signature-method/signature-method-modal.component.ts b/src/frontend/app/service/signature-method/signature-method-modal.component.ts
index ba69dafa54..3b9c9fd57e 100644
--- a/src/frontend/app/service/signature-method/signature-method-modal.component.ts
+++ b/src/frontend/app/service/signature-method/signature-method-modal.component.ts
@@ -41,7 +41,6 @@ export class SignatureMethodModalComponent implements OnInit, OnDestroy {
     certificate: any;
     signatureLength: any = null;
 
-    server: any = null;
     api: FortifyAPI = null;
 
     constructor(
@@ -68,7 +67,7 @@ export class SignatureMethodModalComponent implements OnInit, OnDestroy {
             this.filters.keyUsage.push(signatureModeData.keyUsage);
         }
 
-        await this.initFortify();
+        await this.processInitFortify();
     }
 
     async ngOnDestroy(): Promise<void> {
@@ -88,11 +87,30 @@ export class SignatureMethodModalComponent implements OnInit, OnDestroy {
         if (challenge) {
             await this.api.login();
         }
+
+        if (this.signaturesService.mustRefreshCerts) {
+            await this.updateProvider(null);
+        }
         this.api.server.cardReader
             .on('insert', (event: any) => this.updateProvider(event))
             .on('remove', (event: any) => this.updateProvider(event));
     }
 
+    async processInitFortify() {
+        if (this.signaturesService.mustRefreshCerts) {
+            this.loadingController.create({
+                message: this.translate.instant('lang.waiting'),
+                spinner: 'dots'
+            }).then(async (load: HTMLIonLoadingElement) => {
+                load.present();
+                await this.initFortify();
+                load.dismiss();
+            });
+        } else {
+            await this.initFortify();
+        }
+    }
+
     async waitFortifyStart() {
         let fortifyState: boolean = false;
         while (!fortifyState) {
@@ -120,9 +138,8 @@ export class SignatureMethodModalComponent implements OnInit, OnDestroy {
             load.present();
 
             try {
-                this.server = certData.detail.socketProvider;
                 this.checkWebsocketSession();
-                this.provider = await this.server.getCrypto(certData.detail.providerId);
+                this.provider = await this.api.server.getCrypto(certData.detail.providerId);
                 this.checkWebsocketSession();
                 this.cert = await this.provider.certStorage.getItem(certData.detail.certificateId);
                 this.checkWebsocketSession();
@@ -147,14 +164,17 @@ export class SignatureMethodModalComponent implements OnInit, OnDestroy {
                 result = await this.sendAndSign(this.idsToProcess[index]);
             }
             load.dismiss();
+            setTimeout(() => {
+                this.signaturesService.mustRefreshCerts = true;
+            }, 500);
             this.modalController.dismiss(result);
         });
     }
 
     async checkWebsocketSession() {
         // session closed?!
-        while (this.server.client.state !== WebSocket.OPEN) {
-            await this.server.connect();
+        while (this.api.server.state !== WebSocket.OPEN) {
+            await this.api.server.connect();
             await new Promise(resolve => setTimeout(resolve, 150));
         }
     }
@@ -172,6 +192,9 @@ export class SignatureMethodModalComponent implements OnInit, OnDestroy {
         } else if (!this.functionsService.empty(event?.action) && event?.action.includes('remove')) {
             this.notificationService.message('lang.outputCardReaderUpdateCerts');
             this.modalController.dismiss('refresh_certs');
+        } else if (this.signaturesService.mustRefreshCerts && event === null) {
+            this.modalController.dismiss('refresh_certs');
+            this.signaturesService.mustRefreshCerts = false;
         }
     }
 
@@ -261,6 +284,7 @@ export class SignatureMethodModalComponent implements OnInit, OnDestroy {
     }
 
     cancelSign() {
+        this.signaturesService.mustRefreshCerts = true;
         this.modalController.dismiss(false);
     }
 
diff --git a/src/frontend/app/service/signatures.service.ts b/src/frontend/app/service/signatures.service.ts
index 15568b7109..1272d4aac4 100755
--- a/src/frontend/app/service/signatures.service.ts
+++ b/src/frontend/app/service/signatures.service.ts
@@ -36,6 +36,8 @@ export class SignaturesContentService {
 
     appSession: any;
 
+    mustRefreshCerts: boolean = true;
+
     private portalHost: DomPortalOutlet;
 
     constructor(
-- 
GitLab