25087d80e4e473894c4c77d2c4b090e93616dab5
[dcaegen2/services.git] /
1 /*
2     Copyright (C) 2019 CMCC, Inc. and others. All rights reserved.
3
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7
8             http://www.apache.org/licenses/LICENSE-2.0
9
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16
17 /**
18  *
19  * @author Chunmeng Guo
20  *
21  */
22
23 import {Component, EventEmitter, OnInit, Output} from '@angular/core';
24 import {Db} from "src/app/core/models/db.model";
25 import {NgbActiveModal, NgbModal} from "@ng-bootstrap/ng-bootstrap";
26 import {NgxSpinnerService} from "ngx-spinner";
27 import {ToastrNotificationService} from "src/app/shared/components/toastr-notification/toastr-notification.service";
28 import {RestApiService} from "src/app/core/services/rest-api.service";
29 import {ModalToolsComponent} from "src/app/views/tools/modal-tools/modal-tools.component";
30
31 @Component({
32   selector: 'app-tool-add-modal',
33   templateUrl: './tool-add-modal.component.html',
34   styleUrls: ['./tool-add-modal.component.css']
35 })
36 export class ToolAddModalComponent implements OnInit {
37
38   @Output() passEntry: EventEmitter<any> = new EventEmitter();
39   seletedItem: string = "";
40   toolList: any = [];
41   loading: Boolean = true;
42   toolNew: Db;
43   tool_NewBody: Db;
44   constructor(
45     public activeModal: NgbActiveModal,
46     private spinner: NgxSpinnerService,
47     private notificationService: ToastrNotificationService,
48     private modalService: NgbModal,
49     private dbApiService: RestApiService
50   ) { }
51
52   ngOnInit() {
53   }
54
55   clickItem(name: string) {
56     this.seletedItem = name;
57   }
58
59   passBack() {
60     console.log(this.seletedItem, "next");
61     this.openNewModal(this.seletedItem);
62   }
63
64   newTool(modalRef) {
65     this.toolNew = new Db();
66     this.tool_NewBody = new Db();
67     modalRef.componentInstance.tool = this.tool_NewBody;
68     modalRef.componentInstance.toolList_length = this.toolList.length;
69     modalRef.componentInstance.passEntry.subscribe(receivedEntry => {
70       this.tool_NewBody = receivedEntry;
71       this.dbApiService
72         .createDb(this.tool_NewBody)
73         .subscribe(
74           res => {
75             this.spinner.hide();
76             if (res.statusCode == 200) {
77               this.toolNew = res.returnBody;
78               this.toolList.push(this.toolNew);
79               this.toolList = [...this.toolList];
80               this.notificationService.success("SUCCESSFULLY_CREARED");
81             } else {
82               this.notificationService.error("FAILED_CREARED");
83             }
84             modalRef.close();
85           },
86           err => {
87             this.spinner.hide();
88             this.notificationService.error(err);
89             modalRef.close();
90           }
91         );
92     });
93   }
94
95   openNewModal(name: string) {
96     let modalRef;
97
98     switch (name) {
99       case "Kibana": {
100         modalRef = this.modalService.open(ModalToolsComponent, {
101           size: "lg",
102           centered: true
103         });
104         modalRef.componentInstance.data = name;
105         this.newTool(modalRef);
106         break;
107       }
108       case "Superset": {
109         modalRef = this.modalService.open(ModalToolsComponent, {
110           size: "lg",
111           centered: true
112         });
113         modalRef.componentInstance.data = name;
114         this.newTool(modalRef);
115         break;
116       }
117       default: {
118         break;
119       }
120     }
121   }
122
123 }