From a02ef95843dab9ba264c38e4b09d92aceb8e61c3 Mon Sep 17 00:00:00 2001
From: Hamza HRAMCHI <hamza.hramchi@xelians.fr>
Date: Tue, 17 May 2022 10:06:20 +0200
Subject: [PATCH] FIX #20778 TIME 0:35 add a wait time for documents being
 converted + loader

---
 lang/en.json                                  |  3 +-
 lang/fr.json                                  |  3 +-
 .../app/document/document.component.html      |  7 ++-
 .../app/document/document.component.scss      |  6 +-
 .../app/document/document.component.ts        | 52 ++++++++++++++--
 .../app/indexation/indexation.component.ts    | 61 +++++++++++--------
 6 files changed, 95 insertions(+), 37 deletions(-)

diff --git a/lang/en.json b/lang/en.json
index c9a9c11929..9d210c5bed 100755
--- a/lang/en.json
+++ b/lang/en.json
@@ -649,6 +649,7 @@
 		"licence": "GNU GPLv3 license",
         "groupsToManage": "Choose the authorized assignment groups",
 		"unlinkGroup": "Unlink group",
-        "emptyGroups": "No groups available to associate"
+        "emptyGroups": "No groups available to associate",
+        "errorConvertingDocument": "Error converting document"
     }
 }
\ No newline at end of file
diff --git a/lang/fr.json b/lang/fr.json
index bfcad10f72..ec399440da 100755
--- a/lang/fr.json
+++ b/lang/fr.json
@@ -648,6 +648,7 @@
 		"licence": "licence GNU GPLv3",
 		"groupsToManage": "Choisir les groupes d'affectations autorisés",
 		"unlinkGroup": "Dissocier le groupe",
-		"emptyGroups": "Aucun groupe disponible à associer"
+		"emptyGroups": "Aucun groupe disponible à associer",
+		"errorConvertingDocument": "Erreur lors de la conversion du document"
 	}
 }
diff --git a/src/frontend/app/document/document.component.html b/src/frontend/app/document/document.component.html
index 674e1f7902..2ab271c068 100755
--- a/src/frontend/app/document/document.component.html
+++ b/src/frontend/app/document/document.component.html
@@ -44,9 +44,12 @@
     *ngIf="authService.user.substitute !== null && docList[currentDoc]">
     <ion-label style="font-size: 13px;">{{'lang.substitutionInfo' | translate}}</ion-label>
 </ion-toolbar>
-<ion-toolbar class="ion-text-center" color="danger" *ngIf="mainDocument.id !== 0 && mainDocument.status !== 'READY'">
+<ion-toolbar class="ion-text-center" color="danger" *ngIf="mainDocument.id !== 0 && mainDocument.status === 'CONVERTING'">
     <ion-label style="font-size: 13px;">{{'lang.convertingDocument' | translate}}</ion-label>
 </ion-toolbar>
+<ion-toolbar class="ion-text-center" color="danger" *ngIf="mainDocument.id !== 0 && mainDocument.status === 'ERROR'">
+    <ion-label style="font-size: 13px;">{{'lang.errorConvertingDocument' | translate}}</ion-label>
+</ion-toolbar>
 <ion-content *ngIf="!loadingdocument" #mainContent>
     <ng-container *ngIf="(mainDocument.notes !== undefined && mainDocument.notes !== null) || hasWorkflowNotes">
         <ion-fab-button *ngIf="!expandedNote" ngDraggable [bounds]="myBounds" [inBounds]="true"
@@ -134,7 +137,7 @@
 
 </ion-content>
 <ion-footer *ngIf="!loadingImage && currentDoc === 0" class="ion-no-border footer-buttons" [ngStyle]="{'grid-template-columns': actionsList.length === 2 ? 'repeat(2, 1fr)' : 'repeat(3, 1fr)'}">
-    <ion-button [disabled]="mainDocument.status === 'CONVERTING'" *ngFor="let action of actionsList" [color]="action.color" shape="round" size="large" fill="outline" (click)="launchEvent(action)" style="width: auto;">
+    <ion-button [disabled]="isNotReady()" *ngFor="let action of actionsList" [color]="action.color" shape="round" size="large" fill="outline" (click)="launchEvent(action)" style="width: auto;">
         <ion-icon *ngIf="action.logo !== ''" [slot]="'start'" [name]="action.logo"></ion-icon>
         <ion-label style="font-size: 13px;">{{getLabel(action)}}</ion-label>
     </ion-button>
