a8c617ae7ea9e1128f4330c701e8374209476892
[usecase-ui.git] /
1 import { Component, EventEmitter, Input, OnInit, Output } from "@angular/core";
2 import { NzMessageService } from "ng-zorro-antd";
3 import { intentBaseService } from "../../../../core/services/intentBase.service";
4 import { Util } from "../../../../shared/utils/utils";
5 import { COMMUNICATION_FORM_ITEMS } from "../constants";
6
7 @Component({
8   selector: 'app-cloud-leased-line-modal',
9   templateUrl: './cloud-leased-line-modal.component.html',
10   styleUrls: ['./cloud-leased-line-modal.component.less']
11 })
12 export class CloudLeasedLineModalComponent implements OnInit {
13
14   constructor(
15     private myHttp: intentBaseService,
16                 private nzMessage: NzMessageService,
17                 private Util: Util
18         ) {}
19
20   @Input() modelParams: any;
21   @Input() cloudLeasedLineShowFlag: boolean;
22   @Output() cancelEmitter = new EventEmitter<boolean>();
23   comunicationFormItems = COMMUNICATION_FORM_ITEMS;
24   isLoadingOne = false;
25   nodeLists: any[] = [];
26   cloudPointOptions: any[] = [];
27   cloud_leased_line_info = {
28                 name: '',
29                 instanceId: '',
30                 accessPointOne: {
31       name: '',
32       bandwidth: ''
33     },
34                 cloudPointName: '',
35         };
36
37   ngOnInit(): void {}
38   
39   ngOnChanges() {
40     if (this.cloudLeasedLineShowFlag) {
41       if (this.modelParams) {
42         this.cloud_leased_line_info = { ...this.modelParams };
43       } else {
44         this.getInstanceId();
45       }
46       this.queryAccessNodeInfo();
47     }
48         }
49   
50   queryAccessNodeInfo() {
51     this.myHttp.queryAccessNodeInfo().subscribe(
52       (response) => {
53         const { code, data } = response;
54         if (code !== 200) {
55           return;
56         }
57         this.cloudPointOptions = [...data.cloudAccessNodeList];
58         this.nodeLists = [...data.accessNodeList];
59       },
60       (err) => {
61         console.log(err);
62       }
63     )
64   }
65
66   getInstanceId() {
67     this.myHttp.getInstanceId().subscribe(
68       (response) => {
69         const { code, message, data} = response;
70         if (code !== 200) {
71           this.nzMessage.error(message);
72           return;
73         }
74         this.cloud_leased_line_info.instanceId = data && data.instanceId;
75       },
76       (err) => {
77         console.log(err);
78       }
79     )
80   }
81
82   submit(): void {
83     const paramOnj = { ...this.cloud_leased_line_info };
84     for (const iterator in paramOnj) {
85       if (this.isString(paramOnj[iterator]) && !paramOnj[iterator]) {
86         this.nzMessage.error(`Please enter ${iterator}`);
87         return;
88       }
89       if (!this.isString(paramOnj[iterator])) {
90         const { name, bandwidth} = paramOnj[iterator];
91         if (!name) {
92           this.nzMessage.error(`Please enter accessPointOne Name`);
93           return;
94         }
95
96         if (bandwidth !== 0 && !/^\+?[1-9][0-9]*$/.test(bandwidth)) {
97           this.nzMessage.error(`Please enter a positive integer accessPointOne bandwidth`);
98           return;
99         }
100       }
101     }
102     
103     this.myHttp.createIntentInstance({
104       ...this.cloud_leased_line_info
105     }).subscribe(
106       (data) => {
107         this.nzMessage.success('Create IntentInstance Success!');
108         this.cancel();
109       },
110       (err) => {
111         console.log(err);
112       }
113     )
114   }
115
116   cancel(): void {
117     this.cloudLeasedLineShowFlag = false
118     this.cloud_leased_line_info = {
119       name: '',
120       instanceId: '',
121       accessPointOne: {
122         name: '',
123         bandwidth: ''
124       },
125       cloudPointName: '',
126     };
127     this.cancelEmitter.emit();
128   }
129
130   isString(val) {
131     return Object.prototype.toString.call(val) === '[object String]';
132   }
133 }