Fix angular 9 build
[oweals/peertube.git] / client / src / app / header / header.component.ts
1 import { filter, first, map, tap } from 'rxjs/operators'
2 import { Component, OnInit } from '@angular/core'
3 import { ActivatedRoute, NavigationEnd, Params, Router } from '@angular/router'
4 import { getParameterByName } from '../shared/misc/utils'
5 import { AuthService, Notifier, ServerService } from '@app/core'
6 import { of } from 'rxjs'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8
9 @Component({
10   selector: 'my-header',
11   templateUrl: './header.component.html',
12   styleUrls: [ './header.component.scss' ]
13 })
14
15 export class HeaderComponent implements OnInit {
16   searchValue = ''
17   ariaLabelTextForSearch = ''
18
19   constructor (
20     private router: Router,
21     private route: ActivatedRoute,
22     private auth: AuthService,
23     private serverService: ServerService,
24     private authService: AuthService,
25     private notifier: Notifier,
26     private i18n: I18n
27   ) {}
28
29   ngOnInit () {
30     this.ariaLabelTextForSearch = this.i18n('Search videos, channels')
31
32     this.router.events
33         .pipe(
34           filter(e => e instanceof NavigationEnd),
35           map(() => getParameterByName('search', window.location.href))
36         )
37         .subscribe(searchQuery => this.searchValue = searchQuery || '')
38   }
39
40   doSearch () {
41     const queryParams: Params = {}
42
43     if (window.location.pathname === '/search' && this.route.snapshot.queryParams) {
44       Object.assign(queryParams, this.route.snapshot.queryParams)
45     }
46
47     Object.assign(queryParams, { search: this.searchValue })
48
49     const o = this.auth.isLoggedIn()
50       ? this.loadUserLanguagesIfNeeded(queryParams)
51       : of(true)
52
53     o.subscribe(() => this.router.navigate([ '/search' ], { queryParams }))
54   }
55
56   private loadUserLanguagesIfNeeded (queryParams: any) {
57     if (queryParams && queryParams.languageOneOf) return of(queryParams)
58
59     return this.auth.userInformationLoaded
60                .pipe(
61                  first(),
62                  tap(() => Object.assign(queryParams, { languageOneOf: this.auth.getUser().videoLanguages }))
63                )
64   }
65 }