From 08d762589b271e84856a465f24de90aece4b1bb0 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Mon, 6 Jan 2020 18:52:33 +0100
Subject: [PATCH] FEAT #12091 TIME 0:30 fix data validation before action + fix
 filling badge

---
 .../indexing-form/indexing-form.component.ts  | 35 +++++----
 .../app/process/process.component.html        |  4 +-
 src/frontend/app/process/process.component.ts | 72 ++++++++++++-------
 3 files changed, 71 insertions(+), 40 deletions(-)

diff --git a/src/frontend/app/indexation/indexing-form/indexing-form.component.ts b/src/frontend/app/indexation/indexing-form/indexing-form.component.ts
index ebf9b88d041..48c9c86dcf9 100644
--- a/src/frontend/app/indexation/indexing-form/indexing-form.component.ts
+++ b/src/frontend/app/indexation/indexing-form/indexing-form.component.ts
@@ -1,4 +1,4 @@
-import { Component, OnInit, Input, ViewChild } from '@angular/core';
+import { Component, OnInit, Input, ViewChild, EventEmitter, Output } from '@angular/core';
 import { HttpClient } from '@angular/common/http';
 import { LANG } from '../../translate.component';
 import { NotificationService } from '../../notification.service';
@@ -6,7 +6,7 @@ import { HeaderService } from '../../../service/header.service';
 import { MatDialog } from '@angular/material/dialog';
 import { AppService } from '../../../service/app.service';
 import { tap, catchError, finalize, exhaustMap, map, filter } from 'rxjs/operators';
-import { of, forkJoin } from 'rxjs';
+import { of, forkJoin, Subject, Observable } from 'rxjs';
 import { SortPipe } from '../../../plugins/sorting.pipe';
 import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
 import { FormControl, Validators, FormGroup, ValidationErrors, ValidatorFn, AbstractControl } from '@angular/forms';
