Skip to content

Instantly share code, notes, and snippets.

@rakia
Created July 21, 2024 18:40
Show Gist options
  • Save rakia/63a3776c47e241d40f93594db9b6f58c to your computer and use it in GitHub Desktop.
Save rakia/63a3776c47e241d40f93594db9b6f58c to your computer and use it in GitHub Desktop.
Angular: Logic to show real-time feedback to users when they type a name that already exists in the system
@Component({
selector: 'app-ecs-fieldsets-with-details',
templateUrl: './ecs-fieldsets-with-details.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class EcsFieldsetsWithDetailsComponent implements OnChanges {
private storeService = inject(EcsFieldsStoreService);
nameAlreadyExists$ = new BehaviorSubject<boolean>(false);
/**
* This method calls the store service and sets the boolean nameAlreadyExists depending on the result.
* @param name
*/
checkIfNameExists(name: string): void {
const res: boolean = this.storeService.checkIfNameExists(name);
this.nameAlreadyExists$.next(res);
}
onCancelEdit(): void {
this.nameAlreadyExists$.next(false);
}
ngOnChanges(changes: SimpleChanges): void {
if (changes['ecsFieldsets']?.currentValue && !changes['ecsFieldsets']?.firstChange) {
this.nameAlreadyExists$.next(false);
}
}
updateEcsField(updatedEntity: UpdatedEntity<UpdatableEcsFieldAttributes>): void {
this.storeService.updateEcsField(updatedEntity).then(() => {
this.nameAlreadyExists$.next(false);
});
}
createCustomField(entity: EcsField): void {
this.storeService.createCustomField(entity).then(() => {
this.nameAlreadyExists$.next(false);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment