]>
Commit | Line | Data |
---|---|---|
37a44fc9 | 1 | import { Observable } from 'rxjs' |
5fb2e288 | 2 | import { map } from 'rxjs/operators' |
5fb2e288 | 3 | import { ActivatedRouteSnapshot, Resolve, Router } from '@angular/router' |
d4a8e7a6 | 4 | import { ResultList } from '@shared/models' |
5fb2e288 | 5 | |
37a44fc9 C |
6 | export abstract class AbstractLazyLoadResolver <T> implements Resolve<any> { |
7 | protected router: Router | |
5fb2e288 C |
8 | |
9 | resolve (route: ActivatedRouteSnapshot) { | |
10 | const url = route.params.url | |
5fb2e288 C |
11 | |
12 | if (!url) { | |
13 | console.error('Could not find url param.', { params: route.params }) | |
14 | return this.router.navigateByUrl('/404') | |
15 | } | |
16 | ||
37a44fc9 | 17 | return this.finder(url) |
5fb2e288 C |
18 | .pipe( |
19 | map(result => { | |
20 | if (result.data.length !== 1) { | |
21 | console.error('Cannot find result for this URL') | |
22 | return this.router.navigateByUrl('/404') | |
23 | } | |
24 | ||
37a44fc9 | 25 | const redirectUrl = this.buildUrl(result.data[0]) |
5fb2e288 | 26 | |
37a44fc9 | 27 | return this.router.navigateByUrl(redirectUrl) |
5fb2e288 C |
28 | }) |
29 | ) | |
30 | } | |
37a44fc9 C |
31 | |
32 | protected abstract finder (url: string): Observable<ResultList<T>> | |
33 | protected abstract buildUrl (e: T): string | |
5fb2e288 | 34 | } |