aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+search/video-lazy-load.resolver.ts
blob: e43e0089b8891bf346957f1f6d3cdb559fff0237 (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 { map } from 'rxjs/operators'
import { Injectable } from '@angular/core'
import { ActivatedRouteSnapshot, Resolve, Router } from '@angular/router'
import { SearchService } from '@app/shared/shared-search'

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

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

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

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

          const video = result.data[0]

          return this.router.navigateByUrl('/w/' + video.uuid)
        })
      )
  }
}