Improved test coverage in front end
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ei-coordinator / ei-job.datasource.ts
index b0a348a..9e048b6 100644 (file)
  * ========================LICENSE_END===================================
  */
 
-import { DataSource } from '@angular/cdk/collections';
 import { HttpErrorResponse } from '@angular/common/http';
-import { MatSort } from '@angular/material';
-import { Observable } from 'rxjs/Observable';
+import { Injectable } from '@angular/core';
+import { MatTableDataSource } from '@angular/material';
+
 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
-import { merge } from 'rxjs';
 import { of } from 'rxjs/observable/of';
 import { catchError, finalize, map } from 'rxjs/operators';
-import { EIJob } from '../interfaces/ei.jobs';
+
+import { EIJob } from '../interfaces/ei.types';
 import { EIService } from '../services/ei/ei.service';
 import { NotificationService } from '../services/ui/notification.service';
 
-export class EIJobDataSource extends DataSource<EIJob> {
+@Injectable({
+    providedIn: 'root'
+})
+
+export class EIJobDataSource extends MatTableDataSource<EIJob> {
 
     private eiJobSubject = new BehaviorSubject<EIJob[]>([]);
 
@@ -42,7 +46,6 @@ export class EIJobDataSource extends DataSource<EIJob> {
 
     constructor(
         private eiSvc: EIService,
-        public sort: MatSort,
         private notificationService: NotificationService) {
         super();
     }
@@ -63,38 +66,12 @@ export class EIJobDataSource extends DataSource<EIJob> {
             });
     }
 
-    connect(): Observable<EIJob[]> {
-        const dataMutations = [
-            this.eiJobSubject.asObservable(),
-            this.sort.sortChange
-        ];
-        return merge(...dataMutations).pipe(map(() => {
-            return this.getSortedData([...this.eiJobSubject.getValue()]);
-        }));
+    connect(): BehaviorSubject<EIJob[]> {
+        return this.eiJobSubject;
     }
 
     disconnect(): void {
         this.eiJobSubject.complete();
         this.loadingSubject.complete();
     }
-
-    private getSortedData(data: EIJob[]) {
-        if (!this.sort || !this.sort.active || this.sort.direction === '') {
-            return data;
-        }
-
-        return data.sort((a, b) => {
-            const isAsc = this.sort.direction === 'asc';
-            switch (this.sort.active) {
-                case 'ei_job_identity': return compare(a.ei_job_identity, b.ei_job_identity, isAsc);
-                case 'owner': return compare(a.owner, b.owner, isAsc);
-                case 'ei_type_identity': return compare(a.ei_type_identity, b.ei_type_identity, isAsc);
-                default: return 0;
-            }
-        });
-    }
-}
-
-function compare(a: string, b: string, isAsc: boolean) {
-    return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
 }