import { Injectable } from '@angular/core'; import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot, Router, } from '@angular/router'; import { Observable, of } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { EntryService } from '../services/entry.service'; @Injectable({ providedIn: 'root', }) export class EntryResolverService implements Resolve { constructor(private entryService: EntryService, private router: Router) {} resolve( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable { const id = route.params['id']; // TODO: redirect to 404 if (!id) { this.router.navigate(['/library']); return of(null); } return this.entryService.getEntryById(id).pipe( catchError((error) => { console.error(`Fetching Entry with id ${id} failed`, error); // TODO: redirect to 404 this.router.navigate(['/library']); return of(null); // Return null observable or an observable with default data }) ); } }