import { Component, ComponentRef, EmbeddedViewRef, OnInit, Type, ViewContainerRef } from '@angular/core'; import { Router } from '@angular/router'; import { DialogService } from 'primeng/dynamicdialog'; import {Toast} from "primeng/toast"; import { ExitComponentComponent } from './components/exit-component/exit-component.component'; import { CookiesService } from './services/cookies.service'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'], providers: [DialogService], }) export class AppComponent implements OnInit { public opened: boolean = false; public isAuth!: boolean; private messageComponent!: ComponentRef; title = 'selfdelivery-admin-panel'; constructor( private cookiesService: CookiesService, private viewContainerRef: ViewContainerRef, public dialogService: DialogService, private router: Router, ) {} ngOnInit(): void { this.messageComponent = this.appendComponent(Toast); const token = decodeURI(this.cookiesService.getItem('token') ?? '') this.isAuth = token.length ? true : false; this.router.navigate(['/clients']) } appendComponent(component: Type): ComponentRef { const componentRef = this.viewContainerRef.createComponent(component); const domElem = (componentRef.hostView as EmbeddedViewRef) .rootNodes[0] as HTMLElement; document.body.appendChild(domElem); return componentRef; } openSidenav() { this.opened = true } setIsAuth() { const token = decodeURI(this.cookiesService.getItem('token') ?? '') this.isAuth = token.length ? true : false; } signOut() { const ref = this.dialogService.open(ExitComponentComponent, { width: 'auto', style: { 'max-width': '90vw', 'max-height': '90vh', }, contentStyle: { 'max-height': '90vh', height: 'auto', 'max-width': '90vw', overflow: 'auto', 'border-radius': '7px', 'padding-top': '20px' }, baseZIndex: 10000, autoZIndex: true, dismissableMask: true, closeOnEscape: true, showHeader: false, }); ref.onClose.subscribe( result => { if (result) { this.cookiesService.deleteCookie('token') this.setIsAuth() } } ); } }