rework admin table
[portal/ric-dashboard.git] / webapp-frontend / src / app / admin / user.datasource.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 import { CollectionViewer, DataSource } from '@angular/cdk/collections';
22 import { MatSort } from '@angular/material';
23 import { merge } from 'rxjs';
24 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
25 import { Observable } from 'rxjs/Observable';
26 import { of } from 'rxjs/observable/of';
27 import { catchError, finalize, map } from 'rxjs/operators';
28 import { DashboardUser } from '../interfaces/dashboard.types';
29 import { DashboardService } from '../services/dashboard/dashboard.service';
30
31 export class UserDataSource extends DataSource<DashboardUser> {
32
33   private usersSubject = new BehaviorSubject<DashboardUser[]>([]);
34
35   private loadingSubject = new BehaviorSubject<boolean>(false);
36
37   public loading$ = this.loadingSubject.asObservable();
38
39   constructor(private dashboardSvc: DashboardService, private sort: MatSort) {
40     super();
41   };
42
43   loadTable() {
44     this.loadingSubject.next(true);
45     this.dashboardSvc.getUsers()
46       .pipe(
47         catchError(() => of([])),
48         finalize(() => this.loadingSubject.next(false))
49       )
50       .subscribe(Users => this.usersSubject.next(Users))
51   }
52
53   connect(collectionViewer: CollectionViewer): Observable<DashboardUser[]> {
54     const dataMutations = [
55       this.usersSubject.asObservable(),
56       this.sort.sortChange
57     ];
58     return merge(...dataMutations).pipe(map(() => {
59       return this.getSortedData([...this.usersSubject.getValue()]);
60     }));
61   }
62
63   disconnect(collectionViewer: CollectionViewer): void {
64     this.usersSubject.complete();
65     this.loadingSubject.complete();
66   }
67
68   private getSortedData(data: DashboardUser[]) {
69     if (!this.sort.active || this.sort.direction === '') {
70       return data;
71     }
72
73     return data.sort((a: DashboardUser, b: DashboardUser) => {
74       const isAsc = this.sort.direction === 'asc';
75       switch (this.sort.active) {
76         case 'id': return compare(a.id, b.id, isAsc);
77         case 'firstName': return compare(a.firstName, b.firstName, isAsc);
78         case 'lastName': return compare(a.lastName, b.lastName, isAsc);
79         case 'status': return compare(a.status, b.status, isAsc);
80         default: return 0;
81       }
82     });
83   }
84 }
85
86 function compare(a, b, isAsc: boolean) {
87   return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
88 }