import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router'; import { Observable } from 'rxjs'; import { RemoteData } from '../core/data/remote-data'; import { ItemDataService } from '../core/data/item-data.service'; import { Item } from '../core/shared/item.model'; import { Store } from '@ngrx/store'; import { map } from 'rxjs/operators'; import { hasValue } from '../shared/empty.util'; import { getItemPageRoute } from './item-page-routing-paths'; import { ItemResolver } from './item.resolver'; /** * This class represents a resolver that requests a specific item before the route is activated and will redirect to the * entity page */ @Injectable() export class ItemPageResolver extends ItemResolver { constructor( protected itemService: ItemDataService, protected store: Store, protected router: Router ) { super(itemService, store, router); } /** * Method for resolving an item based on the parameters in the current route * @param {ActivatedRouteSnapshot} route The current ActivatedRouteSnapshot * @param {RouterStateSnapshot} state The current RouterStateSnapshot * @returns Observable<> Emits the found item based on the parameters in the current route, * or an error if something went wrong */ resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable> { return super.resolve(route, state).pipe( map((rd: RemoteData) => { if (rd.hasSucceeded && hasValue(rd.payload)) { const itemRoute = getItemPageRoute(rd.payload); const thisRoute = state.url; if (!thisRoute.startsWith(itemRoute)) { const itemId = rd.payload.uuid; const subRoute = thisRoute.substring(thisRoute.indexOf(itemId) + itemId.length, thisRoute.length); this.router.navigateByUrl(itemRoute + subRoute); } } return rd; }) ); } }