Fix Bug 1978: Unable to view the NS Instances on switching between projects from...
[osm/NG-UI.git] / src / app / utilities / switch-project / SwitchProjectComponent.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 Switch Project Component
20  */
21 import { HttpClient, HttpHeaders } from '@angular/common/http';
22 import { Component, Injector, Input, OnInit } from '@angular/core';
23 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
24 import { Router } from '@angular/router';
25 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
26 import { APIURLHEADER, ERRORDATA, LOCALSTORAGE, URLPARAMS } from 'CommonModel';
27 import { DataService } from 'DataService';
28 import { environment } from 'environment';
29 import { RestService } from 'RestService';
30
31 /**
32  * Creating component
33  * @Component takes SwitchProjectComponent.html as template url
34  */
35 @Component({
36   templateUrl: './SwitchProjectComponent.html',
37   styleUrls: ['./SwitchProjectComponent.scss']
38 })
39 /** Exporting a class @exports SwitchProjectComponent */
40 export class SwitchProjectComponent implements OnInit {
41   /** Invoke service injectors @public */
42   public injector: Injector;
43
44   /** dataService to pass the data from one component to another @public */
45   public dataService: DataService;
46
47   /** Varaibles to hold http client @public */
48   public httpClient: HttpClient;
49
50   /** Instance for active modal service @public */
51   public activeModal: NgbActiveModal;
52
53   /** FormGroup instance added to the form @ html @public */
54   public switchProjectForm: FormGroup;
55
56   /** Form submission Add */
57   public submitted: boolean = false;
58
59   /** Check the Projects loading results @public */
60   public isLoadingResults: boolean = false;
61
62   /** Give the message for the loading @public */
63   public message: string = 'PLEASEWAIT';
64
65   /** Controls the header form @private */
66   private headers: HttpHeaders;
67
68   /** Input contains component objects @private */
69   @Input() private params: URLPARAMS;
70
71   /** Instance of the rest service @private */
72   private restService: RestService;
73
74   /** FormBuilder instance added to the formBuilder @private */
75   private formBuilder: FormBuilder;
76
77   /** Service holds the router information @private */
78   private router: Router;
79
80   constructor(injector: Injector) {
81     this.injector = injector;
82     this.dataService = this.injector.get(DataService);
83     this.restService = this.injector.get(RestService);
84     this.activeModal = this.injector.get(NgbActiveModal);
85     this.formBuilder = this.injector.get(FormBuilder);
86     this.router = this.injector.get(Router);
87   }
88
89   /** convenience getter for easy access to form fields */
90   get f(): FormGroup['controls'] { return this.switchProjectForm.controls; }
91
92   /**
93    * Lifecyle Hooks the trigger before component is instantiate
94    */
95   public ngOnInit(): void {
96     this.initializeForm();
97   }
98
99   /** initialize Forms @public */
100   public initializeForm(): void {
101     this.switchProjectForm = this.formBuilder.group({
102       password: ['', [Validators.required]]
103     });
104   }
105
106   /** Switch project @public */
107   public switchProject(): void {
108     this.submitted = true;
109     if (!this.switchProjectForm.invalid) {
110       this.isLoadingResults = true;
111       this.headers = new HttpHeaders({
112         'Content-Type': 'application/json',
113         Accept: 'application/json',
114         'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
115       });
116       const payLoad: {} = JSON.stringify({
117         username: this.params.username,
118         password: this.switchProjectForm.value.password,
119         project_id: this.params.projectID
120       });
121       const apiURLHeader: APIURLHEADER = {
122         url: environment.GENERATETOKEN_URL,
123         httpOptions: { headers: this.headers }
124       };
125       this.restService.postResource(apiURLHeader, payLoad).subscribe((data: LOCALSTORAGE) => {
126         if (data) {
127           localStorage.setItem('id_token', data.id);
128           localStorage.setItem('project_id', this.params.projectID);
129           localStorage.setItem('expires', data.expires.toString());
130           localStorage.setItem('username', data.username);
131           localStorage.setItem('project', data.project_name);
132           localStorage.setItem('token_state', data.id);
133           this.activeModal.close();
134           if (this.router.url.includes('history-operations')) {
135             this.router.navigate(['/instances/ns']).then((): void => {
136               location.reload();
137             }).catch();
138           } else {
139             location.reload();
140           }
141           this.isLoadingResults = false;
142         }
143       }, (error: ERRORDATA): void => {
144         this.isLoadingResults = false;
145         this.restService.handleError(error, 'post');
146         this.activeModal.close();
147       });
148     }
149   }
150 }