a9d239dfa3752c77798873875dcd6f54e511e21b
[usecase-ui.git] /
1 import { Component, OnInit } from "@angular/core";
2 import { Router } from "@angular/router";
3 import { NzMessageService } from "ng-zorro-antd";
4 import { intentBaseService } from "../../../../core/services/intentBase.service";
5
6 @Component({
7   selector: 'app-cloud-leased-line',
8   templateUrl: './cloud-leased-line.component.html',
9   styleUrls: ['./cloud-leased-line.component.less']
10 })
11 export class CloudLeasedLineComponent implements OnInit {
12
13   constructor(
14     private router:Router,
15     private myHttp: intentBaseService,
16     private nzMessage: NzMessageService 
17   ) {}
18
19         ngOnChanges() {}
20
21         ngOnInit() {
22     this.pageIndex = 1;
23     this.pageSize = 10;
24     this.getCloudLeasedLineList();
25   }
26
27         ngOnDestroy() {}
28
29   statusObj: any = {
30     0: 'Incomplete',
31     1: 'Completed',
32     2: 'Deleted',
33     3: 'Inactive'
34   }
35   // table lists
36         listOfData: any[] = [];
37   // pageSize or pageNum
38         pageIndex: number = 1;
39         pageSize: number = 10;
40         total: number = 0;
41         loading = false;
42   // cantrol dialog show or hidden
43   cloudLeasedLineShowFlag: boolean = false;
44   smartCloudLeasedLineShowFlag: boolean = false;
45   // resolve to dialog
46   resolveResult: any = null;
47   intervalTime: number = 5000;
48   progressingTimer: any[] = [];
49   
50   // init source data
51         getCloudLeasedLineList(): void {
52     this.myHttp.getInstanceList({
53       currentPage: this.pageIndex,
54       pageSize: this.pageSize
55     }).subscribe((response) => {
56       const { code, message, data } = response;
57       if (code !== 200) {
58         this.nzMessage.error(message);
59                                 return;
60       }
61       this.total = data.totalRecords;
62       if (data.list === 0) {
63         return;
64       }
65       
66       this.listOfData = data.list.map((item, index) => {
67         if (this.statusObj[item.status] === 'Incomplete') {
68           const updateStatus = (prodata) => {
69             item.status = prodata.status || item.status;
70           };
71           
72           const obj = { instanceId: item.instanceId };
73           this.queryStatus(obj, index, updateStatus).then(() => {
74             item.status = '1';
75           });
76         }
77         return item;
78       });
79     }, (err) => {
80       console.log(err);
81     });
82   }
83
84   queryStatus(obj: any, index: number, callback: any) {
85     return new Promise((res) => {
86                         const requery = () => {
87         const param = {
88           ids: [obj.instanceId]
89         };
90                                 this.myHttp.getInstanceStatus(param).subscribe((response) => {
91           const { code, data:{ IntentInstances } } = response;
92           if (code !== 200 || !IntentInstances || IntentInstances.length === 0) {
93             return;
94           }
95           const intentInstance = IntentInstances[0];
96           if (this.statusObj[intentInstance.status] === 'Incomplete') {
97             callback(intentInstance);
98             let progressSetTimeOut = setTimeout(() => {
99               requery();
100             }, this.intervalTime);
101             this.progressingTimer.push({
102               instanceId: obj.instanceId,
103               timer: progressSetTimeOut,
104             });
105           } else {
106             this.progressingTimer.forEach((item) => {
107               if (item.instanceId === obj.instanceId) {
108                 clearInterval(item.timer);
109               }
110             });
111             res(intentInstance);
112           }
113                                 });
114                         };
115                         requery();
116                 });
117   }
118
119   // change page message
120   searchData(): void {
121     this.getCloudLeasedLineList();
122   }
123   
124   // dialog show
125         cloudLeasedLineShow(): void {
126           this.cloudLeasedLineShowFlag = true;
127         }
128   // dialog close
129         cloudLeasedLineClose(): void {
130     this.cloudLeasedLineShowFlag = false;
131     this.pageIndex = 1;
132     this.pageSize = 10;
133     this.getCloudLeasedLineList();
134   }
135   // smart dialog show
136         smartCloudLeasedLineShow(): void {
137           this.smartCloudLeasedLineShowFlag = true;
138         }
139   // smart dialog close
140         smartCloudLeasedLineClose(param): void {
141     this.smartCloudLeasedLineShowFlag = false;
142     if (param.cancel) {
143       return;
144     }
145
146     this.resolveResult = param.data;
147     this.cloudLeasedLineShowFlag = true;
148   }
149   // to monitor page
150   goMonitorService(data): void {
151     // this.router.navigateByUrl('/fcaps/monitor_service');navigate
152     this.router.navigate(['/fcaps/monitor_service'], {
153       queryParams: {
154         instanceId: data.instanceId
155       },
156       skipLocationChange: true
157     });
158   }
159
160   activeCloudLeasedLine(row): void {
161     this.myHttp.activeIntentInstance({
162       instanceId: row.instanceId
163     }).subscribe((data) => {
164       const { code, message } = data;
165       if (code !== 200) {
166         this.nzMessage.error(message);
167         return;
168       }
169       this.getCloudLeasedLineList();
170     }, (err) => {
171       console.log(err);
172     });
173   }
174
175   inactiveCloudLeasedLine(row): void {
176     this.myHttp.invalidIntentInstance({
177       instanceId: row.instanceId
178     }).subscribe((data) => {
179       const { code, message } = data;
180       if (code !== 200) {
181         this.nzMessage.error(message);
182         return;
183       }
184       this.getCloudLeasedLineList();
185     }, (err) => {
186       console.log(err);
187     });
188   }
189
190   deleteCloudLeasedLine(row): void {
191     this.myHttp.deleteIntentInstance(row.instanceId).subscribe((data) => {
192       const { code, message } = data;
193       if (code !== 200) {
194         this.nzMessage.error(message);
195         return;
196       }
197       this.getCloudLeasedLineList();
198     }, (err) => {
199       console.log(err);
200     });
201   }
202 }