Created
July 21, 2024 18:40
-
-
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
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
@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