All files / projects/organization-management/src/app/pages/user-edit-roles user-edit-roles-page.component.ts

91.3% Statements 21/23
71.42% Branches 10/14
71.42% Functions 5/7
91.3% Lines 21/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 621x 1x 1x 1x 1x   1x   1x   1x               1x               1x     1x 1x 1x       1x 1x 1x   1x 1x 1x     1x     1x                            
import { ChangeDetectionStrategy, Component, DestroyRef, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Observable, combineLatest, of } from 'rxjs';
import { map, shareReplay, switchMap, take } from 'rxjs/operators';
 
import { AccountFacade } from 'ish-core/facades/account.facade';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { whenTruthy } from 'ish-core/utils/operators';
 
import { OrganizationManagementFacade } from '../../facades/organization-management.facade';
import { B2bUser } from '../../models/b2b-user/b2b-user.model';
 
@Component({
  selector: 'ish-user-edit-roles-page',
  templateUrl: './user-edit-roles-page.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class UserEditRolesPageComponent implements OnInit {
  selectedUser$: Observable<B2bUser>;
  loading$: Observable<boolean>;
  error$: Observable<HttpError>;
 
  staticRoles$: Observable<string[]>;
 
  form$: Observable<FormGroup>;
  private destroyRef = inject(DestroyRef);
 
  constructor(
    private fb: FormBuilder,
    private organizationManagementFacade: OrganizationManagementFacade,
    private accountFacade: AccountFacade
  ) {}
 
  ngOnInit() {
    this.loading$ = this.organizationManagementFacade.usersLoading$;
    this.error$ = this.organizationManagementFacade.usersError$;
    this.selectedUser$ = this.organizationManagementFacade.selectedUser$;
 
    this.staticRoles$ = combineLatest([this.selectedUser$, this.accountFacade.user$]).pipe(
      map(([selectedUser, currentUser]) => selectedUser?.login === currentUser?.login),
      switchMap(isCurrentUser => (isCurrentUser ? of(['APP_B2B_ACCOUNT_OWNER']) : of([])))
    );
 
    this.form$ = this.selectedUser$.pipe(
      whenTruthy(),
      map(user =>
        this.fb.group({
          roleIDs: [user.roleIDs],
        })
      ),
      shareReplay(1)
    );
  }
 
  submitForm() {
    this.form$.pipe(take(1), takeUntilDestroyed(this.destroyRef)).subscribe(form => {
      this.organizationManagementFacade.setSelectedUserRoles(form.value.roleIDs);
    });
  }
}