]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+search/shared/abstract-lazy-load.resolver.ts
Translated using Weblate (Persian)
[github/Chocobozzz/PeerTube.git] / client / src / app / +search / shared / abstract-lazy-load.resolver.ts
1 import { Observable } from 'rxjs'
2 import { map } from 'rxjs/operators'
3 import { ActivatedRouteSnapshot, Resolve, Router } from '@angular/router'
4 import { ResultList } from '@shared/models'
5
6 export abstract class AbstractLazyLoadResolver <T> implements Resolve<any> {
7 protected router: Router
8
9 resolve (route: ActivatedRouteSnapshot) {
10 const url = route.params.url
11
12 if (!url) {
13 console.error('Could not find url param.', { params: route.params })
14 return this.router.navigateByUrl('/404')
15 }
16
17 return this.finder(url)
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
25 const redirectUrl = this.buildUrl(result.data[0])
26
27 return this.router.navigateByUrl(redirectUrl)
28 })
29 )
30 }
31
32 protected abstract finder (url: string): Observable<ResultList<T>>
33 protected abstract buildUrl (e: T): string
34 }