aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+search/shared/abstract-lazy-load.resolver.ts
blob: 7551d977ddd91c83005848131ef686ebfcfd6536 (plain) (blame)
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
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'
import { ActivatedRouteSnapshot, Resolve, Router } from '@angular/router'
import { logger } from '@root-helpers/logger'
import { ResultList } from '@shared/models'

export abstract class AbstractLazyLoadResolver <T> implements Resolve<any> {
  protected router: Router

  resolve (route: ActivatedRouteSnapshot) {
    const url = route.params.url

    if (!url) {
      logger.error('Could not find url param.', { params: route.params })
      return this.router.navigateByUrl('/404')
    }

    return this.finder(url)
      .pipe(
        map(result => {
          if (result.data.length !== 1) {
            logger.error('Cannot find result for this URL')
            return this.router.navigateByUrl('/404')
          }

          const redirectUrl = this.buildUrl(result.data[0])

          return this.router.navigateByUrl(redirectUrl)
        })
      )
  }

  protected abstract finder (url: string): Observable<ResultList<T>>
  protected abstract buildUrl (e: T): string
}