import { Component , OnInit } from '@angular/core' ; import { UserService } from './shared/services/user.service' ; import { AuthenticationService } from './shared/services/authentication.service' ; import { ShowProgressService } from './shared/services/show-progress.service' ; import { Router } from '@angular/router' ; import { APIInfoService } from './shared/services/data.service' ; import { APIInfo } from './models/APIInfo' ; import { isNullOrUndefined } from 'util' ; import { RavenErrorHandler } from './shared/services/raven-error-handler.service' ; import { ThemeService } from './shared/services/theme.service' ; import { TranslateService } from '@ngx-translate/core' ; import { DomSanitizer , SafeStyle } from '@angular/platform-browser' ; import { TranslatableSnackBarService } from './shared/services/translatable-snack-bar.service' ; @ Component ( { selector : 'app-root' , templateUrl : './app.component.html' , styleUrls : [ './app.component.scss' ] } ) export class AppComponent implements OnInit { title = 'app works!' ; showProgressBar = false ; apiInfo : APIInfo ; avatarBackgroundImage : SafeStyle | undefined ; constructor ( private router , private authenticationService , public userService , private showProgress , private apiInfoService , private ravenErrorHandler , private snackBar , private themeService , public translate , private domSanitizer ) { translate . setDefaultLang ( 'en' ) ; showProgress . toggleSidenav$ . subscribe ( toggle => { this . toggleProgressBar ( ) ; } ) ; } ngOnInit ( ) { const lang = localStorage . getItem ( 'lang' ) || this . translate . getBrowserLang ( ) || this . translate . getDefaultLang ( ) ; this . translate . use ( lang ) ; this . authenticationService . reloadUser ( ) ; this . apiInfoService . readAPIInfo ( ) . then ( ( info ) => { this . ravenErrorHandler . setup ( info . sentryDsn ) ; this . apiInfo = info ; } ) . catch ( ( err ) => { this . snackBar . open ( 'home.backendError' , { err : err . error } , null ) ; } ) ; this . updateCurrentUser ( ) ; this . userService . data . subscribe ( actualProfilePicturePath => { if ( actualProfilePicturePath === undefined && this . userService . user . profile . picture ) { actualProfilePicturePath = this . userService . user . profile . picture . path ; } if ( actualProfilePicturePath === undefined || actualProfilePicturePath === '' ) { this . avatarBackgroundImage = undefined ; return ; } actualProfilePicturePath = '/api/' + actualProfilePicturePath ; this . avatarBackgroundImage = this . domSanitizer . bypassSecurityTrustStyle ( `url(${ actualProfilePicturePath })` ) ; } ) ; } updateCurrentUser ( ) { const storedUser = JSON . parse ( localStorage . getItem ( 'user' ) ) ; if ( storedUser ) { this . userService . setUser ( storedUser ) ; } } changeLanguage ( lang ) { localStorage . setItem ( 'lang' , lang ) ; this . translate . use ( lang ) ; } hasWarning ( ) { return ! isNullOrUndefined ( this . apiInfo ) && ! isNullOrUndefined ( this . apiInfo . nonProductionWarning ) ; } isLoggedIn ( ) { return this . authenticationService . isLoggedIn ; } logout ( ) { delete this . avatarBackgroundImage ; this . authenticationService . logout ( ) ; } toggleProgressBar ( ) { this . showProgressBar = ! this . showProgressBar ; } isAdmin ( ) { return this . userService . isAdmin ( ) ; } isStudent ( ) { return this . userService . isStudent ( ) ; } specialContainerStyle ( ) { const routeTest = /^(\/|\/login|\/register|\/reset|\/activation-resend)$/ . test ( this . router . url ) ; return ( routeTest && ! this . isLoggedIn ( ) ) ? 'special-style' : '' ; } contentStyle ( ) { let style = 'app-content' ; const routeTest = /^\/file/ . test ( this . router . url ) ; if ( ! routeTest ) { style += ' app-content-padding' ; } return style ; } }