diff --git a/src/frontend/app/document/document.component.scss b/src/frontend/app/document/document.component.scss
index 5719a22075..118403742b 100644
--- a/src/frontend/app/document/document.component.scss
+++ b/src/frontend/app/document/document.component.scss
@@ -398,7 +398,7 @@ button.disabled {
     .popover-content{
             height: 50%;
             top: 50px;
-        }
+    }
 }
 
 ::ng-deep .custom-popover-class {
@@ -407,3 +407,7 @@ button.disabled {
         top: 50px;
     }
 }
+
+::ng-deep.popover-viewport.sc-ion-popover-md {
+    overflow: auto;
+}
diff --git a/src/frontend/app/document/document.component.ts b/src/frontend/app/document/document.component.ts
index ffbda0c365..a8c15c6c11 100755
--- a/src/frontend/app/document/document.component.ts
+++ b/src/frontend/app/document/document.component.ts
@@ -18,7 +18,7 @@ import { LocalStorageService } from '../service/local-storage.service';
 import { ActionSheetController, AlertController, LoadingController, MenuController, ModalController, NavController } from '@ionic/angular';
 import { NgxExtendedPdfViewerService } from 'ngx-extended-pdf-viewer';
 import { catchError, exhaustMap, tap } from 'rxjs/operators';
-import { of, Subscription } from 'rxjs';
+import { of, Subscription, timer } from 'rxjs';
 import { SignatureMethodService } from '../service/signature-method/signature-method.service';
 import { FunctionsService } from '../service/functions.service';
 import { ActionsService } from '../service/actions.service';
@@ -74,6 +74,7 @@ export class DocumentComponent implements OnInit, OnDestroy {
     };
 
     subscription: Subscription;
+    timerSubscription: Subscription;
 
     signaturesContent: any = [];
     docList: any = [];
@@ -135,17 +136,15 @@ export class DocumentComponent implements OnInit, OnDestroy {
         });
     }
 
-    ngOnDestroy(): void {
-        this.subscription.unsubscribe();
-    }
-
     imageLoaded(ev: any) {
         this.userMode = this.mainDocument.workflow.find((item: any) => item.current)?.mode;
         if (this.userMode === 'note') {
             this.actionsList = this.actionsList.filter((item: any) => item.event !== 'openSignatures');
         }
         this.getImageDimensions(true);
-        this.load.dismiss();
+        if (this.mainDocument.status !== 'CONVERTING') {
+            this.load.dismiss();
+        }
         this.menu.enable(true, 'right-menu');
         this.loadingImage = false;
         document.getElementsByClassName('drag-scroll-content')[0].scrollTop = 0;
@@ -310,6 +309,11 @@ export class DocumentComponent implements OnInit, OnDestroy {
         });
     }
 
