2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 import { CollectionViewer, DataSource } from '@angular/cdk/collections';
22 import { HttpErrorResponse } from '@angular/common/http';
23 import { MatSort } from '@angular/material';
24 import { Observable } from 'rxjs/Observable';
25 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
26 import { merge } from 'rxjs';
27 import { of } from 'rxjs/observable/of';
28 import { catchError, finalize, map } from 'rxjs/operators';
29 import { DashboardUser } from '../interfaces/dashboard.types';
30 import { DashboardService } from '../services/dashboard/dashboard.service';
31 import { NotificationService } from '../services/ui/notification.service';
33 export class UserDataSource extends DataSource<DashboardUser> {
35 private userSubject = new BehaviorSubject<DashboardUser[]>([]);
37 private loadingSubject = new BehaviorSubject<boolean>(false);
39 public loading$ = this.loadingSubject.asObservable();
41 public rowCount = 1; // hide footer during intial load
43 constructor(private dashboardSvc: DashboardService,
44 private sort: MatSort,
45 private notificationService: NotificationService) {
50 this.loadingSubject.next(true);
51 this.dashboardSvc.getUsers()
53 catchError( (her: HttpErrorResponse) => {
54 console.log('UserDataSource failed: ' + her.message);
55 this.notificationService.error('Failed to get users: ' + her.message);
58 finalize(() => this.loadingSubject.next(false))
60 .subscribe( (users: DashboardUser[]) => {
61 this.rowCount = users.length;
62 this.userSubject.next(users);
66 connect(collectionViewer: CollectionViewer): Observable<DashboardUser[]> {
67 const dataMutations = [
68 this.userSubject.asObservable(),
71 return merge(...dataMutations).pipe(map(() => {
72 return this.getSortedData([...this.userSubject.getValue()]);
76 disconnect(collectionViewer: CollectionViewer): void {
77 this.userSubject.complete();
78 this.loadingSubject.complete();
81 private getSortedData(data: DashboardUser[]) {
82 if (!this.sort.active || this.sort.direction === '') {
86 return data.sort((a: DashboardUser, b: DashboardUser) => {
87 const isAsc = this.sort.direction === 'asc';
88 switch (this.sort.active) {
89 case 'id': return this.compare(a.id, b.id, isAsc);
90 case 'firstName': return this.compare(a.firstName, b.firstName, isAsc);
91 case 'lastName': return this.compare(a.lastName, b.lastName, isAsc);
92 case 'status': return this.compare(a.status, b.status, isAsc);
98 private compare(a: any, b: any, isAsc: boolean) {
99 return (a < b ? -1 : 1) * (isAsc ? 1 : -1);