vo.component.ts 2.18 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
import { Component, OnInit, Input } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { UserService } from '../user.service';
import * as t from '../types/types.module';

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

  @Input() group: t.Group;
  public deployment: t.Deployment;

  constructor(
    public userService: UserService,
    public http: HttpClient,
  ) { }

  ngOnInit() {
    let deployments = this.userService.userState.deployments;
    this.deployment = deployments.find(
      (d: t.Deployment) => {
        if (d.group === this.group.id) {
            return true;
        }
        return false;
      }
    );
    console.log(this.deployment);
  }

  public isDeployed(key: t.SSHKeyRef): boolean {
    if (this.deployment) {
      return this.deployment.ssh_keys.some(k => {
        return k.id === key.id;
      });
    }
    return false;
  }

  public taskItem(site: t.Site, key: t.SSHKeyRef): t.DeploymentStateItem {
    if (this.deployment) {
      const deploymentState = this.deployment.states.find(
        state => {
          return state.key.id === key.id
        }
      );
      if (deploymentState) {
        return deploymentState.state_items.find(
          item => {
            return item.site.id === site.id
          }
        );
      }
    }
  }

  public taskState(site: t.Site, key: t.SSHKeyRef): string {
    let item = this.taskItem(site, key);
    if (item) {
      return item.state;
    }
    return "";
  }

  public changeDeployment(key: t.SSHKeyRef, action: string) {
    const body = {
      'type': action,
      'key': key.id,
      'group': this.group.id,
    };
    return this.http.post('/backend/api/deployments', body).subscribe(
      (newDep: t.Deployment) => {
        // update the deployment
        this.deployment = newDep;
      },
      (err) => {
        console.log(err);
        this.userService.update();
      }
    );
  }


  public deploymentChange(key: t.SSHKeyRef) {
    if (!this.isDeployed(key)) {
      this.changeDeployment(key, 'add');
    } else {
      this.changeDeployment(key, 'remove');
    }
  }
}