import { Injectable } from '@angular/core' ; import { Resolve , ActivatedRouteSnapshot , RouterStateSnapshot , Router } from '@angular/router' ; import { tap , catchError } from 'rxjs/operators' ; import { of } from 'rxjs' ; import { Lab } from './models/lab' ; import { LabStorageService } from './lab-storage.service' ; import { SnackbarService } from './snackbar.service' ; import { BLANK_LAB_TPL_ID , DEFAULT_LAB_TPL_ID } from './lab-template.service' ; @ Injectable ( ) export class LabResolver implements Resolve < Lab > { constructor ( private labStorageService , private router , private snackBar ) { } resolve ( route ) { if ( route . paramMap . get ( 'id' ) ) { return this . labStorageService . getLab ( route . paramMap . get ( 'id' ) ) . pipe ( tap ( lab => { if ( ! lab || lab . hidden ) { this . snackBar . notifyLabDoesntExist ( ) ; this . router . navigate ( [ '/editor' ] ) ; } } ) , catchError ( error => { this . snackBar . notifyLabDoesntExist ( ) ; this . router . navigate ( [ '/editor' ] ) ; return of ( null ) ; } ) ) ; } return route . queryParamMap . get ( 'tpl' ) === BLANK_LAB_TPL_ID ? this . labStorageService . createLab ( ) : this . labStorageService . createLabFromTemplate ( route . queryParamMap . get ( 'tpl' ) || DEFAULT_LAB_TPL_ID ) ; } }