Skip to content

Commit

Permalink
update validation timing for search johs
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaed Parkar committed Nov 7, 2023
1 parent 20478ca commit d8c15ef
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<input id="judiciaryDisplayNameInput" class="govuk-input" type="text" formControlName="displayName" tabindex="2" />
</div>
<button
*ngIf="searchResult && form.valid"
*ngIf="judicialMember && form.valid"
class="govuk-button"
data-module="govuk-button"
(click)="confirmJudiciaryMemberWithDisplayName()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,51 @@ export class SearchForJudicialMemberComponent {
}
@Output() judicialMemberSelected = new EventEmitter<JudicialMemberDto>();

private judicialMember: JudicialMemberDto;
judicialMember: JudicialMemberDto;
private editMode = false;
constructor(private judiciaryService: JudicialService) {
this.createForm();
}

createForm() {
this.form = new FormGroup<SearchForJudicialMemberForm>({
judiciaryEmail: new FormControl<string>('', [Validators.required, Validators.minLength(3)]),
displayName: new FormControl<string>('')
});

this.form.controls.judiciaryEmail.valueChanges
.pipe(
tap(() => {
this.form.controls.displayName.removeValidators(Validators.required);
this.form.controls.displayName.updateValueAndValidity({ emitEvent: false });
}),
debounceTime(this.NotificationDelayTime)
)
.subscribe(newJudiciaryEmail => {
if (newJudiciaryEmail === '') {
this.showResult = false;
this.form.reset({
judiciaryEmail: '',
displayName: ''
});
}

if (this.form.controls.judiciaryEmail.invalid) {
return;
}
if (this.editMode) {
return;
}
this.searchForJudicialMember();
});
}

searchForJudicialMember() {
this.judiciaryService.getJudicialUsers(this.form.value.judiciaryEmail).subscribe(result => {
this.searchResult = result;
this.showResult = true;
this.form.controls.displayName.addValidators(Validators.required);
this.form.controls.displayName.updateValueAndValidity({ emitEvent: false });
});
}

Expand Down Expand Up @@ -74,39 +108,6 @@ export class SearchForJudicialMemberComponent {
});
this.form.controls.displayName.removeValidators(Validators.required);
}

createForm() {
this.form = new FormGroup<SearchForJudicialMemberForm>({
judiciaryEmail: new FormControl<string>('', [Validators.required, Validators.minLength(3)]),
displayName: new FormControl<string>('')
});

this.form.controls.judiciaryEmail.valueChanges
.pipe(
tap(() => {
this.form.controls.displayName.removeValidators(Validators.required);
this.form.controls.judiciaryEmail.updateValueAndValidity({ emitEvent: false });
}),
debounceTime(this.NotificationDelayTime)
)
.subscribe(newJudiciaryEmail => {
if (newJudiciaryEmail === '') {
this.showResult = false;
this.form.reset({
judiciaryEmail: '',
displayName: ''
});
}

if (this.form.controls.judiciaryEmail.invalid) {
return;
}
if (this.editMode) {
return;
}
this.searchForJudicialMember();
});
}
}

interface SearchForJudicialMemberForm {
Expand Down

0 comments on commit d8c15ef

Please sign in to comment.