Initial Commit - NG UI
[osm/NG-UI.git] / src / app / k8s / k8s-add-repo / K8sAddRepoComponent.ts
1 /*
2  Copyright 2020 TATA ELXSI
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  Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17 */
18 /**
19  * @file k8sAddRepoComponent.ts.
20  */
21 import { Component, Injector, OnInit } from '@angular/core';
22 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
23 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
24 import { TranslateService } from '@ngx-translate/core';
25 import { NotifierService } from 'angular-notifier';
26 import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
27 import { environment } from 'environment';
28 import { RestService } from 'RestService';
29 import { SharedService } from 'SharedService';
30 /**
31  * Creating Component
32  * @Component takes K8sAddRepoComponent.html as template url
33  */
34 @Component({
35   selector: 'app-k8s-add-repo',
36   templateUrl: './K8sAddRepoComponent.html',
37   styleUrls: ['./K8sAddRepoComponent.scss']
38 })
39 /** Exporting a class @exports K8sAddRepoComponent */
40 export class K8sAddRepoComponent implements OnInit {
41   /** To inject services @public */
42   public injector: Injector;
43
44   /** FormGroup instance added to the form @ html @public */
45   public k8srepoForm: FormGroup;
46
47   /** Instance for active modal service @public */
48   public activeModal: NgbActiveModal;
49
50   /** Form submission Add */
51   public submitted: boolean = false;
52
53   /** Supported Vim type for the dropdown */
54   public repoTypeSelect: {}[];
55
56   /** Check the loading results @public */
57   public isLoadingResults: boolean = false;
58
59   /** Give the message for the loading @public */
60   public message: string = 'PLEASEWAIT';
61
62   /** FormBuilder instance added to the formBuilder @private */
63   private formBuilder: FormBuilder;
64
65   /** Utilizes rest service for any CRUD operations @private */
66   private restService: RestService;
67
68   /** Notifier service to popup notification @private */
69   private notifierService: NotifierService;
70
71   /** Contains tranlsate instance @private */
72   private translateService: TranslateService;
73
74   /** Contains all methods related to shared @private */
75   private sharedService: SharedService;
76
77   constructor(injector: Injector) {
78     this.injector = injector;
79     this.restService = this.injector.get(RestService);
80     this.activeModal = this.injector.get(NgbActiveModal);
81     this.formBuilder = this.injector.get(FormBuilder);
82     this.notifierService = this.injector.get(NotifierService);
83     this.translateService = this.injector.get(TranslateService);
84     this.sharedService = this.injector.get(SharedService);
85   }
86
87   public ngOnInit(): void {
88     this.repoTypeSelect = [
89       { id: 'helm-chart', name: 'Helm Chart' },
90       { id: 'juju-bundle', name: 'Juju Bundle' }
91     ];
92     /** On Initializing call the methods */
93     this.k8srepoFormAction();
94   }
95
96   /** On modal initializing forms  @public */
97   public k8srepoFormAction(): void {
98     this.k8srepoForm = this.formBuilder.group({
99       name: ['', [Validators.required]],
100       type: [null, [Validators.required]],
101       url: ['', [Validators.required, Validators.pattern(this.sharedService.REGX_URL_PATTERN)]],
102       description: ['', [Validators.required]]
103     });
104   }
105
106   /** convenience getter for easy access to form fields */
107   get f(): FormGroup['controls'] { return this.k8srepoForm.controls; }
108
109   /** On modal submit k8sAddRepoSubmit will called @public */
110   public k8sAddRepoSubmit(): void {
111     this.submitted = true;
112     this.sharedService.cleanForm(this.k8srepoForm);
113     if (this.k8srepoForm.invalid) {
114       return;
115     }
116     this.isLoadingResults = true;
117     const modalData: MODALCLOSERESPONSEDATA = {
118       message: 'Done'
119     };
120     const apiURLHeader: APIURLHEADER = {
121       url: environment.K8REPOS_URL
122     };
123     this.restService.postResource(apiURLHeader, this.k8srepoForm.value).subscribe((result: {}) => {
124       this.activeModal.close(modalData);
125       this.notifierService.notify('success', this.k8srepoForm.value.name +
126         this.translateService.instant('PAGE.K8S.CREATEDSUCCESSFULLY'));
127       this.isLoadingResults = false;
128     }, (error: ERRORDATA) => {
129       this.isLoadingResults = false;
130       this.restService.handleError(error, 'post');
131     });
132   }
133
134 }