All files / app/search/presenter/pages/github-search/components/molecules/results-table results-table.component.ts

100% Statements 17/17
100% Branches 0/0
100% Functions 7/7
100% Lines 16/16

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                            1x           13x         13x 13x 13x       13x   13x 13x       13x 30x     13x 19x         13x       13x 13x   13x      
import { AfterViewInit, Component, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTable } from '@angular/material/table';
import { firstValueFrom, map, Observable, timer } from 'rxjs';
import { GithubUser } from 'src/app/search/domain/entities/github-user';
import { GithubSearchController } from '../../../github-search.controller';
import { ResultsTableDataSource } from './results-table-datasource';
 
@Component({
  selector: 'app-results-table',
  templateUrl: './results-table.component.html',
  styleUrls: ['./results-table.component.scss'],
})
export class ResultsTableComponent implements AfterViewInit {
  @ViewChild(MatPaginator) paginator!: MatPaginator;
  @ViewChild(MatSort) sort!: MatSort;
  @ViewChild(MatTable) table!: MatTable<GithubUser>;
  dataSource: ResultsTableDataSource;
 
  displayedColumns = ['avatarImg', 'login', 'type', 'htmlUrl'];
 
  totalCount$!: Observable<number>;
  pageIndex$!: Observable<number>;
 
  constructor(public controller: GithubSearchController) {
    this.dataSource = new ResultsTableDataSource(controller);
    this.createHelperObservables();
  }
 
  async ngAfterViewInit(): Promise<void> {
    await firstValueFrom(timer(100));
 
    this.setDefaultSorting();
    this.connnectDataSource();
  }
 
  createHelperObservables() {
    this.totalCount$ = this.controller.dataState$.pipe(
      map((dataState) => dataState.data.totalCount)
    );
 
    this.pageIndex$ = this.controller.viewState$.pipe(
      map((viewState) => viewState.page)
    );
  }
 
  setDefaultSorting() {
    this.sort.sort({ id: 'login', start: 'asc', disableClear: false });
  }
 
  connnectDataSource() {
    this.dataSource.sort = this.sort;
    this.dataSource.paginator = this.paginator;
 
    this.table.dataSource = this.dataSource;
  }
}