]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/menu/menu.component.ts
refactor theme toggle into a service
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / menu.component.ts
CommitLineData
8afc19a6 1import { Component, OnInit, ViewChild } from '@angular/core'
b33f657c 2import { UserRight } from '../../../../shared/models/users/user-right.enum'
1a00c561 3import { AuthService, AuthStatus, RedirectService, ServerService, ThemeService } from '../core'
b33f657c 4import { User } from '../shared/users/user.model'
8afc19a6 5import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
602eb142
C
6
7@Component({
8 selector: 'my-menu',
383bfc83 9 templateUrl: './menu.component.html',
3eeeb87f 10 styleUrls: [ './menu.component.scss' ]
602eb142
C
11})
12export class MenuComponent implements OnInit {
8afc19a6
C
13 @ViewChild('languageChooserModal') languageChooserModal: LanguageChooserComponent
14
b33f657c 15 user: User
df98563e 16 isLoggedIn: boolean
954605a8
C
17 userHasAdminAccess = false
18
19 private routesPerRight = {
20 [UserRight.MANAGE_USERS]: '/admin/users',
4610bc5b 21 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
954605a8
C
22 [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/video-abuses',
23 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/video-blacklist'
24 }
602eb142
C
25
26 constructor (
27 private authService: AuthService,
db7af09b 28 private serverService: ServerService,
8c985ef5 29 private redirectService: RedirectService,
1a00c561 30 private themeService: ThemeService
602eb142
C
31 ) {}
32
df98563e
C
33 ngOnInit () {
34 this.isLoggedIn = this.authService.isLoggedIn()
b33f657c 35 if (this.isLoggedIn === true) this.user = this.authService.getUser()
954605a8 36 this.computeIsUserHasAdminAccess()
602eb142
C
37
38 this.authService.loginChangedSource.subscribe(
39 status => {
40 if (status === AuthStatus.LoggedIn) {
df98563e 41 this.isLoggedIn = true
b33f657c 42 this.user = this.authService.getUser()
954605a8 43 this.computeIsUserHasAdminAccess()
df98563e 44 console.log('Logged in.')
602eb142 45 } else if (status === AuthStatus.LoggedOut) {
df98563e 46 this.isLoggedIn = false
b33f657c 47 this.user = undefined
954605a8 48 this.computeIsUserHasAdminAccess()
df98563e 49 console.log('Logged out.')
602eb142 50 } else {
df98563e 51 console.error('Unknown auth status: ' + status)
602eb142
C
52 }
53 }
df98563e 54 )
602eb142
C
55 }
56
291e8d3e 57 isRegistrationAllowed () {
ff2c1fe8
RK
58 return this.serverService.getConfig().signup.allowed &&
59 this.serverService.getConfig().signup.allowedForCurrentIP
a184c71b
C
60 }
61
954605a8
C
62 getFirstAdminRightAvailable () {
63 const user = this.authService.getUser()
64 if (!user) return undefined
65
66 const adminRights = [
67 UserRight.MANAGE_USERS,
4610bc5b 68 UserRight.MANAGE_SERVER_FOLLOW,
954605a8
C
69 UserRight.MANAGE_VIDEO_ABUSES,
70 UserRight.MANAGE_VIDEO_BLACKLIST
71 ]
72
73 for (const adminRight of adminRights) {
74 if (user.hasRight(adminRight)) {
75 return adminRight
76 }
77 }
78
79 return undefined
80 }
81
82 getFirstAdminRouteAvailable () {
83 const right = this.getFirstAdminRightAvailable()
84
85 return this.routesPerRight[right]
602eb142
C
86 }
87
b33f657c
C
88 logout (event: Event) {
89 event.preventDefault()
90
df98563e 91 this.authService.logout()
602eb142 92 // Redirect to home page
b1d40cff 93 this.redirectService.redirectToHomepage()
602eb142 94 }
954605a8 95
8afc19a6
C
96 openLanguageChooser () {
97 this.languageChooserModal.show()
98 }
99
9a0fc840 100 toggleDarkTheme () {
1a00c561 101 this.themeService.toggleDarkTheme()
9a0fc840
RK
102 }
103
954605a8
C
104 private computeIsUserHasAdminAccess () {
105 const right = this.getFirstAdminRightAvailable()
106
107 this.userHasAdminAccess = right !== undefined
108 }
602eb142 109}