import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router'; import { Observable, throwError } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { FileManagerService } from 'app/modules/admin/apps/file-manager/file-manager.service'; import { Item } from 'app/modules/admin/apps/file-manager/file-manager.types'; @Injectable({ providedIn: 'root' }) export class FileManagerItemsResolver implements Resolve { /** * Constructor */ constructor(private _fileManagerService: FileManagerService) { } // ----------------------------------------------------------------------------------------------------- // @ Public methods // ----------------------------------------------------------------------------------------------------- /** * Resolver * * @param route * @param state */ resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { return this._fileManagerService.getItems(); } } @Injectable({ providedIn: 'root' }) export class FileManagerItemResolver implements Resolve { /** * Constructor */ constructor( private _router: Router, private _fileManagerService: FileManagerService ) { } // ----------------------------------------------------------------------------------------------------- // @ Public methods // ----------------------------------------------------------------------------------------------------- /** * Resolver * * @param route * @param state */ resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { return this._fileManagerService.getItemById(route.paramMap.get('id')) .pipe( // Error here means the requested task 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); }) ); } }