fix: country flag and phone now in sync (#6727)

* fix: country flag and phone now in sync

* change default country

---------

Co-authored-by: Elio Bischof <[email protected]>
Co-authored-by: Elio Bischof <[email protected]>
This commit is contained in:
Miguel Cabrerizo
2023-10-24 13:47:44 +00:00
committed by GitHub
co-authored by Elio Bischof Elio Bischof
parent 36eeae1071
commit 6f82285ad6
19 changed files with 102 additions and 40 deletions
@@ -2,7 +2,7 @@ import { Location } from '@angular/common';
import { ChangeDetectorRef, Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { AbstractControl, UntypedFormBuilder, UntypedFormGroup, ValidatorFn, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { Subject } from 'rxjs';
import { Subject, debounceTime } from 'rxjs';
import { AddHumanUserRequest } from 'src/app/proto/generated/zitadel/management_pb';
import { Domain } from 'src/app/proto/generated/zitadel/org_pb';
import { PasswordComplexityPolicy } from 'src/app/proto/generated/zitadel/policy_pb';
@@ -35,7 +35,11 @@ export class UserCreateComponent implements OnInit, OnDestroy {
public user: AddHumanUserRequest.AsObject = new AddHumanUserRequest().toObject();
public genders: Gender[] = [Gender.GENDER_FEMALE, Gender.GENDER_MALE, Gender.GENDER_UNSPECIFIED];
public languages: string[] = supportedLanguages;
public selected: CountryPhoneCode | undefined;
public selected: CountryPhoneCode | undefined = {
countryCallingCode: '1',
countryCode: 'US',
countryName: 'United States of America',
};
public countryPhoneCodes: CountryPhoneCode[] = [];
public userForm!: UntypedFormGroup;
public pwdForm!: UntypedFormGroup;
@@ -145,6 +149,14 @@ export class UserCreateComponent implements OnInit, OnDestroy {
});
}
});
this.phone?.valueChanges.pipe(debounceTime(200)).subscribe((value: string) => {
const phoneNumber = formatPhone(value);
if (phoneNumber) {
this.selected = this.countryPhoneCodes.find((code) => code.countryCode === phoneNumber.country);
this.phone?.setValue(phoneNumber.phone);
}
});
}
public createUser(): void {
@@ -175,8 +187,10 @@ export class UserCreateComponent implements OnInit, OnDestroy {
if (this.phone && this.phone.value) {
// Try to parse number and format it according to country
const phoneNumber = formatPhone(this.phone.value);
this.selected = this.countryPhoneCodes.find((code) => code.countryCode === phoneNumber.country);
humanReq.setPhone(new AddHumanUserRequest.Phone().setPhone(phoneNumber.phone));
if (phoneNumber) {
this.selected = this.countryPhoneCodes.find((code) => code.countryCode === phoneNumber.country);
humanReq.setPhone(new AddHumanUserRequest.Phone().setPhone(phoneNumber.phone));
}
}
this.mgmtService
@@ -258,4 +272,14 @@ export class UserCreateComponent implements OnInit, OnDestroy {
return;
}
}
public compareCountries(i1: CountryPhoneCode, i2: CountryPhoneCode) {
return (
i1 &&
i2 &&
i1.countryCallingCode === i2.countryCallingCode &&
i1.countryCode == i2.countryCode &&
i1.countryName == i2.countryName
);
}
}