From 06b0a22531f37d137acc21abec44f1cdf0e812f6 Mon Sep 17 00:00:00 2001
From: Hamza HRAMCHI <hamza.hramchi@xelians.fr>
Date: Thu, 20 Jan 2022 11:44:40 +0100
Subject: [PATCH] FEAT #14146 TIME 1:50 WIP: upload new version of doc/pj

---
 .../app/viewer/document-viewer.component.html |  5 ++
 .../app/viewer/document-viewer.component.ts   | 53 +++++++++++++++++--
 .../document-viewer-modal.component.html      |  6 +++
 src/lang/lang-en.json                         |  3 +-
 src/lang/lang-fr.json                         |  3 +-
 5 files changed, 64 insertions(+), 6 deletions(-)

diff --git a/src/frontend/app/viewer/document-viewer.component.html b/src/frontend/app/viewer/document-viewer.component.html
index 9a4d43e8cf7..cebe10e9eb3 100644
--- a/src/frontend/app/viewer/document-viewer.component.html
+++ b/src/frontend/app/viewer/document-viewer.component.html
@@ -75,6 +75,11 @@
                         <mat-icon color="primary" class="fas fa-external-link-alt"></mat-icon>
                         <span>{{'lang.openInExternalModal' | translate}}</span>
                     </button>
+                    <button mat-menu-item *ngIf="(file.contentView !== undefined || base64 !== null) && resId !== null"
+                        [title]="'lang.uploadNewVersion' | translate" (click)="isNewVersion = true; docToUpload.click()" >
+                        <mat-icon color="primary" class="fas fa-file-upload"></mat-icon>
+                        <span style="white-space: pre-wrap;">{{'lang.uploadNewVersion' | translate}}</span>
+                    </button>
                     <button mat-menu-item *ngIf="(file.contentView !== undefined || base64 !== null) && resId != null && mode !== 'attachment' && externalId.signatureBookId" (click)="openMaarchParapheurWorkflow()">
                         <mat-icon color="primary" class="fas fa-list-ol"></mat-icon>
                         <span>{{'lang.externalVisaWorkflow' | translate}}</span>
