All files / content content.component.ts

81.81% Statements 9/11
0% Branches 0/4
75% Functions 3/4
81.81% Lines 9/11

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                              1x 3x   3x   3x       3x   3x                   3x       1x       1x      
import { ChangeDetectionStrategy, Component, DestroyRef, inject, ViewChild } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { MatSidenavContent } from '@angular/material/sidenav';
import { NavigationEnd, Router } from '@angular/router';
import { ISidebarState, sidebarAction, sidebarSelector } from '@app/client-store-sidebar';
import { Store } from '@ngrx/store';
import { tap } from 'rxjs/operators';
 
@Component({
  selector: 'app-content',
  templateUrl: './content.component.html',
  styleUrls: ['./content.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: false,
})
export class AppContentComponent {
  private readonly destroyRef = inject(DestroyRef);
 
  private readonly store = inject(Store<ISidebarState>);
 
  private readonly router = inject(Router);
 
  @ViewChild('content') public readonly content?: MatSidenavContent;
 
  public readonly sidebarOpen$ = this.store.select(sidebarSelector.sidebarOpen);
 
  public readonly routerEvents$ = this.router.events.pipe(
    takeUntilDestroyed(this.destroyRef),
    tap(event => {
      if (event instanceof NavigationEnd && this.content instanceof MatSidenavContent) {
        this.content.scrollTo({ top: 0 });
      }
    }),
  );
 
  constructor() {
    void this.routerEvents$.subscribe();
  }
 
  public sidebarCloseHandler(): void {
    this.store.dispatch(sidebarAction.close({ payload: { navigate: true } }));
  }
 
  public sidebarOpenHandler(): void {
    this.store.dispatch(sidebarAction.open({ payload: { navigate: true } }));
  }
}