aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/search/channel-lazy-load.resolver.ts
blob: 5b6961e985e6e6588904a85378b6f0e28867a2f9 (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
36
37
38
39
40
41
42
43
import { map } from 'rxjs/operators'
import { Injectable } from '@angular/core'
import { ActivatedRouteSnapshot, Resolve, Router } from '@angular/router'
import { SearchService } from './search.service'

@Injectable()
export class ChannelLazyLoadResolver implements Resolve<any> {
  constructor (
    private router: Router,
    private searchService: SearchService
  ) { }

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

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

    if (externalRedirect === 'true') {
      window.open(url)
      this.router.navigateByUrl(fromPath)
      return
    }

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

          const channel = result.data[0]

          return this.router.navigateByUrl('/video-channels/' + channel.nameWithHost)
        })
      )
  }
}