forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 31
/
item-page.resolver.ts
66 lines (62 loc) · 3.04 KB
/
item-page.resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, 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, isNotEmpty } from '../shared/empty.util';
import { getItemPageRoute } from './item-page-routing-paths';
import { ItemResolver } from './item.resolver';
import { HardRedirectService } from '../core/services/hard-redirect.service';
/**
* 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 hardRedirectService: HardRedirectService,
protected itemService: ItemDataService,
protected store: Store<any>,
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<<RemoteData<Item>> 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<RemoteData<Item>> {
return super.resolve(route, state).pipe(
map((rd: RemoteData<Item>) => {
if (rd.hasSucceeded && hasValue(rd.payload)) {
// Check if custom url not empty and if the current id parameter is different from the custom url redirect to custom url
if (hasValue(rd.payload.metadata) && isNotEmpty(rd.payload.metadata['cris.customurl'])) {
if (route.params.id !== rd.payload.metadata['cris.customurl'][0].value) {
const newUrl = state.url.replace(route.params.id, rd.payload.metadata['cris.customurl'][0].value);
this.router.navigateByUrl(newUrl);
}
} else {
const thisRoute = state.url;
// Angular uses a custom function for encodeURIComponent, (e.g. it doesn't encode commas
// or semicolons) and thisRoute has been encoded with that function. If we want to compare
// it with itemRoute, we have to run itemRoute through Angular's version as well to ensure
// the same characters are encoded the same way.
const itemRoute = this.router.parseUrl(getItemPageRoute(rd.payload)).toString();
if (!thisRoute.startsWith(itemRoute)) {
const itemId = rd.payload.uuid;
const subRoute = thisRoute.substring(thisRoute.indexOf(itemId) + itemId.length, thisRoute.length);
this.hardRedirectService.redirect(itemRoute + subRoute, 301);
}
}
}
return rd;
})
);
}
}