40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
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<any> {
|
|
constructor(private entryService: EntryService, private router: Router) {}
|
|
|
|
resolve(
|
|
route: ActivatedRouteSnapshot,
|
|
state: RouterStateSnapshot
|
|
): Observable<any> {
|
|
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
|
|
})
|
|
);
|
|
}
|
|
}
|