import { AfterViewInit , Component , OnInit , ViewChild } from '@angular/core' ; import { MatPaginator } from '@angular/material/paginator' ; import { MatSort } from '@angular/material/sort' ; import { MatTableDataSource } from '@angular/material/table' ; import { Router } from '@angular/router' ; import { AlertService } from 'carey-alert' ; import { Email } from '../models/email' ; import { EmailService } from '../service/email.service' ; import { DateService } from '../../../../services/date.service' ; import { UiUtil } from '../../../../util/ui-util' ; import { HttpErrorResponse } from '@angular/common/http' ; import { GoogleApiError } from '../models/google-api-error' ; @ Component ( { selector : 'app-inbox' , templateUrl : './inbox.component.html' , styleUrls : [ './inbox.component.css' ] } ) export class InboxComponent implements OnInit , AfterViewInit { displayedColumns : string [ ] = [ 'action' , 'from' , 'subject' , 'date' ] ; dataSource : MatTableDataSource < Email > ; dataLoading : boolean = true ; @ ViewChild ( MatPaginator ) paginator : MatPaginator ; @ ViewChild ( MatSort ) sort : MatSort ; constructor ( private alertService , private router , private emailService , private dateService , private uiUtil ) { } ngOnInit ( ) { this . loadInbox ( ) ; this . uiUtil . scrollToTop ( ) ; } ngAfterViewInit ( ) { if ( this . dataSource ) { if ( ! this . dataSource . paginator ) { this . dataSource . paginator = this . paginator ; } if ( ! this . dataSource . sort ) { this . dataSource . sort = this . sort ; } } } private createFilter ( ) { let filterFunction = function ( email , filter ) : boolean { let searchTerms = JSON . parse ( filter ) ; return ( email . subject != null ) ; } ; return filterFunction ; } private loadInbox ( refresh ? ) { if ( refresh ) this . dataLoading = true ; this . emailService . fetchInbox ( refresh ) . subscribe ( ( emails ) => this . handleInboxRetrieval ( emails ) , ( err ) => this . handleInboxError ( err ) ) ; } private handleInboxError ( err ) { if ( err instanceof HttpErrorResponse ) { let httpError = < HttpErrorResponse > err ; let googleApiError = httpError . error ; if ( googleApiError . error == 'invalid_grant' ) { let route = '/user/email/email-choice' ; this . router . navigate ( [ route ] ) ; } else { this . alertService . error ( "Problem loading emails!" ) ; } } else { this . alertService . error ( "Problem loading emails!" ) ; } this . dataLoading = false ; } private handleInboxRetrieval ( emails ) { this . dataSource = new MatTableDataSource ( emails ) ; this . dataSource . paginator = this . paginator ; this . dataSource . sort = this . sort ; this . dataLoading = false ; } getSubjectSnippetDisplay ( email ) { let subject = email . subject ; let snippet = email . snippet ; let display = `${ subject } - ${ snippet }` ; return display ; } viewMessage ( id ) { let route = '/user/email/message' ; this . router . navigate ( [ route ] , { queryParams : { id : id } } ) ; } composeEmail ( ) { let route = '/user/email/compose-email' ; this . router . navigate ( [ route ] ) ; } }