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