From a1fb6fa39035ab9d732d14d5b973017dad2438f1 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Thu, 21 Nov 2019 17:19:16 +0100
Subject: [PATCH] FEAT #11691 TIME 1 add control required in create attachment

---
 .../attachment-create.component.html          | 44 ++++++++++---
 .../attachment-create.component.scss          | 32 +++++++++-
 .../attachment-create.component.ts            | 61 +++++++++++--------
 .../app/viewer/document-viewer.component.ts   |  1 +
 src/frontend/lang/lang-en.ts                  |  6 +-
 src/frontend/lang/lang-fr.ts                  | 32 +++++-----
 src/frontend/lang/lang-nl.ts                  |  4 ++
 7 files changed, 129 insertions(+), 51 deletions(-)

diff --git a/src/frontend/app/attachments/attachment-create/attachment-create.component.html b/src/frontend/app/attachments/attachment-create/attachment-create.component.html
index 41af6ac4c4b..96d7dc40d6b 100644
--- a/src/frontend/app/attachments/attachment-create/attachment-create.component.html
+++ b/src/frontend/app/attachments/attachment-create/attachment-create.component.html
@@ -4,7 +4,7 @@
     </div>
     <h1 mat-dialog-title>
         <span style="flex: 1;">
-            Creation d'une pièce jointe
+            {{lang.attachmentCreation}}
         </span>
         <button *ngIf="!loading" [title]="lang.close" mat-icon-button [disabled]="isDocLoading()"
             (click)="dialogRef.close();">
@@ -12,8 +12,11 @@
         </button></h1>
     <mat-dialog-content class="attach-content">
         <mat-tab-group style="width: 100%;">
-            <mat-tab *ngFor="let attachment of attachments; let i=index" label="PJ n°{{i+1}}"
-                [disabled]="isDocLoading()">
+            <mat-tab *ngFor="let attachment of attachments; let i=index" [disabled]="isDocLoading()">
+                <ng-template mat-tab-label>
+                    <span [class.complete]="isPjValid(i)">PJ n°{{i+1}}&nbsp;<i *ngIf="isPjValid(i)"
+                            class="fa fa-check"></i></span>
+                </ng-template>
                 <div style="display:flex;height:100%;">
                     <div class="attachment-form" *ngIf="!loading">
                         <div class="attachment-form-item">
@@ -21,16 +24,29 @@
                                 [formControlSelect]="attachment['type']" [datas]="attachmentsTypes"
                                 (afterSelected)="getAttachType($event, i)" style="width:100%;">
                             </plugin-select-search>
+                            <div class="fieldState">
+                                <i class="fas fa-asterisk" *ngIf="!attachment['type'].hasError('required')"></i>
+                                <i class="fas fa-times" style="color:red;font-size:15px;"
+                                    *ngIf="attachment['type'].hasError('required')"></i>
+                            </div>
                         </div>
                         <div class="attachment-form-item">
                             <mat-form-field class="input-form">
                                 <input matInput placeholder="{{lang.subject}}" [formControl]="attachment['title']">
                             </mat-form-field>
+                            <div class="fieldState">
+                                <i class="fas fa-asterisk" *ngIf="!attachment['title'].hasError('required')"></i>
+                                <i class="fas fa-times" style="color:red;font-size:15px;"
+                                    *ngIf="attachment['title'].hasError('required')"></i>
+                            </div>
                         </div>
                         <div class="attachment-form-item">
                             <mat-form-field class="input-form">
                                 <input matInput placeholder="{{lang.contact}}">
                             </mat-form-field>
+                            <div class="fieldState">
+                                <i class="fas fa-asterisk noMandatory"></i>
+                            </div>
                         </div>
                         <div class="attachment-form-item">
                             <mat-form-field class="input-form" (click)="picker.open()" style="cursor:pointer;">
@@ -50,19 +66,29 @@
                                     </mat-icon>
                                 </button>
                             </mat-form-field>
+                            <div class="fieldState">
+                                <i class="fas fa-asterisk noMandatory"></i>
+                            </div>
                         </div>
