Fix problem with double load of EI tables
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ei-coordinator / ei-job.datasource.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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 { Injectable } from '@angular/core';
22
23 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
24
25 import { EIJob } from '../interfaces/ei.types';
26 import { EIService } from '../services/ei/ei.service';
27
28 @Injectable({
29     providedIn: 'root'
30 })
31
32 export class EIJobDataSource {
33
34     private jobs: Array<EIJob> = [];
35
36     public eiJobs(): EIJob[] {
37         return this.jobs;
38     }
39
40     private loadingSubject = new BehaviorSubject<boolean>(false);
41
42     public loading$ = this.loadingSubject.asObservable();
43
44     public rowCount = 1; // hide footer during intial load
45
46     constructor(
47         private eiSvc: EIService) {
48     }
49
50     loadJobs() {
51         this.loadingSubject.next(true);
52         this.jobs = [];
53         this.eiSvc.getProducerIds()
54             .subscribe((producerIds: string[]) => {
55                 producerIds.forEach(id => {
56                     this.getJobsForProducer(id);
57                 });
58             });
59         this.rowCount = this.jobs.length;
60     }
61
62     private getJobsForProducer(id: string) {
63         console.log('Getting jobs for producer ID: ', id);
64         this.eiSvc.getJobsForProducer(id).subscribe(producerJobs => {
65             this.jobs = this.jobs.concat(producerJobs);
66         });
67     }
68 }