vo-data.component.ts 2.35 KB
Newer Older
1
import { Component, OnInit, Input } from '@angular/core';
2
3
4

import { Observable, BehaviorSubject } from 'rxjs';
import { map } from 'rxjs/operators';
5
6

import { UserService } from '../user.service';
Lukas Burgey's avatar
Lukas Burgey committed
7
import { LanguageService } from '../language.service';
Lukas Burgey's avatar
Lukas Burgey committed
8
import { DialogService } from '../dialogues/dialog.service';
Lukas Burgey's avatar
Lukas Burgey committed
9
import { VO, Site, Service, Deployment, DeploymentState }  from '../types/types.module';
10
11
12
13
14
15
16
17

@Component({
  selector: 'app-vo-data',
  templateUrl: './vo-data.component.html',
  styleUrls: ['./vo-data.component.css']
})
export class VoDataComponent implements OnInit {

18
  @Input() vo: VO;
19

20
21
22
  private services: Service[];
  private services$ = new BehaviorSubject<Service[]>([]);
  public services$$: Observable<Service[]>;
23

24
25
26
  private sites: Map<number, Site> = new Map();
  private sites$ = new BehaviorSubject<Site[]>([]);
  public sites$$: Observable<Site[]>;
27

Lukas Burgey's avatar
Lukas Burgey committed
28
  public deployment$: Observable<Deployment>;
29
30
31

  constructor(
    public userService: UserService,
Lukas Burgey's avatar
Lukas Burgey committed
32
    public lang: LanguageService,
Lukas Burgey's avatar
Lukas Burgey committed
33
    public dialog: DialogService,
34
35
36
37
  ) {
  }

  ngOnInit(): void {
38
39
40
41
42
43
44
45
46
    this.services$$ = this.services$.asObservable();
    this.sites$$ = this.sites$.asObservable();


    this.deployment$ = this.userService.subscribeDeployment(
      (dep: Deployment) => dep.vo ? dep.vo.id == this.vo.id : false
    );

    // this is just used when no deployment exists
Lukas Burgey's avatar
Lukas Burgey committed
47
    this.userService.subscribeServices().subscribe(
48
      (services: Service[]) => {
49
        this.services = services;
50
        this.services$.next(this.services);
51
52
      }
    );
53

54
55
56
57
58
59
60
61
62
63
64
65
    this.deployment$.subscribe(
      (dep: Deployment) => {
        if (dep != undefined) {
          this.services = dep.services;
          this.services$.next(this.services);
        }
      }
    );

    this.services$$.subscribe(
      (services: Service[]) => {
        services.map(
Lukas Burgey's avatar
Lukas Burgey committed
66
          (s: Service) => this.sites.set(s.site.id, s.site)
67
68
        );

69
        const uniqueSites = []
70
71
72
73
74
75
76
77
78
79
80
81
82
        this.sites.forEach(
          site => uniqueSites.push(site)
        );
        this.sites$.next(uniqueSites);
      }
    );
  }

  ngOnDestroy(): void {
    this.services$.complete();
    this.sites$.complete();
  }

83
  public servicesAtSite(site: Site): Observable<Service[]> {
84
85
    return this.services$$.pipe(
      map(
86
        (services: Service[]) => services.filter(
Lukas Burgey's avatar
Lukas Burgey committed
87
88
          (service: Service) => service.site.id === site.id
        ),
89
      ),
90
91
92
    );
  }
}