From 7de9649433f62acb600a625df8386048c11b3280 Mon Sep 17 00:00:00 2001
From: Etienne LOUPIAS <eloupias@grandlyon.com>
Date: Mon, 16 Sep 2024 10:54:53 +0200
Subject: [PATCH] fix(profile): format tel number for all phone inputs

---
 src/app/contact/contact.component.html                |  2 +-
 .../account-info/account-info.component.html          |  6 +-----
 .../structure-contact.component.html                  |  2 +-
 .../structure-orientator.component.ts                 |  5 +++--
 .../mediation-beneficiary-info.component.ts           |  7 ++++---
 src/app/shared/components/input/input.component.ts    |  6 ++++++
 src/app/utils/utils.ts                                | 11 -----------
 7 files changed, 16 insertions(+), 23 deletions(-)

diff --git a/src/app/contact/contact.component.html b/src/app/contact/contact.component.html
index 92ef14404..a254324c4 100644
--- a/src/app/contact/contact.component.html
+++ b/src/app/contact/contact.component.html
@@ -33,7 +33,7 @@
     autocomplete="on"
     [status]="contactForm.get('phone').value ? (contactForm.get('phone').invalid ? 'error' : 'success') : null"
     [value]="contactForm.get('phone').value"
-    (valueChange)="contactForm.get('phone').setValue($event); utils.modifyPhoneInput(contactForm, 'phone', $event)"
+    (valueChange)="contactForm.get('phone').setValue($event)"
   />
 
   <app-input
diff --git a/src/app/form/form-view/account-form/account-info/account-info.component.html b/src/app/form/form-view/account-form/account-info/account-info.component.html
index 7d82fa96e..c98c7efeb 100644
--- a/src/app/form/form-view/account-form/account-info/account-info.component.html
+++ b/src/app/form/form-view/account-form/account-info/account-info.component.html
@@ -35,11 +35,7 @@
       [required]="true"
       [status]="accountForm.get('phone').value ? (accountForm.get('phone').invalid ? 'error' : 'success') : null"
       [value]="accountForm.get('phone').value"
-      (valueChange)="
-        accountForm.get('phone').setValue($event);
-        utils.modifyPhoneInput(accountForm, 'phone', $event);
-        setValidationsForm()
-      "
+      (valueChange)="accountForm.get('phone').setValue($event); setValidationsForm()"
     />
   </div>
 </form>
diff --git a/src/app/form/form-view/structure-form/structure-contact/structure-contact.component.html b/src/app/form/form-view/structure-form/structure-contact/structure-contact.component.html
index bdbbd6870..86067bbe9 100644
--- a/src/app/form/form-view/structure-form/structure-contact/structure-contact.component.html
+++ b/src/app/form/form-view/structure-form/structure-contact/structure-contact.component.html
@@ -21,7 +21,7 @@
       type="tel"
       [status]="getContactPhoneStatus()"
       [value]="structureForm.get('contactPhone').value"
-      (valueChange)="utils.modifyPhoneInput(structureForm, 'contactPhone', $event); setValidationsForm()"
+      (valueChange)="structureForm.get('contactPhone').setValue($event); setValidationsForm()"
     />
   </div>
 </form>
diff --git a/src/app/form/orientation-form-view/global-components/structure-orientator/structure-orientator.component.ts b/src/app/form/orientation-form-view/global-components/structure-orientator/structure-orientator.component.ts
index 55e12b573..562921453 100644
--- a/src/app/form/orientation-form-view/global-components/structure-orientator/structure-orientator.component.ts
+++ b/src/app/form/orientation-form-view/global-components/structure-orientator/structure-orientator.component.ts
@@ -52,8 +52,9 @@ export class StructureOrientatorComponent implements OnInit {
 
   public updatedForm(field: string, value: string): void {
     if (field === 'structurePhone') {
-      this.utils.modifyPhoneInput(this.form, 'structurePhone', value);
-    } else this.form.get(field).patchValue(value);
+      value = this.utils.modifyPhoneValue(value);
+    }
+    this.form.get(field).patchValue(value);
 
     this.validatePage.emit(this.form.valid);
   }
diff --git a/src/app/form/orientation-form-view/online-demarch/online-mediation/mediation-beneficiary-info/mediation-beneficiary-info.component.ts b/src/app/form/orientation-form-view/online-demarch/online-mediation/mediation-beneficiary-info/mediation-beneficiary-info.component.ts
index ac62cd667..ddc31962d 100644
--- a/src/app/form/orientation-form-view/online-demarch/online-mediation/mediation-beneficiary-info/mediation-beneficiary-info.component.ts
+++ b/src/app/form/orientation-form-view/online-demarch/online-mediation/mediation-beneficiary-info/mediation-beneficiary-info.component.ts
@@ -24,13 +24,14 @@ export class MediationBeneficiaryInfoComponent implements OnInit {
     return Boolean(this.form.get('email'));
   }
   public getSup(): string {
-    return this.isPhone() && this.isEmail() ? '1' : '';
+    return this.isPhone() && this.isEmail() ? '1' : '*';
   }
 
   public updatedForm(field: string, value: string): void {
     if (field === 'phone') {
-      this.utils.modifyPhoneInput(this.form, 'phone', value);
-    } else this.form.get(field).patchValue(value);
+      value = this.utils.modifyPhoneValue(value);
+    }
+    this.form.get(field).patchValue(value);
 
     this.checkValidation.emit();
   }
diff --git a/src/app/shared/components/input/input.component.ts b/src/app/shared/components/input/input.component.ts
index f96f0ea0a..9f92ff241 100644
--- a/src/app/shared/components/input/input.component.ts
+++ b/src/app/shared/components/input/input.component.ts
@@ -1,5 +1,6 @@
 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
 import { CustomRegExp } from 'src/app/utils/CustomRegExp';
+import { Utils } from '../../../utils/utils';
 
 @Component({
   selector: 'app-input',
@@ -55,6 +56,8 @@ export class InputComponent implements OnInit {
 
   @Output() finishedEditing = new EventEmitter<string>();
 
+  constructor(private utils: Utils) {}
+
   public hasIconInField = false;
   private example = '';
 
@@ -80,6 +83,9 @@ export class InputComponent implements OnInit {
         this.setStatusSuccess();
       }
     }
+    if (this.type === 'tel') {
+      this.value = this.utils.modifyPhoneValue(this.value);
+    }
     this.valueChange.emit(this.value);
   }
 
diff --git a/src/app/utils/utils.ts b/src/app/utils/utils.ts
index 9753d60b9..36eedd9dc 100644
--- a/src/app/utils/utils.ts
+++ b/src/app/utils/utils.ts
@@ -17,17 +17,6 @@ export type CanExitResolver = (value: boolean) => void;
   providedIn: 'root',
 })
 export class Utils {
-  public modifyPhoneInput(form: UntypedFormGroup, controlName: string, phoneNumber: string): void {
-    form.get(controlName).setValue(phoneNumber);
-    // Take length of phone number without spaces.
-    const phoneNoSpace = phoneNumber.replace(/\s/g, '');
-    // Check to refresh every 2 number.
-    if (phoneNoSpace.length % 2 === 0) {
-      // Add space every 2 number
-      form.get(controlName).setValue(phoneNoSpace.replace(/(?!^)(?=(?:\d{2})+$)/g, ' ')); // NOSONAR
-    }
-  }
-
   public modifyPhoneValue(phoneNumber: string): string {
     // Take length of phone number without spaces.
     const phoneNoSpace = phoneNumber.replace(/\s/g, '');
-- 
GitLab