import { Injectable } from '@angular/core' ; import { MatSnackBar } from '@angular/material/snack-bar' ; import { ActivatedRouteSnapshot , Resolve , Router } from '@angular/router' ; import { Doc } from '../core/Doc' ; import { LocalStorageService } from '../core/storage/local/local-storage.service' ; @ Injectable ( ) export class HistoryResolverService implements Resolve < Doc > { constructor ( private router , private storage , private snackBar ) { } async resolve ( route ) { try { const key = route . params [ 'key' ] ; return this . storage . fetchDoc ( key ) . then ( ( doc ) => { if ( doc ) { return doc ; } else { throw new Error ( 'Document could not be found' ) ; } } ) ; } catch ( err ) { log . warn ( 'Failed to open the document history.' , err . message ) ; this . snackBar . open ( `Could not resolve the URL: ${ err . message }` , 'close' , { duration : 3000 } ) ; this . router . navigateByUrl ( '/docs' , { skipLocationChange : false } ) ; return undefined ; } } }