Skip to content

Instantly share code, notes, and snippets.

View rakia's full-sized avatar

Rakia Ben Sassi rakia

View GitHub Profile
@rakia
rakia / esc-field-name-already-exists.component.ts
Created July 21, 2024 19:23
Angular: Simplified logic to show real-time feedback to users when they type a name that already exists in the system
nameAlreadyExists: boolean = false;
private notifyParentComponentWhenNameChanges(): void {
this.form
?.get('name')
?.valueChanges.pipe(
takeUntilDestroyed(this.destroyRef),
debounceTime(300), // debounce user input
distinctUntilChanged()
)
@rakia
rakia / esc-field-name-already-exists.component.ts
Last active July 21, 2024 19:21
Angular: Simplified logic to show real-time feedback to users when they type a name that already exists in the system
@Component({
selector: 'app-ecs-field',
templateUrl: './ecs-field.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class EcsFieldComponent implements OnInit, OnChanges {
private storeService = inject(EcsFieldsStoreService);
nameAlreadyExists: boolean = false;
@rakia
rakia / esc-field-name-already-exists.component.ts
Created July 21, 2024 18:56
Angular: Logic to show real-time feedback to users when they type a name that already exists in the system
@Component({
selector: 'app-ecs-field',
templateUrl: './ecs-field.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class EcsFieldComponent {
formBuilder = inject(FormBuilder);
@Input() entity: EcsField;
@Input() nameAlreadyExists: boolean | null = false;
@rakia
rakia / esc-fieldsets-name-already-exists.component.ts
Created July 21, 2024 18:52
Angular: Logic to show real-time feedback to users when they type a name that already exists in the system
@Component({
selector: 'app-ecs-fieldset',
templateUrl: './ecs-fieldset.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class EcsFieldsetComponent {
formBuilder = inject(FormBuilder);
@Input() entity: EcsFieldset;
@Input() nameAlreadyExists: boolean | null = false;
@rakia
rakia / esc-fieldset-name-already-exists.component.html
Created July 21, 2024 18:49
Angular: Logic to show real-time feedback to users when they type a name that already exists in the system
<ng-container *ngIf="isCreateMode">
<div class="mt-0">
<app-create-custom-field
[ecsFieldset]="entity.id!"
[nameAlreadyExists]="nameAlreadyExists"
(checkIfNameExists)="checkIfNameExists.emit($event)"
(cancelEdit)="cancelEdit.emit()"
(save)="createCustomField.emit($event)"
>
</app-create-custom-field>
@rakia
rakia / esc-fieldsets-with-details-name-already-exists.component.html
Last active July 21, 2024 18:48
Angular: Logic to show real-time feedback to users when they type a name that already exists in the system
<app-ecs-fieldset
[entity]="ecsFieldset"
[fields]="(ecsFields$ | async) || []"
[nameAlreadyExists]="nameAlreadyExists$ | async"
(checkIfNameExists)="checkIfNameExists($event)"
(cancelEdit)="onCancelEdit()"
>
</app-ecs-fieldset>
@rakia
rakia / escfield-name-already-exists.component.ts
Created July 21, 2024 18:44
Angular: Logic to show real-time feedback to users when they type a name that already exists in the system
@Component({
selector: 'app-create-custom-field',
templateUrl: './create-custom-field.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CreateCustomFieldComponent implements OnInit {
private readonly destroyRef = inject(DestroyRef);
@Input() nameAlreadyExists: boolean | null = false;
@Output() checkIfNameExists = new EventEmitter<string>();
@rakia
rakia / name-already-exists.component.ts
Created July 21, 2024 18:40
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);
@rakia
rakia / show-warning.html
Created July 21, 2024 18:36
Show real-time feedback to users when they type a name that already exists in the system
<!-- Show warning: Name already exists -->
<ng-container *ngIf="nameAlreadyExists">
<div class="mb-4 text-red-600">
{{ "WARNING.DUPLICATE_NAME" | transloco }}
</div>
</ng-container>
@rakia
rakia / redirect-to.ts
Created May 23, 2024 07:57
Angular Routing: redirectTo as function
const routes: Routes = [
{ path: "dashboard", component: DashboardComponent },
{
path: "profiles",
redirectTo: ({ queryParams }) => {
const errorHandler = inject(ErrorHandler);
const userId = queryParams['userId'];
if (userId) {
// return the appropriate redirect path based on the userId query-parameter
return `/profiles/${userId}`;