|
1 | | -import { Component } from '@angular/core'; |
| 1 | +import { Component, OnInit } from '@angular/core'; |
2 | 2 |
|
3 | 3 | import { TranslateService } from '@ngx-translate/core'; |
4 | 4 | import { faBroadcastTower } from '@fortawesome/free-solid-svg-icons'; |
5 | 5 | import { AuthService } from '@app/auth/auth.service'; |
6 | 6 | import { Router } from '@angular/router'; |
7 | | -import { User } from '@shared/form/form-body-application/form-body-application.component'; |
| 7 | +import { Organisation } from '@app/admin/organisation/organisation.model'; |
| 8 | +import { UserResponse } from '@app/admin/users/user.model'; |
| 9 | +import { SharedVariableService } from '@app/shared-variable/shared-variable.service'; |
8 | 10 |
|
9 | 11 | @Component({ |
10 | | - selector: 'app-navbar', |
11 | | - templateUrl: './navbar.component.html', |
12 | | - styleUrls: ['./navbar.component.scss'], |
| 12 | + selector: 'app-navbar', |
| 13 | + templateUrl: './navbar.component.html', |
| 14 | + styleUrls: ['./navbar.component.scss'], |
13 | 15 | }) |
14 | | -export class NavbarComponent { |
15 | | - public user: User; |
16 | | - isLoginMode = true; |
17 | | - faBroadcastTower = faBroadcastTower; |
18 | | - |
19 | | - |
20 | | - constructor( |
21 | | - private authService: AuthService, |
22 | | - public translate: TranslateService, |
23 | | - private router: Router, |
24 | | - ) { |
25 | | - translate.use('da'); |
26 | | - } |
27 | | - |
28 | | - onLogout() { |
29 | | - this.authService.logout(); |
30 | | - this.router.navigateByUrl('auth'); |
31 | | - } |
32 | | - |
33 | | - isLoggedIn() { |
34 | | - return this.authService.isLoggedIn() |
35 | | - } |
| 16 | +export class NavbarComponent implements OnInit { |
| 17 | + public user: UserResponse; |
| 18 | + public organisations: Organisation[]; |
| 19 | + |
| 20 | + isLoginMode = true; |
| 21 | + faBroadcastTower = faBroadcastTower; |
| 22 | + |
| 23 | + constructor( |
| 24 | + private authService: AuthService, |
| 25 | + public translate: TranslateService, |
| 26 | + private router: Router, |
| 27 | + private sharedVariableServioce: SharedVariableService |
| 28 | + ) { |
| 29 | + translate.use('da'); |
| 30 | + } |
| 31 | + |
| 32 | + ngOnInit(): void { |
| 33 | + this.getAllowedOrganizations(); |
| 34 | + } |
| 35 | + |
| 36 | + onLogout() { |
| 37 | + this.authService.logout(); |
| 38 | + this.router.navigateByUrl('auth'); |
| 39 | + } |
| 40 | + |
| 41 | + isLoggedIn() { |
| 42 | + return this.authService.isLoggedIn(); |
| 43 | + } |
| 44 | + |
| 45 | + getAllowedOrganizations() { |
| 46 | + this.authService.me().subscribe((response) => { |
| 47 | + this.organisations = response.organizations; |
| 48 | + this.user = response.user; |
| 49 | + this.sharedVariableServioce.getSelectedOrganisationId(); |
| 50 | + if ( |
| 51 | + (this.sharedVariableServioce.getSelectedOrganisationId() == 0 && |
| 52 | + response.organizations.length > 0) || |
| 53 | + !response.organizations.some( |
| 54 | + (x) => x.id == this.sharedVariableServioce.getSelectedOrganisationId() |
| 55 | + ) |
| 56 | + ) { |
| 57 | + this.setSelectedOrganisation(response.organizations[0].id); |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + setSelectedOrganisation(value) { |
| 63 | + this.sharedVariableServioce.setSelectedOrganisationId(value); |
| 64 | + } |
| 65 | + |
| 66 | + getSelectedOrganisation() { |
| 67 | + return this.sharedVariableServioce.getSelectedOrganisationId(); |
| 68 | + } |
| 69 | + |
| 70 | + public onChange(value) { |
| 71 | + this.setSelectedOrganisation(value); |
| 72 | + this.sharedVariableServioce.setValue(value); |
| 73 | + } |
36 | 74 | } |
0 commit comments