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'); } } }