|
1 | | -import { |
2 | | - AfterViewInit, |
3 | | - Component, |
4 | | - Input, |
5 | | - ViewChild, |
6 | | -} from '@angular/core'; |
7 | | -import { MatPaginator } from '@angular/material/paginator'; |
8 | | -import { MatSort } from '@angular/material/sort'; |
9 | | -import { TranslateService } from '@ngx-translate/core'; |
10 | | -import { startWith, switchMap, map, catchError } from 'rxjs/operators'; |
11 | | -import { UserGetManyResponse, UserResponse } from '../../user.model'; |
12 | | -import { UserService } from '../../user.service'; |
13 | | -import { merge, Observable, of as observableOf } from 'rxjs'; |
14 | | -import { environment } from '@environments/environment'; |
15 | | -import { DefaultPageSizeOptions } from '@shared/constants/page.constants'; |
16 | | -import { ActivatedRoute } from '@angular/router'; |
17 | | -import { MeService } from '@shared/services/me.service'; |
18 | | - |
19 | | -@Component({ |
20 | | - selector: 'app-user-table', |
21 | | - templateUrl: './user-table.component.html', |
22 | | - styleUrls: ['./user-table.component.scss'], |
23 | | -}) |
24 | | -export class UserTableComponent implements AfterViewInit { |
25 | | - displayedColumns: string[] = [ |
26 | | - 'name', |
27 | | - 'email', |
28 | | - 'global', |
29 | | - 'status', |
30 | | - 'lastLogin', |
31 | | - 'menu', |
32 | | - ]; |
33 | | - data: UserResponse[]; |
34 | | - |
35 | | - public pageSize = environment.tablePageSize; |
36 | | - pageSizeOptions = DefaultPageSizeOptions; |
37 | | - resultsLength = 0; |
38 | | - isLoadingResults = true; |
39 | | - @ViewChild(MatPaginator) paginator: MatPaginator; |
40 | | - @ViewChild(MatSort) sort: MatSort; |
41 | | - |
42 | | - // If supplied, users will only be retrieved for the specified organization |
43 | | - // If supplied, permissionId will ignored, even if supplied |
44 | | - @Input() organizationId?: number; |
45 | | - |
46 | | - // If supplied, users will be retrieved on the permissionId (userGroup/brugerGruppe) |
47 | | - @Input() permissionId?: number; |
48 | | - |
49 | | - @Input() canSort = true; |
50 | | - isGlobalAdmin: boolean; |
51 | | - |
52 | | - constructor( |
53 | | - public translate: TranslateService, |
54 | | - private userService: UserService, |
55 | | - private meService: MeService |
56 | | - ) { |
57 | | - this.isGlobalAdmin = this.meService.hasGlobalAdmin(); |
58 | | - } |
59 | | - |
60 | | - getUsers( |
61 | | - orderByColumn: string, |
62 | | - orderByDirection: string |
63 | | - ): Observable<UserGetManyResponse> { |
64 | | - if (this.organizationId !== null && this.organizationId !== undefined) { |
65 | | - if (this.isGlobalAdmin) { |
66 | | - return this.userService.getMultiple( |
67 | | - this.paginator.pageSize, |
68 | | - this.paginator.pageIndex * this.paginator.pageSize, |
69 | | - orderByColumn, |
70 | | - orderByDirection |
71 | | - ); |
72 | | - } else { |
73 | | - return this.userService.getMultipleByOrganization( |
74 | | - this.paginator.pageSize, |
75 | | - this.paginator.pageIndex * this.paginator.pageSize, |
76 | | - orderByColumn, |
77 | | - orderByDirection, |
78 | | - this.organizationId |
79 | | - ); |
80 | | - } |
81 | | - } else { |
82 | | - return this.userService.getMultiple( |
83 | | - this.paginator.pageSize, |
84 | | - this.paginator.pageIndex * this.paginator.pageSize, |
85 | | - orderByColumn, |
86 | | - orderByDirection, |
87 | | - this.permissionId |
88 | | - ); |
89 | | - } |
90 | | - } |
91 | | - |
92 | | - ngAfterViewInit() { |
93 | | - // If the user changes the sort order, reset back to the first page. |
94 | | - this.sort.sortChange.subscribe(() => (this.paginator.pageIndex = 0)); |
95 | | - |
96 | | - merge(this.sort.sortChange, this.paginator.page) |
97 | | - .pipe( |
98 | | - startWith({}), |
99 | | - switchMap(() => { |
100 | | - this.isLoadingResults = true; |
101 | | - return this.getUsers(this.sort.active, this.sort.direction); |
102 | | - }), |
103 | | - map((data) => { |
104 | | - // Flip flag to show that loading has finished. |
105 | | - this.isLoadingResults = false; |
106 | | - this.resultsLength = data.count; |
107 | | - |
108 | | - return data.data; |
109 | | - }), |
110 | | - catchError(() => { |
111 | | - this.isLoadingResults = false; |
112 | | - return observableOf([]); |
113 | | - }) |
114 | | - ) |
115 | | - .subscribe((data) => (this.data = data)); |
116 | | - } |
117 | | -} |
| 1 | +import { AfterViewInit, Component, Input, ViewChild } from '@angular/core'; |
| 2 | +import { MatPaginator } from '@angular/material/paginator'; |
| 3 | +import { MatSort } from '@angular/material/sort'; |
| 4 | +import { TranslateService } from '@ngx-translate/core'; |
| 5 | +import { catchError, map, startWith, switchMap } from 'rxjs/operators'; |
| 6 | +import { UserGetManyResponse, UserResponse } from '../../user.model'; |
| 7 | +import { UserService } from '../../user.service'; |
| 8 | +import { merge, Observable, of as observableOf } from 'rxjs'; |
| 9 | +import { environment } from '@environments/environment'; |
| 10 | +import { DefaultPageSizeOptions } from '@shared/constants/page.constants'; |
| 11 | +import { MeService } from '@shared/services/me.service'; |
| 12 | + |
| 13 | +@Component({ |
| 14 | + selector: 'app-user-table', |
| 15 | + templateUrl: './user-table.component.html', |
| 16 | + styleUrls: ['./user-table.component.scss'], |
| 17 | +}) |
| 18 | +export class UserTableComponent implements AfterViewInit { |
| 19 | + displayedColumns: string[] = [ |
| 20 | + 'name', |
| 21 | + 'email', |
| 22 | + 'global', |
| 23 | + 'status', |
| 24 | + 'lastLogin', |
| 25 | + 'menu', |
| 26 | + ]; |
| 27 | + data: UserResponse[]; |
| 28 | + |
| 29 | + public pageSize = environment.tablePageSize; |
| 30 | + pageSizeOptions = DefaultPageSizeOptions; |
| 31 | + resultsLength = 0; |
| 32 | + isLoadingResults = true; |
| 33 | + @ViewChild(MatPaginator) paginator: MatPaginator; |
| 34 | + @ViewChild(MatSort) sort: MatSort; |
| 35 | + |
| 36 | + // If supplied, users will only be retrieved for the specified organization |
| 37 | + // If supplied, permissionId will ignored, even if supplied |
| 38 | + @Input() organizationId?: number; |
| 39 | + |
| 40 | + // If supplied, users will be retrieved on the permissionId (userGroup/brugerGruppe) |
| 41 | + @Input() permissionId?: number; |
| 42 | + |
| 43 | + @Input() canSort = true; |
| 44 | + isGlobalAdmin: boolean; |
| 45 | + |
| 46 | + constructor( |
| 47 | + public translate: TranslateService, |
| 48 | + private userService: UserService, |
| 49 | + private meService: MeService |
| 50 | + ) { |
| 51 | + this.isGlobalAdmin = this.meService.hasGlobalAdmin(); |
| 52 | + } |
| 53 | + |
| 54 | + getUsers( |
| 55 | + orderByColumn: string, |
| 56 | + orderByDirection: string |
| 57 | + ): Observable<UserGetManyResponse> { |
| 58 | + if (this.organizationId !== null && this.organizationId !== undefined) { |
| 59 | + if (this.isGlobalAdmin) { |
| 60 | + return this.userService.getMultiple( |
| 61 | + this.paginator.pageSize, |
| 62 | + this.paginator.pageIndex * this.paginator.pageSize, |
| 63 | + orderByColumn, |
| 64 | + orderByDirection |
| 65 | + ); |
| 66 | + } else { |
| 67 | + return this.userService.getMultipleByOrganization( |
| 68 | + this.paginator.pageSize, |
| 69 | + this.paginator.pageIndex * this.paginator.pageSize, |
| 70 | + orderByColumn, |
| 71 | + orderByDirection, |
| 72 | + this.organizationId |
| 73 | + ); |
| 74 | + } |
| 75 | + } else { |
| 76 | + return this.userService.getMultiple( |
| 77 | + this.paginator.pageSize, |
| 78 | + this.paginator.pageIndex * this.paginator.pageSize, |
| 79 | + orderByColumn, |
| 80 | + orderByDirection, |
| 81 | + this.permissionId |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + ngAfterViewInit() { |
| 87 | + // If the user changes the sort order, reset back to the first page. |
| 88 | + this.sort.sortChange.subscribe(() => (this.paginator.pageIndex = 0)); |
| 89 | + |
| 90 | + merge(this.sort.sortChange, this.paginator.page) |
| 91 | + .pipe( |
| 92 | + startWith({}), |
| 93 | + switchMap(() => { |
| 94 | + this.isLoadingResults = true; |
| 95 | + return this.getUsers(this.sort.active, this.sort.direction); |
| 96 | + }), |
| 97 | + map((data) => { |
| 98 | + // Flip flag to show that loading has finished. |
| 99 | + this.isLoadingResults = false; |
| 100 | + this.resultsLength = data.count; |
| 101 | + |
| 102 | + return data.data; |
| 103 | + }), |
| 104 | + catchError(() => { |
| 105 | + this.isLoadingResults = false; |
| 106 | + return observableOf([]); |
| 107 | + }) |
| 108 | + ) |
| 109 | + .subscribe((data) => (this.data = data)); |
| 110 | + } |
| 111 | +} |
0 commit comments