Skip to content
Snippets Groups Projects
app.component.ts 2.26 KiB
Newer Older
import { Component, OnInit } from '@angular/core';
import {
  GuardsCheckStart,
  NavigationCancel,
  NavigationEnd,
  NavigationError,
  NavigationStart,
  Router,
} from '@angular/router';
import { ProfileService } from './profile/services/profile.service';
import { AuthService } from './services/auth.service';
import { RouterListenerService } from './services/routerListener.service';
Hugo SUBTIL's avatar
Hugo SUBTIL committed
import { UpdateService } from './services/update.service';
import { PrintService } from './shared/service/print.service';
Hugo SUBTIL's avatar
Hugo SUBTIL committed

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
Hugo SUBTIL's avatar
Hugo SUBTIL committed
  styleUrls: ['./app.component.scss'],
Hugo SUBTIL's avatar
Hugo SUBTIL committed
})
export class AppComponent implements OnInit {
  public title = 'resin';
  public loading = true;
Hugo SUBTIL's avatar
Hugo SUBTIL committed

  constructor(
    public printService: PrintService,
    private authService: AuthService,
    private profilService: ProfileService,
    private routerListener: RouterListenerService,
Hugo SUBTIL's avatar
Hugo SUBTIL committed
    private updateService: UpdateService,
  ) {
    if (this.authService.isLoggedIn()) {
      this.profilService.getProfile();
    }
    this.setHeightApp();
    window.addEventListener('resize', () => {
      this.setHeightApp();
    });
    this.routerListener.loadRouting();
Hugo SUBTIL's avatar
Hugo SUBTIL committed
    // handle pwa update
    this.updateService.subscribeUpdate();

    this.router.events.subscribe((event) => {
      switch (true) {
        case event instanceof NavigationStart: {
          this.loading = true;
          break;
        }

        case event instanceof GuardsCheckStart:
        case event instanceof NavigationEnd:
        case event instanceof NavigationCancel: {
          this.loading = false;
          break;
        }
        case event instanceof NavigationError: {
          setTimeout(() => {
            this.loading = false;
          }, 100);
          break;
        }
        default: {
          break;
        }
      }
    });
  ngOnInit(): void {
    /**
     * Reset scroll to top for article reading
     */
    this.router.events.subscribe((evt) => {
      if (!(evt instanceof NavigationEnd)) {
        return;
      }
      document.getElementsByClassName('app-body')[0].scrollTo(0, 0);
    });
  }

  private setHeightApp(): void {
    const vh = window.innerHeight * 0.01;
    document.documentElement.style.setProperty('--vh', `${vh}px`);
Hugo SUBTIL's avatar
Hugo SUBTIL committed
}