Created
July 21, 2024 19:23
-
-
Save rakia/671ddd9461585f77bdcb2899f2992bb7 to your computer and use it in GitHub Desktop.
Angular: Simplified logic to show real-time feedback to users when they type a name that already exists in the system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nameAlreadyExists: boolean = false; | |
private notifyParentComponentWhenNameChanges(): void { | |
this.form | |
?.get('name') | |
?.valueChanges.pipe( | |
takeUntilDestroyed(this.destroyRef), | |
debounceTime(300), // debounce user input | |
distinctUntilChanged() | |
) | |
.subscribe((name) => { | |
if (name) { | |
this.nameAlreadyExists = this.storeService.checkIfNameExists(name); | |
} | |
}); | |
} | |
onClickCancel(): void { | |
if (this.form?.touched && this.form?.dirty) { | |
const dialogData: DialogData = { | |
hasActions: true, | |
mode: 'confirmAction', | |
text: '', | |
title: 'shared.MESSAGES.CONFIRM_CANCEL.TITLE', | |
}; | |
const dialogRef = this.dialog.open(ConfirmDialogComponent, { | |
disableClose: true, | |
data: dialogData, | |
}); | |
dialogRef.afterClosed().subscribe((data: boolean) => { | |
if (data) { | |
this.updateForm(); | |
this.disableEditMode(); | |
this.nameAlreadyExists = false; | |
} | |
}); | |
} else { | |
this.disableEditMode(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment