4299ba838612b47595a65796bae38de0b017d77a
[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<any>();
23   comunicationFormItems = COMMUNICATION_FORM_ITEMS;
24   isUpdateFlag: boolean = false;
25   nodeLists: any[] = [];
26   cloudPointOptions: any[] = [];
27   cloud_leased_line_info = {
28                 name: '',
29                 instanceId: '',
30     protect: false,
31                 accessPointOne: {
32       name: '',
33       bandwidth: ''
34     },
35                 cloudPointName: '',
36         };
37
38   ngOnInit(): void {}
39   
40   ngOnChanges() {
41     if (this.cloudLeasedLineShowFlag) {
42       if (this.modelParams) {
43         this.isUpdateFlag = this.modelParams.isUpdateFlag;
44         this.cloud_leased_line_info = { ...this.modelParams };
45       } else {
46         this.getInstanceId();
47       }
48       this.queryAccessNodeInfo();
49     }
50         }
51   
52   queryAccessNodeInfo() {
53     this.myHttp.queryAccessNodeInfo().subscribe(
54       (response) => {
55         const { code, data } = response;
56         if (code !== 200) {
57           return;
58         }
59         this.cloudPointOptions = [...data.cloudAccessNodeList];
60         this.nodeLists = [...data.accessNodeList];
61       },
62       (err) => {
63         console.log(err);
64       }
65     )
66   }
67
68   getInstanceId() {
69     this.myHttp.getInstanceId().subscribe(
70       (response) => {
71         const { code, message, data} = response;
72         if (code !== 200) {
73           this.nzMessage.error(message);
74           return;
75         }
76         this.cloud_leased_line_info.instanceId = data && data.instanceId;
77       },
78       (err) => {
79         console.log(err);
80       }
81     )
82   }
83
84   submit(): void {
85     const paramOnj = { ...this.cloud_leased_line_info };
86     for (const iterator in paramOnj) {
87       if (this.isBoolean(paramOnj[iterator])) {
88         continue;
89       }
90       if (this.isString(paramOnj[iterator]) && !paramOnj[iterator]) {
91         this.nzMessage.error(`Please enter ${iterator}`);
92         return;
93       }
94       if (!this.isString(paramOnj[iterator])) {
95         const { name, bandwidth} = paramOnj[iterator];
96         if (!name) {
97           this.nzMessage.error(`Please enter accessPointOne Name`);
98           return;
99         }
100
101         if (bandwidth !== 0 && !/^\+?[1-9][0-9]*$/.test(bandwidth)) {
102           this.nzMessage.error(`Please enter a positive integer accessPointOne bandwidth`);
103           return;
104         }
105       }
106     }
107     
108     if (this.isUpdateFlag) {
109       this.updateIntentInstance();
110       return;
111     }
112     this.createIntentInstance();
113   }
114
115   updateIntentInstance(): void {
116     const { accessPointOne: { bandwidth } } = this.cloud_leased_line_info;
117     this.myHttp.updateIntentInstance({
118       instanceId: this.modelParams.instanceId,
119       bandwidth
120     }).subscribe(
121       (response) => {
122         const { code, message } = response;
123         if (code !== 200) {
124           this.nzMessage.error(message);
125           return;
126         }
127         this.nzMessage.success('Update IntentInstance Success!');
128         this.cancel(true);
129       },
130       (err) => {
131         console.log(err);
132       }
133     )
134   }
135
136   createIntentInstance(): void {
137     this.myHttp.createIntentInstance({
138       ...this.cloud_leased_line_info
139     }).subscribe(
140       (response) => {
141         const { code, message } = response;
142         if (code !== 200) {
143           this.nzMessage.error(message);
144           return;
145         }
146         this.nzMessage.success('Create IntentInstance Success!');
147         this.cancel(true);
148       },
149       (err) => {
150         console.log(err);
151       }
152     )
153   }
154
155   cancel(flag): void {
156     this.cloudLeasedLineShowFlag = false
157     this.isUpdateFlag = false
158     this.cloud_leased_line_info = {
159       name: '',
160       instanceId: '',
161       protect: false,
162       accessPointOne: {
163         name: '',
164         bandwidth: ''
165       },
166       cloudPointName: '',
167     };
168     this.cancelEmitter.emit(flag);
169   }
170
171   isString(val) {
172     return typeof val === 'string' || typeof val === 'number';
173     //return Object.prototype.toString.call(val) === '[object String]';
174   }
175
176   isBoolean(val) {
177     return typeof val === 'boolean';
178   }
179 }