Skip to content

Commit

Permalink
fix: console error after saving user roles selection
Browse files Browse the repository at this point in the history
  • Loading branch information
SGrueber authored and shauke committed Dec 22, 2023
1 parent 6263074 commit cbb77f5
Showing 1 changed file with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
FormGroup,
NG_VALUE_ACCESSOR,
} from '@angular/forms';
import { Observable, ReplaySubject } from 'rxjs';
import { Observable, ReplaySubject, noop } from 'rxjs';
import { first, map, shareReplay, startWith, switchMap, take, tap, withLatestFrom } from 'rxjs/operators';

import { OrganizationManagementFacade } from '../../facades/organization-management.facade';
Expand All @@ -31,6 +31,8 @@ export class UserRolesSelectionComponent implements ControlValueAccessor, OnInit
form$: Observable<FormGroup>;

private onTouched: Function;
private onChange: (roles: string[]) => void = noop;

private staticRoles$ = new ReplaySubject<string[]>(1);
private destroyRef = inject(DestroyRef);

Expand Down Expand Up @@ -59,6 +61,20 @@ export class UserRolesSelectionComponent implements ControlValueAccessor, OnInit
),
shareReplay(1)
);

this.form$
.pipe(
switchMap(form => form.valueChanges.pipe(startWith(form.value))),
withLatestFrom(this.staticRoles$),
map(([value, staticRoles]) => this.modelToRoles(value, staticRoles)),
tap(() => {
if (this.onTouched) {
this.onTouched();
}
}),
takeUntilDestroyed(this.destroyRef)
)
.subscribe(v => this.onChange(v));
}

private createFormControl(isStatic: boolean, disable: boolean) {
Expand Down Expand Up @@ -114,19 +130,7 @@ export class UserRolesSelectionComponent implements ControlValueAccessor, OnInit
}

registerOnChange(fn: (roles: string[]) => void): void {
this.form$
.pipe(
switchMap(form => form.valueChanges.pipe(startWith(form.value))),
withLatestFrom(this.staticRoles$),
map(([value, staticRoles]) => this.modelToRoles(value, staticRoles)),
tap(() => {
if (this.onTouched) {
this.onTouched();
}
}),
takeUntilDestroyed(this.destroyRef)
)
.subscribe(fn);
this.onChange = fn;
}

registerOnTouched(fn: Function): void {
Expand Down

0 comments on commit cbb77f5

Please sign in to comment.