Added target and terminal support in Dashboard
[src/xds/xds-agent.git] / webapp / src / app / pages / targets / target-card / target-card.component.ts
1 /**
2 * @license
3 * Copyright (C) 2018 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 import { Component, Input, Pipe, PipeTransform } from '@angular/core';
20 import { TargetService, ITarget, TargetType, TargetTypeEnum, TargetTypes } from '../../../@core-xds/services/target.service';
21 import { AlertService } from '../../../@core-xds/services/alert.service';
22 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
23 import { ConfirmModalComponent, EType } from '../../confirm/confirm-modal/confirm-modal.component';
24 import { find } from 'rxjs/operator/find';
25 import { findIndex } from 'rxjs/operator/findIndex';
26
27 @Component({
28   selector: 'xds-target-card',
29   styleUrls: ['./target-card.component.scss'],
30   templateUrl: './target-card.component.html',
31 })
32 export class TargetCardComponent {
33
34   // FIXME workaround of https://github.com/angular/angular-cli/issues/2034
35   // should be removed with angular 5
36   // @Input() target: ITarget;
37   @Input() target = <ITarget>null;
38
39   constructor(
40     private alert: AlertService,
41     private targetSvr: TargetService,
42     private modalService: NgbModal,
43   ) {
44   }
45
46   delete(tgt: ITarget) {
47
48     const modal = this.modalService.open(ConfirmModalComponent, {
49       size: 'lg',
50       backdrop: 'static',
51       container: 'nb-layout',
52     });
53     modal.componentInstance.title = 'Confirm SDK deletion';
54     modal.componentInstance.type = EType.YesNo;
55     modal.componentInstance.question = `
56       Do you <b>permanently delete '` + tgt.name + `'</b> target ?
57       <br><br>
58       <i><small>(Target ID: ` + tgt.id + ` )</small></i>`;
59
60     modal.result
61       .then(res => {
62         if (res === 'yes') {
63           this.targetSvr.delete(tgt).subscribe(
64             r => { },
65             err => this.alert.error('ERROR delete: ' + err),
66           );
67         }
68       });
69
70   }
71
72 }
73
74 // Make Target type human readable
75 @Pipe({
76   name: 'readableType',
77 })
78
79 export class TargetReadableTypePipe implements PipeTransform {
80   transform(type: TargetTypeEnum): string {
81     const tt = TargetTypes.find(el => type === el.value);
82     if (tt) {
83       return tt.display;
84     }
85     return String(type);
86   }
87 }