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, ServerService, Notifier } from '@app/core'
6 import { of } from 'rxjs'
7 import { ServerConfig } from '@shared/models'
10 selector: 'my-header',
11 templateUrl: './header.component.html',
12 styleUrls: [ './header.component.scss' ]
15 export class HeaderComponent implements OnInit {
18 private serverConfig: ServerConfig
21 private router: Router,
22 private route: ActivatedRoute,
23 private auth: AuthService,
24 private serverService: ServerService,
25 private authService: AuthService,
26 private notifier: Notifier
32 filter(e => e instanceof NavigationEnd),
33 map(() => getParameterByName('search', window.location.href))
35 .subscribe(searchQuery => this.searchValue = searchQuery || '')
37 this.serverConfig = this.serverService.getTmpConfig()
38 this.serverService.getConfig().subscribe(
39 config => this.serverConfig = config,
41 err => this.notifier.error(err.message)
46 const queryParams: Params = {}
48 if (window.location.pathname === '/search' && this.route.snapshot.queryParams) {
49 Object.assign(queryParams, this.route.snapshot.queryParams)
52 Object.assign(queryParams, { search: this.searchValue })
54 const o = this.auth.isLoggedIn()
55 ? this.loadUserLanguagesIfNeeded(queryParams)
58 o.subscribe(() => this.router.navigate([ '/search' ], { queryParams }))
62 return this.authService.isLoggedIn()
65 isRegistrationAllowed () {
66 return this.serverConfig.signup.allowed &&
67 this.serverConfig.signup.allowedForCurrentIP
71 if (this.isUserLoggedIn()) {
72 this.router.navigate([ '/videos/upload' ])
73 } else if (this.isRegistrationAllowed()) {
74 this.router.navigate([ '/signup' ])
76 this.router.navigate([ '/login', { fromUpload: true } ])
80 private loadUserLanguagesIfNeeded (queryParams: any) {
81 if (queryParams && queryParams.languageOneOf) return of(queryParams)
83 return this.auth.userInformationLoaded
86 tap(() => Object.assign(queryParams, { languageOneOf: this.auth.getUser().videoLanguages }))