2021-01-27 17:28:11 +01:00
import { SettingsService , SETTINGS_KEYS } from './services/settings.service' ;
2020-11-07 12:05:15 +01:00
import { Component , OnDestroy , OnInit } from '@angular/core' ;
import { Router } from '@angular/router' ;
import { Subscription } from 'rxjs' ;
import { ConsumerStatusService } from './services/consumer-status.service' ;
2021-01-27 17:28:11 +01:00
import { ToastService } from './services/toast.service' ;
2020-10-27 01:10:18 +01:00
@Component ( {
selector : 'app-root' ,
templateUrl : './app.component.html' ,
2020-11-22 14:43:59 +01:00
styleUrls : [ './app.component.scss' ]
2020-10-27 01:10:18 +01:00
} )
2020-11-07 12:05:15 +01:00
export class AppComponent implements OnInit , OnDestroy {
2021-01-27 17:28:11 +01:00
newDocumentSubscription : Subscription ;
2020-11-07 12:05:15 +01:00
successSubscription : Subscription ;
failedSubscription : Subscription ;
2021-01-04 22:45:56 +01:00
constructor ( private settings : SettingsService , private consumerStatusService : ConsumerStatusService , private toastService : ToastService , private router : Router ) {
2021-01-01 22:38:26 +01:00
let anyWindow = ( window as any )
2021-05-11 17:25:50 +09:00
anyWindow . pdfWorkerSrc = 'assets/js/pdf.worker.min.js' ;
2021-01-01 22:38:26 +01:00
this . settings . updateDarkModeSettings ( )
2020-11-07 12:05:15 +01:00
}
ngOnDestroy ( ) : void {
this . consumerStatusService . disconnect ( )
2021-01-27 17:28:11 +01:00
if ( this . successSubscription ) {
this . successSubscription . unsubscribe ( )
}
if ( this . failedSubscription ) {
this . failedSubscription . unsubscribe ( )
}
if ( this . newDocumentSubscription ) {
this . newDocumentSubscription . unsubscribe ( )
}
}
private showNotification ( key ) {
if ( this . router . url == '/dashboard' && this . settings . get ( SETTINGS_KEYS . NOTIFICATIONS_CONSUMER_SUPPRESS_ON_DASHBOARD ) ) {
return false
}
return this . settings . get ( key )
2020-10-27 01:10:18 +01:00
}
2020-11-07 12:05:15 +01:00
ngOnInit ( ) : void {
this . consumerStatusService . connect ( )
2021-06-13 10:48:46 -04:00
2020-11-07 12:05:15 +01:00
this . successSubscription = this . consumerStatusService . onDocumentConsumptionFinished ( ) . subscribe ( status = > {
2021-01-27 17:28:11 +01:00
if ( this . showNotification ( SETTINGS_KEYS . NOTIFICATIONS_CONSUMER_SUCCESS ) ) {
this . toastService . show ( { title : $localize ` Document added ` , delay : 10000 , content : $localize ` Document ${ status . filename } was added to paperless. ` , actionName : $localize ` Open document ` , action : ( ) = > {
this . router . navigate ( [ 'documents' , status . documentId ] )
} } )
}
2020-11-07 12:05:15 +01:00
} )
this . failedSubscription = this . consumerStatusService . onDocumentConsumptionFailed ( ) . subscribe ( status = > {
2021-01-27 17:28:11 +01:00
if ( this . showNotification ( SETTINGS_KEYS . NOTIFICATIONS_CONSUMER_FAILED ) ) {
this . toastService . showError ( $localize ` Could not add ${ status . filename } \ : ${ status . message } ` )
}
2020-11-07 12:05:15 +01:00
} )
2021-01-27 17:28:11 +01:00
this . newDocumentSubscription = this . consumerStatusService . onDocumentDetected ( ) . subscribe ( status = > {
if ( this . showNotification ( SETTINGS_KEYS . NOTIFICATIONS_CONSUMER_NEW_DOCUMENT ) ) {
this . toastService . show ( { title : $localize ` New document detected ` , delay : 5000 , content : $localize ` Document ${ status . filename } is being processed by paperless. ` } )
}
} )
2020-11-07 12:05:15 +01:00
}
2020-10-27 01:10:18 +01:00
}