-                        <div class="attachment-form-item">
+                        <div class="attachment-form-item-action">
                             <button mat-raised-button color="primary" (click)="newPj()" [disabled]="isDocLoading()"
-                                style="width:90%;">Nouvelle pièce jointe</button>
+                                style="width:90%;">{{lang.newAttachment}}</button>
                             <button mat-raised-button color="warn" *ngIf="attachments.length > 1" (click)="removePj(i)"
-                                [disabled]="isDocLoading()" style="width:90%;">Enlever la pièce jointe</button>
+                                [disabled]="isDocLoading()" style="width:90%;">{{lang.delAttachment}}</button>
+                            <div class="fieldState">
+                                <i class="fas fa-asterisk noMandatory"></i>
+                            </div>
                         </div>
                     </div>
-                    <div
-                        style="display: flex;flex: 1;align-items: center;justify-content: center;text-align: center;background: #135F7F;overflow: auto;color: white;">
+                    <div class="documentContent">
                         <app-document-viewer #appDocumentViewer style="height:100%;width:100%;position: relative;"
                             [editMode]="true" [mode]="'attachment'" (triggerEvent)="setEncodedFile(i)">
                         </app-document-viewer>
+                        <div class="fieldState stateDoc">
+                            <i class="fas fa-asterisk" *ngIf="!attachment['encodedFile'].hasError('required')"></i>
+                            <i class="fas fa-times" style="color:red;font-size:15px;"
+                                *ngIf="attachment['encodedFile'].hasError('required')"></i>
+                        </div>
                     </div>
                 </div>
             </mat-tab>
@@ -70,7 +96,7 @@
     </mat-dialog-content>
     <div mat-dialog-actions class="actions">
         <button mat-raised-button color="primary" *ngIf="creationMode && !loading" (click)="onSubmit()"
-            [disabled]="!isValid() || isDocLoading()">Valider</button>
+            [disabled]="isDocLoading()">{{lang.validate}}</button>
         <button mat-raised-button mat-button *ngIf="creationMode && !loading" [disabled]="isDocLoading()"
             [mat-dialog-close]="">{{lang.cancel}}</button>
     </div>
diff --git a/src/frontend/app/attachments/attachment-create/attachment-create.component.scss b/src/frontend/app/attachments/attachment-create/attachment-create.component.scss
index f94c1fb2f8a..10a79b6173d 100644
--- a/src/frontend/app/attachments/attachment-create/attachment-create.component.scss
+++ b/src/frontend/app/attachments/attachment-create/attachment-create.component.scss
@@ -39,7 +39,6 @@
 
     &-item {
         display: flex;
-        flex-direction: column;
         align-items: center;
         justify-content: center;
         padding-top: 10px;
@@ -57,6 +56,10 @@
                 padding-right: 5px;
             }
         }
+
+        &-action {
+            flex-direction: column;
+        }
     }
 }
 
@@ -67,4 +70,31 @@
 
 .actions {
     margin: 0px;
+}
+
+.complete {
+    color: green;
+    font-weight: bold;
+}
+
+.fieldState {
+    width: auto !important;
+    padding-top: 0px !important;
+}
+
+.stateDoc {
+    position: absolute;
+    right: 20px;
+    top: 20px;
+}
+
+.documentContent {
+    display: flex;
+    flex: 1;
+    align-items: center;
+    justify-content: center;
+    text-align: center;
+    background: $primary;
+    overflow: auto;
+    color: white;
 }
\ No newline at end of file
diff --git a/src/frontend/app/attachments/attachment-create/attachment-create.component.ts b/src/frontend/app/attachments/attachment-create/attachment-create.component.ts
index f19c7a25961..4bdd0a8d869 100644
--- a/src/frontend/app/attachments/attachment-create/attachment-create.component.ts
+++ b/src/frontend/app/attachments/attachment-create/attachment-create.component.ts
@@ -55,10 +55,6 @@ export class AttachmentCreateComponent implements OnInit {
         this.loadAttachmentTypes();
     }
 