+    ngOnDestroy(): void {
+        this.timerSubscription?.unsubscribe();
+        this.subscription.unsubscribe();
+    }
+
     initDocumentInfos(params: any) {
         this.http.get('../rest/documents/' + params['id']).pipe(
             tap((data: any) => {
@@ -391,6 +395,38 @@ export class DocumentComponent implements OnInit, OnDestroy {
                 // this.renderPdf();
                 this.renderImage();
                 this.loadingdocument = false;
+                this.load.dismiss();
+
+                if (this.mainDocument.status === 'CONVERTING') {
+                    // CHECK DOCUMENT STATUS
+                    this.loadingController.create({
+                        message: this.translate.instant('lang.convertingDocument'),
+                        spinner: 'dots'
+                    }).then((loader: HTMLIonLoadingElement) => {
+                        this.load = loader;
+                        this.load.present();
+                        // timer(0, 10000) call the function immediately and every 10 seconds
+                        this.timerSubscription = timer(0, 10000).pipe(
+                            tap(() => {
+                                this.http.get('../rest/documents/' + params['id']).pipe(
+                                    tap((res: any) => {
+                                        if (res.document.status !== 'CONVERTING') {
+                                            this.totalPages = res.document.pages;
+                                            this.mainDocument.status = res.document.status;
+                                            this.load.dismiss();
+                                            this.timerSubscription?.unsubscribe();
+                                        }
+                                    })
+                                ).subscribe();
+                            }),
+                            catchError((err: any) => {
+                                this.load.dismiss();
+                                this.notificationService.handleErrors(err);
+                                return of(false);
+                            })
+                        ).subscribe();
+                    });
+                }
             }),
             catchError((err: any) => {
                 console.log('error', err);
@@ -769,4 +805,8 @@ export class DocumentComponent implements OnInit, OnDestroy {
             }
         }
     }
+
+    isNotReady() {
+        return ['CONVERTING', 'ERROR'].indexOf(this.mainDocument.status) > -1;
+    }
 }
diff --git a/src/frontend/app/indexation/indexation.component.ts b/src/frontend/app/indexation/indexation.component.ts
index b9623e4795..c5a9853fdc 100644
--- a/src/frontend/app/indexation/indexation.component.ts
+++ b/src/frontend/app/indexation/indexation.component.ts
@@ -301,35 +301,44 @@ export class IndexationComponent implements OnInit {
     }
 
     uploadTrigger(fileInput: any) {
-        if (fileInput.target.files && fileInput.target.files[0] && this.isExtensionAllowed(fileInput.target.files)) {
-            for (let index = 0; index < fileInput.target.files.length; index++) {
-                const filename = fileInput.target.files[index].name;
-                const file = {
-                    title: filename.substr(0, filename.lastIndexOf('.')),
-                    reference: filename.substr(0, filename.lastIndexOf('.')).substr(0, 53),
-                    mainDocument: true,
-                    content: ''
-                };
-                const reader = new FileReader();
-                reader.readAsArrayBuffer(fileInput.target.files[index]);
-                reader.onload = (value: any) => {
-                    file.mainDocument = this.filesToUpload.length === 0;
-                    file.reference = this.filesToUpload.length === 0 ? file.reference : '';
-                    file.content = this.getBase64Document(value.target.result);
-                    this.filesToUpload.push(file);
-                    if (this.filesToUpload.length === 1) {
-                        setTimeout(() => {
-                            this.menu.open('right-menu');
-                        }, 500);
-                    }
-                };
+        this.loadingController.create({
+            message: this.translate.instant('lang.loadingDocument'),
+            spinner: 'dots'
+        }).then(async (load: HTMLIonLoadingElement) => {
+            load.present();
+            if (fileInput.target.files && fileInput.target.files[0] && this.isExtensionAllowed(fileInput.target.files)) {
+                for (let index = 0; index < fileInput.target.files.length; index++) {
+                    const filename = fileInput.target.files[index].name;
+                    const file = {
+                        title: filename.substr(0, filename.lastIndexOf('.')),
+                        reference: filename.substr(0, filename.lastIndexOf('.')).substr(0, 53),
+                        mainDocument: true,
+                        content: ''
+                    };
+                    const reader = new FileReader();
+                    reader.readAsArrayBuffer(fileInput.target.files[index]);
+                    reader.onload = (value: any) => {
+                        file.mainDocument = this.filesToUpload.length === 0;
+                        file.reference = this.filesToUpload.length === 0 ? file.reference : '';
+                        file.content = this.getBase64Document(value.target.result);
+                        this.filesToUpload.push(file);
+                        if (this.filesToUpload.length === 1) {
+                            setTimeout(() => {
+                                this.menu.open('right-menu');
+                            }, 500);
+                        }
+                    };
+                }
+                this.fileImport.nativeElement.value = '';
+                load.dismiss();
+            } else {
+                this.loading = false;
+                load.dismiss();
             }
-            this.fileImport.nativeElement.value = '';
-        } else {
-            this.loading = false;
-        }
+        });
     }
 
+
     isExtensionAllowed(files: any[]) {
         for (let index = 0; index < files.length; index++) {
             if (files[index].name.toLowerCase().split('.').pop() !== 'pdf') {
-- 
GitLab