Skip to content

Commit b5d1a02

Browse files
author
Allan Asp Christensen
authored
Merge pull request #17 from OS2iot/IOT-466
IOT-466: Dropdown to select organisation + synchronization to application.
2 parents 0e50583 + d2cfa94 commit b5d1a02

File tree

12 files changed

+207
-63
lines changed

12 files changed

+207
-63
lines changed

src/app/admin/permission/permission-list/permission-tabel/permission-row/permission-row.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<ul class="dropdown-menu" attr.aria-labelledby="tableRowDropdown-{{permission.id}}">
2525
<li class="dropdown-item">
2626
<a [routerLink]="[permission.id, 'edit-permission']"
27-
routerLinkActive="active">{{ 'PERMISSION.TABLE-ROW.EDIT' | translate }}
27+
routerLinkActive="active">{{ 'USERS.TABLE-ROW.EDIT' | translate }}
2828
</a>
2929
</li>
3030
<li class="dropdown-item">

src/app/app.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { DatatargetModule } from './views/datatarget/datatarget.module';
1414
import { ProfilesModule } from './profiles/profiles.module';
1515
import { AuthJwtInterceptor } from '@shared/helpers/auth-jwt.interceptor';
1616
import { AuthModule } from './auth/auth.module';
17+
import { GlobalErrorHandler } from '@shared/helpers/global-error-handler';
18+
import { SharedVariableModule } from './shared-variable/shared-variable.module';
1719
import { DashboardModule } from './dashboard/dashboard.module';
1820

