All files / app/component/user/user-list user-list.component.ts

50% Statements 17/34
41.66% Branches 10/24
33.33% Functions 4/12
48% Lines 12/25

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 62 63 64 65 66 67 68 69 70 71 722x                 2x 2x 2x                 6x         2x       2x     2x 2x               8x         1x 1x                                                
import { Component, Input, QueryList, ViewChildren } from '@angular/core';
import { Router } from '@angular/router';
import { find } from 'lodash-es';
import { catchError, of } from 'rxjs';
import { HasChanges } from '../../../guard/pending-changes.guard';
import { Page } from '../../../model/page';
import { Profile } from '../../../model/profile';
import { User } from '../../../model/user';
import { ProfileService } from '../../../service/api/profile.service';
import { LoadingComponent } from '../../loading/loading.component';
import { PageControlsComponent } from '../../page-controls/page-controls.component';
import { UserComponent } from '../user.component';
 
@Component({
  selector: 'app-user-list',
  templateUrl: './user-list.component.html',
  styleUrls: ['./user-list.component.scss'],
  host: { 'class': 'user-list' },
  imports: [UserComponent, LoadingComponent, PageControlsComponent]
})
export class UserListComponent implements HasChanges {
 
  @Input()
  scim?: Page<Profile>;
 
  @ViewChildren(UserComponent)
  list?: QueryList<UserComponent>;
 
  private _page?: Page<User>;
  private cache: Map<string, Profile | undefined> = new Map();
 
  constructor(
    private router: Router,
    private profiles: ProfileService,
  ) { }
 
  saveChanges() {
    return !this.list?.find(u => !u.saveChanges());
  }
 
  get page() {
    return this._page;
  }
 
  @Input()
  set page(value: Page<User> | undefined) {
    this.cache.clear();
    this._page = value;
  }
 
  hasUser(tag: string) {
    return !!find(this.page?.content, p => p.tag === tag);
  }
 
  getProfile(user: User) {
    const tag = user.tag + user.origin;
    if (!this._page) this._page = {} as any;
    if (!this.cache.has(tag)) {
      const profile = find(this.scim?.content, p => p.tag === tag);
      if (profile) {
        this.cache.set(tag, profile);
      } else {
        this.cache.set(tag, undefined);
        this.profiles.getProfile(tag).pipe(
          catchError(e => of(undefined))
        ).subscribe(p => this.cache.set(tag, p as Profile));
      }
    }
    return this.cache.get(tag) || undefined;
  }
}