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 } from '@app/core'
6 import { of } from 'rxjs'
10 templateUrl: './header.component.html',
11 styleUrls: [ './header.component.scss' ]
14 export class HeaderComponent implements OnInit {
18 private router: Router,
19 private route: ActivatedRoute,
20 private auth: AuthService
26 filter(e => e instanceof NavigationEnd),
27 map(() => getParameterByName('search', window.location.href))
29 .subscribe(searchQuery => this.searchValue = searchQuery || '')
33 const queryParams: Params = {}
35 if (window.location.pathname === '/search' && this.route.snapshot.queryParams) {
36 Object.assign(queryParams, this.route.snapshot.queryParams)
39 Object.assign(queryParams, { search: this.searchValue })
41 const o = this.auth.isLoggedIn()
42 ? this.loadUserLanguagesIfNeeded(queryParams)
45 o.subscribe(() => this.router.navigate([ '/search' ], { queryParams }))
48 private loadUserLanguagesIfNeeded (queryParams: any) {
49 if (queryParams && queryParams.languageOneOf) return of(queryParams)
51 return this.auth.userInformationLoaded
54 tap(() => Object.assign(queryParams, { languageOneOf: this.auth.getUser().videoLanguages }))