From a8c0cd1690cb8a4cc0b5fc24df1a7d0030258f45 Mon Sep 17 00:00:00 2001
From: Jean-Pascal <jean-pascal.aubry@inrae.fr>
Date: Tue, 5 Dec 2023 13:51:13 +0000
Subject: [PATCH] fix: add optional chaining(?) to manage undefined id case in
 setValue() and rename cancelValue function() to emptySelectedList Refs #637

---
 .../components/generic-select/generic-select.component.html  | 2 +-
 .../select-field-line/select-field-line.component.ts         | 5 ++---
 src/app/formulaire/elements/select/select-field.ts           | 2 +-
 3 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/src/app/components/generic-select/generic-select.component.html b/src/app/components/generic-select/generic-select.component.html
index 86622ddf0..1597c621c 100644
--- a/src/app/components/generic-select/generic-select.component.html
+++ b/src/app/components/generic-select/generic-select.component.html
@@ -11,7 +11,7 @@
         </mat-option>
     </mat-select>
     <button mat-button *ngIf="showClearButton" matSuffix mat-icon-button aria-label="Clear"
-        (click)="cancelValue($event)">
+        (click)="emptySelectedList($event)">
         <mat-icon>close</mat-icon>
     </button>
     <div *ngIf="enableHelpButton" class="overlap-select">
diff --git a/src/app/components/select-field-line/select-field-line.component.ts b/src/app/components/select-field-line/select-field-line.component.ts
index 7ca5a451d..ddc5f24f8 100644
--- a/src/app/components/select-field-line/select-field-line.component.ts
+++ b/src/app/components/select-field-line/select-field-line.component.ts
@@ -59,15 +59,14 @@ export class SelectFieldLineComponent implements OnInit {
         return this._select.getValue();
     }
 
-    public onValueChange($event) {
+    public onValueChange($event: any) {
         this.selectedValue = $event.value
         this.valueChanged.emit(this.selectedValue)
     }
 
-    public cancelValue($event): any {
+    public emptySelectedList($event:any) {
         this.selectedValue = [];
         this.valueChanged.emit(this.selectedValue)
-        // this.validChange.emit(false)
         $event.stopPropagation();
     }
 
diff --git a/src/app/formulaire/elements/select/select-field.ts b/src/app/formulaire/elements/select/select-field.ts
index 59e0c8d21..b91451580 100644
--- a/src/app/formulaire/elements/select/select-field.ts
+++ b/src/app/formulaire/elements/select/select-field.ts
@@ -82,7 +82,7 @@ export abstract class SelectField extends Field {
                 || (
                     !Array.isArray(previousSelectedEntry)
                     && !Array.isArray(v)
-                    && previousSelectedEntry.id !== v.id
+                    && previousSelectedEntry?.id !== v?.id
                 )
                 || (
                     Array.isArray(previousSelectedEntry)
-- 
GitLab