import { AlfrescoApiService , AppConfigService , AuthenticationService , FileUploadErrorEvent , PageTitleService , UploadService , SharedLinksApiService } from '@alfresco/adf-core' ; import { GroupService } from '@alfresco/adf-content-services' ; import { Component , OnInit , OnDestroy } from '@angular/core' ; import { ActivatedRoute , Router , ActivationEnd } from '@angular/router' ; import { Store } from '@ngrx/store' ; import { AppStore , AppState , SetCurrentUrlAction , SetInitialStateAction , SetUserProfileAction , SnackbarErrorAction , CloseModalDialogsAction , SetRepositoryInfoAction } from '@alfresco/aca-shared/store' ; import { filter , takeUntil } from 'rxjs/operators' ; import { RouterExtensionService , AppService , ContentApiService } from '@alfresco/aca-shared' ; import { DiscoveryEntry , GroupEntry , Group } from '@alfresco/js-api' ; import { Subject } from 'rxjs' ; import { INITIAL_APP_STATE } from './store/initial-state' ; @ Component ( { selector : 'app-root' , templateUrl : './app.component.html' , styleUrls : [ './app.component.scss' ] } ) export class AppComponent implements OnInit , OnDestroy { onDestroy$ : Subject < boolean > = new Subject < boolean > ( ) ; pageHeading = '' ; constructor ( private route , private router , private pageTitle , private store , private config , private alfrescoApiService , private authenticationService , private uploadService , private routerExtensionService , private contentApi , private appService , private sharedLinksApiService , private groupService ) { } ngOnInit ( ) { this . alfrescoApiService . getInstance ( ) . on ( 'error' , ( error ) => { if ( error . status === 401 && ! this . alfrescoApiService . isExcludedErrorListener ( error ?. response ?. req ?. url ) ) { if ( ! this . authenticationService . isLoggedIn ( ) ) { this . store . dispatch ( new CloseModalDialogsAction ( ) ) ; let redirectUrl = this . route . snapshot . queryParams [ 'redirectUrl' ] ; if ( ! redirectUrl ) { redirectUrl = this . router . url ; } this . router . navigate ( [ '/login' ] , { queryParams : { redirectUrl : redirectUrl } } ) ; } } } ) ; this . loadAppSettings ( ) ; const { router , pageTitle } = this ; this . router . events . pipe ( filter ( ( event ) => event instanceof ActivationEnd && event . snapshot . children . length === 0 ) ) . subscribe ( ( event ) => { const snapshot = event . snapshot || { } ; const data = snapshot . data || { } ; this . pageHeading = data . title || '' ; pageTitle . setTitle ( data . title || '' ) ; this . store . dispatch ( new SetCurrentUrlAction ( router . url ) ) ; } ) ; this . routerExtensionService . mapExtensionRoutes ( ) ; this . uploadService . fileUploadError . subscribe ( ( error ) => this . onFileUploadedError ( error ) ) ; this . sharedLinksApiService . error . pipe ( takeUntil ( this . onDestroy$ ) ) . subscribe ( ( err ) => { this . store . dispatch ( new SnackbarErrorAction ( err . message ) ) ; } ) ; this . appService . ready$ . pipe ( takeUntil ( this . onDestroy$ ) ) . subscribe ( ( isReady ) => { if ( isReady ) { this . loadRepositoryStatus ( ) ; this . loadUserProfile ( ) ; } } ) ; } ngOnDestroy ( ) { this . onDestroy$ . next ( true ) ; this . onDestroy$ . complete ( ) ; } private loadRepositoryStatus ( ) { this . contentApi . getRepositoryInformation ( ) . subscribe ( ( response ) => { this . store . dispatch ( new SetRepositoryInfoAction ( response . entry . repository ) ) ; } ) ; } private async loadUserProfile ( ) { const groupsEntries = await this . groupService . listAllGroupMembershipsForPerson ( '-me-' , { maxItems : 250 } ) ; const groups = [ ] ; if ( groupsEntries ) { groups . push ( ... groupsEntries . map ( ( obj ) => obj . entry ) ) ; } this . contentApi . getPerson ( '-me-' ) . subscribe ( ( person ) => { this . store . dispatch ( new SetUserProfileAction ( { person : person . entry , groups } ) ) ; } ) ; } loadAppSettings ( ) { let baseShareUrl = this . config . get < string > ( 'baseShareUrl' ) ; if ( ! baseShareUrl . endsWith ( '/' ) ) { baseShareUrl += '/' ; } const state = { ... INITIAL_APP_STATE , appName : this . config . get < string > ( 'application.name' ) , headerColor : this . config . get < string > ( 'headerColor' ) , headerTextColor : this . config . get < string > ( 'headerTextColor' , '#000000' ) , logoPath : this . config . get < string > ( 'application.logo' ) , headerImagePath : this . config . get < string > ( 'application.headerImagePath' ) , sharedUrl : baseShareUrl } ; this . store . dispatch ( new SetInitialStateAction ( state ) ) ; } onFileUploadedError ( error ) { let message = 'APP.MESSAGES.UPLOAD.ERROR.GENERIC' ; if ( error . error . status === 403 ) { message = 'APP.MESSAGES.UPLOAD.ERROR.403' ; } if ( error . error . status === 404 ) { message = 'APP.MESSAGES.UPLOAD.ERROR.404' ; } if ( error . error . status === 409 ) { message = 'APP.MESSAGES.UPLOAD.ERROR.CONFLICT' ; } if ( error . error . status === 500 ) { message = 'APP.MESSAGES.UPLOAD.ERROR.500' ; } if ( error . error . status === 504 ) { message = 'APP.MESSAGES.UPLOAD.ERROR.504' ; } this . store . dispatch ( new SnackbarErrorAction ( message ) ) ; } }