import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot, } from '@angular/router'; import { catchError, Observable, throwError } from 'rxjs'; import { Notice } from '../models/notice'; import { NoticePagination } from '../models/notice-pagination'; import { NoticeService } from '../services/notice.service'; @Injectable({ providedIn: 'root', }) export class NoticeResolver implements Resolve { /** * Constructor */ constructor(private _noticeService: NoticeService, private _router: Router) {} // ----------------------------------------------------------------------------------------------------- // @ Public methods // ----------------------------------------------------------------------------------------------------- /** * Resolver * * @param route * @param state */ resolve( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable { return this._noticeService.getNoticeById(route.paramMap.get('id')).pipe( // Error here means the requested product is not available catchError((error) => { // Log the error console.error(error); // Get the parent url const parentUrl = state.url.split('/').slice(0, -1).join('/'); // Navigate to there this._router.navigateByUrl(parentUrl); // Throw an error return throwError(error); }) ); } } @Injectable({ providedIn: 'root', }) export class NoticesResolver implements Resolve { /** * Constructor */ constructor(private _noticeService: NoticeService) {} // ----------------------------------------------------------------------------------------------------- // @ Public methods // ----------------------------------------------------------------------------------------------------- /** * Resolver * * @param route * @param state */ resolve( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable<{ pagination: NoticePagination; notices: Notice[]; }> { return this._noticeService.getNotices(); } }