]>
Commit | Line | Data |
---|---|---|
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' | |
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 route: ActivatedRoute, | |
20 | private auth: AuthService | |
21 | ) {} | |
22 | ||
23 | ngOnInit () { | |
24 | this.router.events | |
25 | .pipe( | |
26 | filter(e => e instanceof NavigationEnd), | |
27 | map(() => getParameterByName('search', window.location.href)) | |
28 | ) | |
29 | .subscribe(searchQuery => this.searchValue = searchQuery || '') | |
30 | } | |
31 | ||
32 | doSearch () { | |
33 | const queryParams: Params = {} | |
34 | ||
35 | if (window.location.pathname === '/search' && this.route.snapshot.queryParams) { | |
36 | Object.assign(queryParams, this.route.snapshot.queryParams) | |
37 | } | |
38 | ||
39 | Object.assign(queryParams, { search: this.searchValue }) | |
40 | ||
41 | const o = this.auth.isLoggedIn() | |
42 | ? this.loadUserLanguagesIfNeeded(queryParams) | |
43 | : of(true) | |
44 | ||
45 | o.subscribe(() => this.router.navigate([ '/search' ], { queryParams })) | |
46 | } | |
47 | ||
48 | private loadUserLanguagesIfNeeded (queryParams: any) { | |
49 | if (queryParams && queryParams.languageOneOf) return of(queryParams) | |
50 | ||
51 | return this.auth.userInformationLoaded | |
52 | .pipe( | |
53 | first(), | |
54 | tap(() => Object.assign(queryParams, { languageOneOf: this.auth.getUser().videoLanguages })) | |
55 | ) | |
56 | } | |
57 | } |