1921
export function HttpLoaderFactory(http: HttpClient) {
@@ -23,6 +25,7 @@ export function HttpLoaderFactory(http: HttpClient) {
2325
@NgModule({
2426
declarations: [AppComponent],
2527
imports: [
28+
SharedVariableModule.forRoot(),
2629
AuthModule,
2730
BrowserModule,
2831
BrowserAnimationsModule,

src/app/auth/auth.service.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,28 @@ import { environment } from '@environments/environment';
55
import * as jwtDecode from 'jwt-decode';
66
import * as moment from 'moment';
77
import { of } from 'rxjs/internal/observable/of';
8+
import { RestService } from '@shared/services/rest.service';
9+
import { Observable } from 'rxjs';
10+
import { Organisation } from '@app/admin/organisation/organisation.model';
11+
import { UserResponse } from '../admin/users/user.model';
812

913
export interface AuthResponseData {
1014
accessToken: string;
1115
}
1216

17+
export interface CurrentUserInfoResponse {
18+
user: UserResponse;
19+
organizations: Organisation[];
20+
}
21+
1322
@Injectable({
1423
providedIn: 'root',
1524
})
1625
export class AuthService {
1726
private baseUrl = environment.baseUrl;
1827
private URL = 'auth/login';
1928

20-
constructor(private http: HttpClient) {}
29+
constructor(private http: HttpClient, private restService: RestService) {}
2130

2231
2332
// hunter2
@@ -36,6 +45,10 @@ export class AuthService {
3645
);
3746
}
3847

48+
me(): Observable<CurrentUserInfoResponse> {
49+
return this.restService.get('auth/me');
50+
}
51+
3952
private setSession(jwt: string) {
4053
localStorage.setItem('id_token', jwt);
4154
}

src/app/models/application.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import { IotDevice } from '../my-applications/iot-devices/iot-device.model';
2+
import { Organisation } from '../admin/organisation/organisation.model';
23

34
export class Application {
4-
public id: number;
5-
public createdAt: string;
6-
public updatedAt: string;
7-
public name: string;
8-
public description: string;
9-
public iotDevices?: IotDevice[];
5+
public id: number;
6+
public createdAt: string;
7+
public updatedAt: string;
8+
public name: string;
9+
public description: string;
10+
public iotDevices?: IotDevice[];
11+
public belongsTo: Organisation;
12+
}
13+
14+
export class ApplicationRequest {
15+
public name: string;
16+
public description: string;
17+
public organizationId: number;
1018
}
1119

1220
export interface ApplicationData {
13-
data: Application[];
14-
ok?: boolean;
15-
count?: number;
21+
data: Application[];
22+
ok?: boolean;
23+
count?: number;
1624
}

src/app/my-applications/list-applications/list-applications.component.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
import { Component, OnInit, OnChanges, SimpleChanges, OnDestroy } from '@angular/core';
1+
import {
2+
Component,
3+
OnInit,
4+
OnChanges,
5+
SimpleChanges,
6+
OnDestroy,
7+
} from '@angular/core';
28
import { Application } from '@app/models/application';
39
import { TranslateService } from '@ngx-translate/core';
410
import { ApplicationService } from '@shared/services/application.service';
511
import { Subscription } from 'rxjs';
612
import { Sort } from 'src/app/models/sort';
13+
import { NavbarComponent } from '../../navbar/navbar.component';
14+
import { SharedVariableService } from '../../shared-variable/shared-variable.service';
715

816
@Component({
17+
providers: [NavbarComponent],
918
selector: 'app-list-applications',
1019
templateUrl: './list-applications.component.html',
1120
styleUrls: ['./list-applications.component.scss'],
@@ -65,18 +74,21 @@ export class ListApplicationsComponent implements OnInit, OnChanges, OnDestroy {
6574

6675
constructor(
6776
public translate: TranslateService,
68-
private applicationService: ApplicationService
77+
private applicationService: ApplicationService,
78+
private globalService: SharedVariableService
6979
) {
7080
translate.use('da');
7181
}
72-
82+
7383
ngOnChanges(changes: SimpleChanges): void {
7484
this.getApplications();
7585
}
7686

7787
ngOnInit(): void {
7888
this.getApplications();
79-
89+
this.globalService.getValue().subscribe((organisationId) => {
90+
this.getApplications(organisationId);
91+
});
8092
}
8193

8294
ngOnDestroy() {
@@ -107,7 +119,6 @@ export class ListApplicationsComponent implements OnInit, OnChanges, OnDestroy {
107119
}
108120

109121
nextPage() {
110-
console.log("got next outer")
111122
if (this.pageOffset < this.pageTotal) {
112123
this.pageOffset++;
113124
}
@@ -122,13 +133,18 @@ export class ListApplicationsComponent implements OnInit, OnChanges, OnDestroy {
122133
});
123134
}
124135

125-
getApplications(): void {
136+
getCurrentOrganisationId(): number {
137+
return this.globalService.getSelectedOrganisationId();
138+
}
139+
140+
getApplications(orgId?: number): void {
126141
this.applicationsSubscription = this.applicationService
127142
.getApplications(
128143
this.pageLimit,
129144
this.pageOffset * this.pageLimit,
130145
this.selectedSortObject.dir,
131-
this.selectedSortObject.col
146+
this.selectedSortObject.col,
147+
orgId ? orgId : this.getCurrentOrganisationId()
132148
)
133149
.subscribe((applications) => {
134150
this.applications = applications.data;

src/app/navbar/navbar.component.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424
<app-global-admin></app-global-admin>
2525
</li>
2626
<li class="nav-item py-2">
27-
<a>
28-
Skanderborg Kommune
29-
</a>
27+
<!-- <label class="form-label" for="name">{{'QUESTION.CHOOSE-PERMISSION-TYPE' | translate}}</label>* -->
28+
<select id="organisation" name="organisation" class="form-select" required #orgSelect
29+
(change)="onChange(orgSelect.value)">
30+
<option *ngFor="let org of organisations" [value]="org.id"
31+
[selected]="org.id === getSelectedOrganisation()">
32+
{{org.name}}</option>
33+
</select>
3034
</li>
3135
<li class="nav-item py-2">
3236
<ul class="navbar-nav flex-column">

src/app/navbar/navbar.component.ts

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,74 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
22

33
import { TranslateService } from '@ngx-translate/core';
44
import { faBroadcastTower } from '@fortawesome/free-solid-svg-icons';
55
import { AuthService } from '@app/auth/auth.service';
66
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';
810

911
@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'],
1315
})
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+
}
3674
}

src/app/navbar/navbar.module.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,9 @@ import { NGMaterialModule } from '@shared/Modules/materiale.module';
2323
RouterModule,
2424
TranslateModule,
2525
FontAwesomeModule,
26-
NGMaterialModule
26+
NGMaterialModule,
2727
],
28-
exports: [
29-
NavbarComponent,
30-
],
31-
providers: [
32-
RestService,
33-
]
28+
exports: [NavbarComponent],
29+
providers: [RestService, NavbarComponent],
3430
})
35-
export class NavbarModule { }
31+
export class NavbarModule {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { NgModule } from '@angular/core';
2+
import { SharedVariableService } from '@app/shared-variable/shared-variable.service';
3+
4+
@NgModule({})
5+
export class SharedVariableModule {
6+
static forRoot() {
7+
return {
8+
ngModule: SharedVariableModule,
9+
providers: [SharedVariableService],
10+
};
11+
}
12+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Injectable } from '@angular/core';
2+
import { BehaviorSubject, Observable } from 'rxjs';
3+
4+
@Injectable({
5+
providedIn: 'root',
6+
})
7+
export class SharedVariableService {
8+
constructor() {
9+
this.routerInfo = new BehaviorSubject<number>(0);
10+
}
11+
12+
private selectedOrganisationId: number;
13+
private routerInfo: BehaviorSubject<number>;
14+
15+
getValue(): Observable<number> {
16+
return this.routerInfo.asObservable();
17+
}
18+
19+
setValue(newValue: number): void {
20+
this.setSelectedOrganisationId(newValue);
21+
this.routerInfo.next(newValue);
22+
}
23+
24+
setSelectedOrganisationId(value: number) {
25+
localStorage.setItem('selected_organisation', value.toString());
26+
this.selectedOrganisationId = value;
27+
}
28+
29+
getSelectedOrganisationId() {
30+
if (this.selectedOrganisationId != null) {
31+
return this.selectedOrganisationId;
32+
}
33+
34+
return +localStorage.getItem('selected_organisation');
35+
}
36+
}

0 commit comments

Comments
 (0)