create entry resolver
This commit is contained in:
1 parent
24c2537fae
commit
281daf06e2
1 file changed
+39
@@ -0,0 +1,39 @@
|
||||
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
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user