@@ -34,6 +34,8 @@ export class IndexingFormComponent implements OnInit {
 
     @Input('hideDiffusionList') hideDiffusionList: boolean = false;
 
+    @Output() loadingFormEndEvent = new EventEmitter<string>();
+
     @ViewChild('appDiffusionsList', { static: false }) appDiffusionsList: DiffusionsListComponent;
 
     fieldCategories: any[] = ['mail', 'contact', 'process', 'classifying'];
@@ -338,18 +340,24 @@ export class IndexingFormComponent implements OnInit {
     }
 
     saveData(userId: number, groupId: number, basketId: number) {
-        const formatdatas = this.formatDatas(this.getDatas());
+        if (this.isValidForm()) {
+            const formatdatas = this.formatDatas(this.getDatas());
 
-        this.http.put(`../../rest/resources/${this.resId}?userId=${userId}&groupId=${groupId}&basketId=${basketId}`, formatdatas).pipe(
-            tap(() => {
-                this.currentResourceValues = JSON.parse(JSON.stringify(this.getDatas(false)));;
-                this.notify.success(this.lang.dataUpdated);
-            }),
-            catchError((err: any) => {
-                this.notify.handleErrors(err);
-                return of(false);
-            })
-        ).subscribe();
+            this.http.put(`../../rest/resources/${this.resId}?userId=${userId}&groupId=${groupId}&basketId=${basketId}`, formatdatas).pipe(
+                tap(() => {
+                    this.currentResourceValues = JSON.parse(JSON.stringify(this.getDatas(false)));;
+                    this.notify.success(this.lang.dataUpdated);
+                }),
+                catchError((err: any) => {
+                    this.notify.handleErrors(err);
+                    return of(false);
+                })
+            ).subscribe();
+            return true;
+        } else {
+            this.notify.error(this.lang.mustFixErrors);
+            return false;
+        }
     }
 
     formatDatas(datas: any) {
@@ -678,6 +686,7 @@ export class IndexingFormComponent implements OnInit {
 
     createForm() {
         this.indexingFormGroup = new FormGroup(this.arrFormControl);
+        this.loadingFormEndEvent.emit();
     }
 
     async resetForm() {
diff --git a/src/frontend/app/process/process.component.html b/src/frontend/app/process/process.component.html
index 051a00beb3e..1c6e9a73f32 100644
--- a/src/frontend/app/process/process.component.html
+++ b/src/frontend/app/process/process.component.html
@@ -86,7 +86,7 @@
                 </app-attachments-list>
                 <app-indexing-form *ngIf="currentTool === 'info' && !loading" #indexingForm [groupId]="currentGroupId"
                     [resId]="currentResourceInformations.resId" [indexingFormId]="currentResourceInformations.modelId"
-                    [mode]="'process'" [canEdit]="canEditData" [hideDiffusionList]="true"></app-indexing-form>
+                    [mode]="'process'" [canEdit]="canEditData" [hideDiffusionList]="true" (loadingFormEndEvent)="triggerProcessAction()"></app-indexing-form>
                 <div style="position: sticky;bottom: 0px;text-align:right;">
                     <button mat-fab *ngIf="indexingForm !== undefined && indexingForm.isResourceModified() && currentTool === 'info'"
                         (click)="confirmModification()" color="accent"
@@ -256,7 +256,7 @@
         </app-attachments-list>
         <app-indexing-form *ngIf="modal.id === 'info' && !loading" #indexingForm [groupId]="currentGroupId"
             [indexingFormId]="currentResourceInformations.modelId" [resId]="currentResourceInformations.resId"
-            [mode]="'process'" [canEdit]="canEditData" [hideDiffusionList]="true">
+            [mode]="'process'" [canEdit]="canEditData" [hideDiffusionList]="true" (loadingFormEndEvent)="triggerProcessAction()">
         </app-indexing-form>
         <div style="position: sticky;bottom: 0px;text-align:right;padding:15px;">
             <button mat-fab *ngIf="indexingForm !== undefined && indexingForm.isResourceModified() && modal.id === 'info'"
diff --git a/src/frontend/app/process/process.component.ts b/src/frontend/app/process/process.component.ts
index 16c09f92b03..5fda0d3f1d9 100755
--- a/src/frontend/app/process/process.component.ts
+++ b/src/frontend/app/process/process.component.ts
@@ -129,6 +129,8 @@ export class ProcessComponent implements OnInit {
 
     canEditData: boolean = false;
 
+    autoAction: boolean = false;
+
     @ViewChild('snav', { static: true }) sidenavLeft: MatSidenav;
     @ViewChild('snav2', { static: true }) sidenavRight: MatSidenav;
 
@@ -261,15 +263,15 @@ export class ProcessComponent implements OnInit {
                         const arrInfo = [];
 
                         if (this.empty(data.firstname) && this.empty(data.lastname)) {
-                            this.senderLightInfo = { 'displayName': data.company, 'filling': data.filling };
+                            this.senderLightInfo = { 'displayName': data.company, 'filling': this.contactService.getFillingColor(data.fillingRate.thresholdLevel) };
                         } else {
                             arrInfo.push(data.firstname);
                             arrInfo.push(data.lastname);
                             if (!this.empty(data.company)) {
-                                arrInfo.push('('+data.company+')');
+                                arrInfo.push('(' + data.company + ')');
                             }
-                            
-                            this.senderLightInfo = { 'displayName': arrInfo.filter(info => info !== '').join(' '), 'filling': this.contactService.getFillingColor(data.thresholdLevel) };
+
+                            this.senderLightInfo = { 'displayName': arrInfo.filter(info => info !== '').join(' '), 'filling': this.contactService.getFillingColor(data.fillingRate.thresholdLevel) };
                         }
                     })
                 ).subscribe();
@@ -322,26 +324,46 @@ export class ProcessComponent implements OnInit {
     }
 
     onSubmit() {
-        if (this.isToolModified()) {
-            const dialogRef = this.openConfirmModification();
-            dialogRef.afterClosed().pipe(
-                tap((data: string) => {
-                    if (data !== 'ok') {
-                        this.refreshTool();
-                    }
-                }),
-                filter((data: string) => data === 'ok'),
-                tap(() => {
-                    this.saveTool();
-                }),
-                finalize(() => this.actionService.launchAction(this.selectedAction, this.currentUserId, this.currentGroupId, this.currentBasketId, [this.currentResourceInformations.resId], this.currentResourceInformations, false)),
-                catchError((err: any) => {
-                    this.notify.handleErrors(err);
-                    return of(false);
-                })
-            ).subscribe();
+        if (this.currentTool === 'info' || this.isModalOpen('info')) {
+            this.processAction();
+        } else {
+            this.autoAction = true;
+            this.currentTool = 'info';
+        }
+    }
+
+    triggerProcessAction() {
+        if (this.autoAction) {
+            this.processAction();
+            this.autoAction = !this.autoAction;
+        }
+    }
+
+    processAction() {
+        if (this.indexingForm.isValidForm()) {
+            if (this.isToolModified()) {
+                const dialogRef = this.openConfirmModification();
+                dialogRef.afterClosed().pipe(
+                    tap((data: string) => {
+                        if (data !== 'ok') {
+                            this.refreshTool();
+                        }
+                    }),
+                    filter((data: string) => data === 'ok'),
+                    tap(() => {
+                        this.saveTool();
+                    }),
+                    finalize(() => this.actionService.launchAction(this.selectedAction, this.currentUserId, this.currentGroupId, this.currentBasketId, [this.currentResourceInformations.resId], this.currentResourceInformations, false)),
+                    catchError((err: any) => {
+                        this.notify.handleErrors(err);
+                        return of(false);
+                    })
+                ).subscribe();
+            } else {
+                this.actionService.launchAction(this.selectedAction, this.currentUserId, this.currentGroupId, this.currentBasketId, [this.currentResourceInformations.resId], this.currentResourceInformations, false);
+            }
         } else {
-            this.actionService.launchAction(this.selectedAction, this.currentUserId, this.currentGroupId, this.currentBasketId, [this.currentResourceInformations.resId], this.currentResourceInformations, false);
+            this.notify.error(this.lang.mustFixErrors);
         }
     }
 
@@ -400,8 +422,8 @@ export class ProcessComponent implements OnInit {
         }
     }
 
-    isModalOpen() {
-        return this.modalModule.map(module => module.id).indexOf(this.currentTool) > -1;
+    isModalOpen(tool = this.currentTool) {
+        return this.modalModule.map(module => module.id).indexOf(tool) > -1;
     }
 
     ngOnDestroy() {
-- 
GitLab