diff --git a/src/frontend/app/viewer/document-viewer.component.ts b/src/frontend/app/viewer/document-viewer.component.ts
index 69ce664ce00..7ca35dcd813 100755
--- a/src/frontend/app/viewer/document-viewer.component.ts
+++ b/src/frontend/app/viewer/document-viewer.component.ts
@@ -169,6 +169,8 @@ export class DocumentViewerComponent implements OnInit, OnDestroy {
 
     status: string = '';
 
+    isNewVersion: boolean = false;
+
     constructor(
         public translate: TranslateService,
         public http: HttpClient,
@@ -309,7 +311,17 @@ export class DocumentViewerComponent implements OnInit, OnDestroy {
     }
 
     uploadTrigger(fileInput: any) {
-        if (fileInput.target.files && fileInput.target.files[0] && this.isExtensionAllowed(fileInput.target.files[0])) {
+        // CHECK IF WE UPLOADING NEW VERSION FOR DOCUMENT
+        if (this.isNewVersion) {
+            const fileData: any = {
+                name : fileInput.target.files[0].name,
+                type : fileInput.target.files[0].type,
+                format : this.file.name.split('.').pop(),
+                content: null
+            };
+            this.setNewVersion(fileInput, fileData);
+
+        } else if (fileInput.target.files && fileInput.target.files[0] && this.isExtensionAllowed(fileInput.target.files[0])) {
             this.initUpload();
 
             const reader = new FileReader();
@@ -334,6 +346,33 @@ export class DocumentViewerComponent implements OnInit, OnDestroy {
         }
     }
 
+    setNewVersion(fileInput: any, file: any) {
+        const reader = new FileReader();
+        reader.readAsArrayBuffer(fileInput.target.files[0]);
+        reader.onload = (value: any) => {
+            this.triggerEvent.emit('uploadFile');
+            file.content = this.getBase64Document(value.target.result);
+            if (file.type !== 'application/pdf') {
+                const base64Data: any = this.convertDocument(file);
+                console.log(base64Data);
+
+                file.base64 = base64Data.base64src;
+            } else {
+                file.base64 = this.getBase64Document(value.target.result);
+            }
+            const dialogRef = this.dialog.open(DocumentViewerModalComponent, {
+                autoFocus: false,
+                panelClass: 'maarch-full-height-modal',
+                data: {
+                    title: file.name,
+                    filename: file.name,
+                    base64: file.base64,
+                    isNewVersion: true
+                }
+            });
+        };
+    }
+
     initUpload() {
         this.loading = true;
         this.file = {
@@ -391,9 +430,14 @@ export class DocumentViewerComponent implements OnInit, OnDestroy {
             this.upload(data).subscribe(
                 (res: any) => {
                     if (res.encodedResource) {
-                        this.file.base64src = res.encodedResource;
-                        this.file.src = this.base64ToArrayBuffer(res.encodedResource);
-                        this.loading = false;
+                        if (this.isNewVersion) {
+                            file.base64src = res.encodedResource;
+                            file.src = this.base64ToArrayBuffer(res.encodedResource);
+                        } else {
+                            this.file.base64src = res.encodedResource;
+                            this.file.src = this.base64ToArrayBuffer(res.encodedResource);
+                            this.loading = false;
+                        }
                     }
                 },
                 (err: any) => {
@@ -408,6 +452,7 @@ export class DocumentViewerComponent implements OnInit, OnDestroy {
             this.loading = false;
         }
 
+        return file;
     }
 
     upload(data: any) {
diff --git a/src/frontend/app/viewer/modal/document-viewer-modal.component.html b/src/frontend/app/viewer/modal/document-viewer-modal.component.html
index 470ed284579..e5529042b42 100755
--- a/src/frontend/app/viewer/modal/document-viewer-modal.component.html
+++ b/src/frontend/app/viewer/modal/document-viewer-modal.component.html
@@ -12,4 +12,10 @@
             [downloadActions]="data.downloadActions" [isSigned]="data.isSigned">
         </app-document-viewer>
     </mat-dialog-content>
+    <ng-container *ngIf="data.isNewVersion !== undefined">
+        <span class="divider-modal"></span>
+        <div mat-dialog-actions class="actions">
+            <button mat-raised-button color="primary">{{'lang.createNewVersion' | translate}}</button>
+        </div>
+    </ng-container>
 </div>
diff --git a/src/lang/lang-en.json b/src/lang/lang-en.json
index 7f463767445..61478831be9 100644
--- a/src/lang/lang-en.json
+++ b/src/lang/lang-en.json
@@ -2576,5 +2576,6 @@
     "downloadFile": "Download file",
     "emptyShippingHistory": "The Maileva sending history is empty.",
     "depositProofShipping": "Proof of deposit sent by Maileva",
-    "shippingSubscriptionDesc": "Allows you to subscribe to mail follow-up notifications"
+    "shippingSubscriptionDesc": "Allows you to subscribe to mail follow-up notifications",
+    "uploadNewVersion": "Upload a new version of the document"
 }
diff --git a/src/lang/lang-fr.json b/src/lang/lang-fr.json
index 4df12ef8c39..07eb213a835 100644
--- a/src/lang/lang-fr.json
+++ b/src/lang/lang-fr.json
@@ -2576,5 +2576,6 @@
     "downloadFile": "Télécharger le fichier",
     "emptyShippingHistory": "L'historique de l'envoi Maileva est vide.",
     "depositProofShipping": "Preuve de dépôt envoyée par Maileva",
-    "shippingSubscriptionDesc": "Permet de s'abonner aux notifications de suivi d'un courrier"
+    "shippingSubscriptionDesc": "Permet de s'abonner aux notifications de suivi d'un courrier",
+    "uploadNewVersion": "Téléverser une nouvelle version du document"
 }
-- 
GitLab