]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/header/header.component.ts
Some language fixes (#2198)
[github/Chocobozzz/PeerTube.git] / client / src / app / header / header.component.ts
CommitLineData
a5f8b0b4 1import { filter, first, map, tap } from 'rxjs/operators'
f3aaa9a9 2import { Component, OnInit } from '@angular/core'
baeb429d 3import { ActivatedRoute, NavigationEnd, Params, Router } from '@angular/router'
f3aaa9a9 4import { getParameterByName } from '../shared/misc/utils'
a5f8b0b4
C
5import { AuthService } from '@app/core'
6import { of } from 'rxjs'
f3aaa9a9
C
7
8@Component({
9 selector: 'my-header',
10 templateUrl: './header.component.html',
11 styleUrls: [ './header.component.scss' ]
12})
13
14export class HeaderComponent implements OnInit {
15 searchValue = ''
16
a5f8b0b4
C
17 constructor (
18 private router: Router,
baeb429d 19 private route: ActivatedRoute,
a5f8b0b4
C
20 private auth: AuthService
21 ) {}
f3aaa9a9
C
22
23 ngOnInit () {
66dc5907 24 this.router.events
db400f44
C
25 .pipe(
26 filter(e => e instanceof NavigationEnd),
ff644b30 27 map(() => getParameterByName('search', window.location.href))
db400f44 28 )
ff644b30 29 .subscribe(searchQuery => this.searchValue = searchQuery || '')
f3aaa9a9
C
30 }
31
32 doSearch () {
baeb429d
C
33 const queryParams: Params = {}
34
35 if (window.location.pathname === '/search' && this.route.snapshot.queryParams) {
36 Object.assign(queryParams, this.route.snapshot.queryParams)
a5f8b0b4
C
37 }
38
baeb429d
C
39 Object.assign(queryParams, { search: this.searchValue })
40
a5f8b0b4 41 const o = this.auth.isLoggedIn()
baeb429d 42 ? this.loadUserLanguagesIfNeeded(queryParams)
a5f8b0b4
C
43 : of(true)
44
45 o.subscribe(() => this.router.navigate([ '/search' ], { queryParams }))
46 }
47
baeb429d
C
48 private loadUserLanguagesIfNeeded (queryParams: any) {
49 if (queryParams && queryParams.languageOneOf) return of(queryParams)
50
a5f8b0b4
C
51 return this.auth.userInformationLoaded
52 .pipe(
53 first(),
54 tap(() => Object.assign(queryParams, { languageOneOf: this.auth.getUser().videoLanguages }))
55 )
f3aaa9a9
C
56 }
57}