Merge branch 'release/1.4.0' into develop
[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 { NavigationEnd, Router } from '@angular/router'
4 import { getParameterByName } from '../shared/misc/utils'
5 import { AuthService } from '@app/core'
6 import { of } from 'rxjs'
7
8 @Component({
9   selector: 'my-header',
10   templateUrl: './header.component.html',
11   styleUrls: [ './header.component.scss' ]
12 })
13
14 export class HeaderComponent implements OnInit {
15   searchValue = ''
16
17   constructor (
18     private router: Router,
19     private auth: AuthService
20   ) {}
21
22   ngOnInit () {
23     this.router.events
24         .pipe(
25           filter(e => e instanceof NavigationEnd),
26           map(() => getParameterByName('search', window.location.href))
27         )
28         .subscribe(searchQuery => this.searchValue = searchQuery || '')
29   }
30
31   doSearch () {
32     const queryParams: any = {
33       search: this.searchValue
34     }
35
36     const o = this.auth.isLoggedIn()
37       ? this.loadUserLanguages(queryParams)
38       : of(true)
39
40     o.subscribe(() => this.router.navigate([ '/search' ], { queryParams }))
41   }
42
43   private loadUserLanguages (queryParams: any) {
44     return this.auth.userInformationLoaded
45                .pipe(
46                  first(),
47                  tap(() => Object.assign(queryParams, { languageOneOf: this.auth.getUser().videoLanguages }))
48                )
49   }
50 }