77c9f0dd0e70d34ec24ef1ea9cc45fc79fe7f914
[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     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.cloud_leased_line_info = { ...this.modelParams };
44       } else {
45         this.getInstanceId();
46       }
47       this.queryAccessNodeInfo();
48     }
49         }
50   
51   queryAccessNodeInfo() {
52     this.myHttp.queryAccessNodeInfo().subscribe(
53       (response) => {
54         const { code, data } = response;
55         if (code !== 200) {
56           return;
57         }
58         this.cloudPointOptions = [...data.cloudAccessNodeList];
59         this.nodeLists = [...data.accessNodeList];
60       },
61       (err) => {
62         console.log(err);
63       }
64     )
65   }
66
67   getInstanceId() {
68     this.myHttp.getInstanceId().subscribe(
69       (response) => {
70         const { code, message, data} = response;
71         if (code !== 200) {
72           this.nzMessage.error(message);
73           return;
74         }
75         this.cloud_leased_line_info.instanceId = data && data.instanceId;
76       },
77       (err) => {
78         console.log(err);
79       }
80     )
81   }
82
83   submit(): void {
84     const paramOnj = { ...this.cloud_leased_line_info };
85     for (const iterator in paramOnj) {
86       if (this.isBoolean(paramOnj[iterator])) {
87         continue;
88       }
89       if (this.isString(paramOnj[iterator]) && !paramOnj[iterator]) {
90         this.nzMessage.error(`Please enter ${iterator}`);
91         return;
92       }
93       if (!this.isString(paramOnj[iterator])) {
94         const { name, bandwidth} = paramOnj[iterator];
95         if (!name) {
96           this.nzMessage.error(`Please enter accessPointOne Name`);
97           return;
98         }
99
100         if (bandwidth !== 0 && !/^\+?[1-9][0-9]*$/.test(bandwidth)) {
101           this.nzMessage.error(`Please enter a positive integer accessPointOne bandwidth`);
102           return;
103         }
104       }
105     }
106     
107     this.myHttp.createIntentInstance({
108       ...this.cloud_leased_line_info
109     }).subscribe(
110       (response) => {
111         const { code, message } = response;
112         if (code !== 200) {
113           this.nzMessage.error(message);
114           return;
115         }
116         this.nzMessage.success('Create IntentInstance Success!');
117         this.cancel();
118       },
119       (err) => {
120         console.log(err);
121       }
122     )
123   }
124
125   cancel(): void {
126     this.cloudLeasedLineShowFlag = false
127     this.cloud_leased_line_info = {
128       name: '',
129       instanceId: '',
130       protect: false,
131       accessPointOne: {
132         name: '',
133         bandwidth: ''
134       },
135       cloudPointName: '',
136     };
137     this.cancelEmitter.emit();
138   }
139
140   isString(val) {
141     return typeof val === 'string' || typeof val === 'number';
142     //return Object.prototype.toString.call(val) === '[object String]';
143   }
144
145   isBoolean(val) {
146     return typeof val === 'boolean';
147   }
148 }