-    loadMailresource(resIdMaster: number) {
-
-    }
-
     loadAttachmentTypes() {
         this.http.get('../../rest/attachmentsTypes').pipe(
             tap((data: any) => {
@@ -111,25 +107,30 @@ export class AttachmentCreateComponent implements OnInit {
     }
 
     onSubmit() {
-        this.sendingData = true;
-        const attach = this.formatAttachments();
-        let arrayRoutes: any = [];
-
-        this.attachments.forEach((element, index: number) => {
-            arrayRoutes.push(this.http.post('../../rest/attachments', attach[index]));
-        });
-
-        forkJoin(arrayRoutes).pipe(
-            tap(() => {
-                this.notify.success(this.lang.attachmentAdded);
-                this.dialogRef.close('success');
-            }),
-            finalize(() => this.sendingData),
-            catchError((err: any) => {
-                this.notify.handleErrors(err);
-                return of(false);
-            })
-        ).subscribe();
+        if (this.isValid()) {
+            this.sendingData = true;
+            const attach = this.formatAttachments();
+            let arrayRoutes: any = [];
+    
+            this.attachments.forEach((element, index: number) => {
+                arrayRoutes.push(this.http.post('../../rest/attachments', attach[index]));
+            });
+    
+            forkJoin(arrayRoutes).pipe(
+                tap(() => {
+                    this.notify.success(this.lang.attachmentAdded);
+                    this.dialogRef.close('success');
+                }),
+                finalize(() => this.sendingData),
+                catchError((err: any) => {
+                    this.notify.handleErrors(err);
+                    return of(false);
+                })
+            ).subscribe();
+        } else {
+            this.notify.error(this.lang.mustCompleteAllAttachments);
+        }
+        
     }
 
     isValid() {
@@ -142,6 +143,14 @@ export class AttachmentCreateComponent implements OnInit {
         return state;
     }
 
+    isPjValid(index: number) {
+        let state = true;
+        if (this.attachFormGroup[index].status === 'INVALID') {
+            state = false;
+        }
+        return state;
+    }
+
     isDocLoading() {
         let state = false;
         this.appDocumentViewer.toArray().forEach((app, index: number) => {
@@ -165,12 +174,12 @@ export class AttachmentCreateComponent implements OnInit {
             encodedFile: new FormControl({ value: '', disabled: false }, [Validators.required])
         });
 
-        this.attachFormGroup.push(new FormGroup(this.attachments[this.attachments.length-1]));
+        this.attachFormGroup.push(new FormGroup(this.attachments[this.attachments.length - 1]));
     }
 
     removePj(i: number) {
-        this.attachments.splice(i,1);
-        this.attachFormGroup.splice(i,1);
+        this.attachments.splice(i, 1);
+        this.attachFormGroup.splice(i, 1);
     }
 
     getAttachType(attachType: any, i: number) {
diff --git a/src/frontend/app/viewer/document-viewer.component.ts b/src/frontend/app/viewer/document-viewer.component.ts
index 28c6bf1e9f4..c2d4c8dfe7b 100644
--- a/src/frontend/app/viewer/document-viewer.component.ts
+++ b/src/frontend/app/viewer/document-viewer.component.ts
@@ -363,6 +363,7 @@ export class DocumentViewerComponent implements OnInit {
         this.dialogRef.afterClosed().pipe(
             filter((data: string) => data === 'ok'),
             tap(() => {
+                this.templateListForm.reset();
                 this.file = {
                     name: '',
                     type: '',
diff --git a/src/frontend/lang/lang-en.ts b/src/frontend/lang/lang-en.ts
index ff19daef0be..8686a9e3a61 100755
--- a/src/frontend/lang/lang-en.ts
+++ b/src/frontend/lang/lang-en.ts
@@ -1306,5 +1306,9 @@ export const LANG_EN = {
     "viewResource"   : "View document",
     "addAttachment"   : "Add an attachment",
     "dateAgo"   : "Since",
-    "dateTo"   : "The"
+    "dateTo"   : "The",
+    "attachmentCreation": "Attachment creation",
+    "newAttachment": "New attachment",
+    "delAttachment": "Delete this attachment",
+    "mustCompleteAllAttachments": "Please, complete all attachments",
 };
diff --git a/src/frontend/lang/lang-fr.ts b/src/frontend/lang/lang-fr.ts
index 4fcf0bc15f5..9a7a4fe5406 100755
--- a/src/frontend/lang/lang-fr.ts
+++ b/src/frontend/lang/lang-fr.ts
@@ -1317,7 +1317,7 @@ export const LANG_FR = {
     "newVersion" : "Nouvelle version",
     "deleteSignedVersion" : "Supprimer la version signée",
     "signedAttachment" : "Pièce jointe signée",
-    "attachmentAdded" : "Pièce jointe créée",
+    "attachmentAdded" : "Pièce(s) jointe(s) créée(s)",
     "attachmentUpdated" : "Pièce jointe modifiée",
     "signedVersionDeleted" : "Version signée supprimée",
     "newVersionAdded" : "Nouvelle version créée",
@@ -1332,17 +1332,21 @@ export const LANG_FR = {
     "goToAdminParams" : "Accéder aux paramètres de l'administration",
     "adminParamsUsers" : "Options administration utilisateurs",
     "chooseAllowedGroups" : "Choisissez les groupes d'affectations autorisés",
-    "reallyWantToDeleteThisGroup"   : "Êtes-vous sûr de vouloir supprimer ce groupe ?",
-    "invalidToken" : "Le jeton est expiré ou n'existe plus",
-    "contact_entity"   : "Entité",
-    "contact_third"   : "Tier bénéficiaire",
-    "confidentialityAndSecurity"   : "Vie privée et sécurité",
-    "modified"   : "Modifiée",
-    "created"   : "Créée",
-    "signedAlt"   : "Signée",
-    "deleteResource"   : "Supprimer le document",
-    "viewResource"   : "Voir le document",
-    "addAttachment"   : "Ajouter une pièce jointe",
-    "dateAgo"   : "Il y a",
-    "dateTo"   : "Le",
+    "reallyWantToDeleteThisGroup": "Êtes-vous sûr de vouloir supprimer ce groupe ?",
+    "invalidToken": "Le jeton est expiré ou n'existe plus",
+    "contact_entity": "Entité",
+    "contact_third": "Tier bénéficiaire",
+    "confidentialityAndSecurity": "Vie privée et sécurité",
+    "modified": "Modifiée",
+    "created": "Créée",
+    "signedAlt": "Signée",
+    "deleteResource": "Supprimer le document",
+    "viewResource": "Voir le document",
+    "addAttachment": "Ajouter une pièce jointe",
+    "dateAgo": "Il y a",
+    "dateTo": "Le",
+    "attachmentCreation": "Creation d'une pièce jointe",
+    "newAttachment": "Nouvelle pièce jointe",
+    "delAttachment": "Enlever la pièce jointe",
+    "mustCompleteAllAttachments": "Veuillez compléter l'ensemble des pièces jointes",
 };
diff --git a/src/frontend/lang/lang-nl.ts b/src/frontend/lang/lang-nl.ts
index 003da42b0bd..7fcde461cf7 100755
--- a/src/frontend/lang/lang-nl.ts
+++ b/src/frontend/lang/lang-nl.ts
@@ -1332,4 +1332,8 @@ export const LANG_NL = {
     "addAttachment"   : "Add an attachment", //_TO_TRANSLATE
     "dateAgo"   : "Since", //_TO_TRANSLATE
     "dateTo"   : "The", //_TO_TRANSLATE
+    "attachmentCreation": "Attachment creation", //_TO_TRANSLATE
+    "newAttachment": "New attachment", //_TO_TRANSLATE
+    "delAttachment": "Delete this attachment", //_TO_TRANSLATE
+    "mustCompleteAllAttachments": "Please, complete all attachments", //_TO_TRANSLATE
 };
-- 
GitLab