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