2 Copyright 2020 TATA ELXSI
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
19 * @file Switch Project Component
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';
33 * @Component takes SwitchProjectComponent.html as template url
36 templateUrl: './SwitchProjectComponent.html',
37 styleUrls: ['./SwitchProjectComponent.scss']
39 /** Exporting a class @exports SwitchProjectComponent */
40 export class SwitchProjectComponent implements OnInit {
41 /** Invoke service injectors @public */
42 public injector: Injector;
44 /** dataService to pass the data from one component to another @public */
45 public dataService: DataService;
47 /** Varaibles to hold http client @public */
48 public httpClient: HttpClient;
50 /** Instance for active modal service @public */
51 public activeModal: NgbActiveModal;
53 /** FormGroup instance added to the form @ html @public */
54 public switchProjectForm: FormGroup;
56 /** Form submission Add */
57 public submitted: boolean = false;
59 /** Check the Projects loading results @public */
60 public isLoadingResults: boolean = false;
62 /** Give the message for the loading @public */
63 public message: string = 'PLEASEWAIT';
65 /** Controls the header form @private */
66 private headers: HttpHeaders;
68 /** Input contains component objects @private */
69 @Input() private params: URLPARAMS;
71 /** Instance of the rest service @private */
72 private restService: RestService;
74 /** FormBuilder instance added to the formBuilder @private */
75 private formBuilder: FormBuilder;
77 /** Service holds the router information @private */
78 private router: Router;
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);
89 /** convenience getter for easy access to form fields */
90 get f(): FormGroup['controls'] { return this.switchProjectForm.controls; }
93 * Lifecyle Hooks the trigger before component is instantiate
95 public ngOnInit(): void {
96 this.initializeForm();
99 /** initialize Forms @public */
100 public initializeForm(): void {
101 this.switchProjectForm = this.formBuilder.group({
102 password: ['', [Validators.required]]
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'
116 const payLoad: {} = JSON.stringify({
117 username: this.params.username,
118 password: this.switchProjectForm.value.password,
119 project_id: this.params.projectID
121 const apiURLHeader: APIURLHEADER = {
122 url: environment.GENERATETOKEN_URL,
123 httpOptions: { headers: this.headers }
125 this.restService.postResource(apiURLHeader, payLoad).subscribe((data: LOCALSTORAGE) => {
127 sessionStorage.setItem('id_token', data.id);
128 sessionStorage.setItem('project_id', this.params.projectID);
129 sessionStorage.setItem('expires', data.expires.toString());
130 sessionStorage.setItem('username', data.username);
131 sessionStorage.setItem('project', data.project_name);
132 sessionStorage.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 => {
137 }).catch((): void => {
138 // Catch Navigation Error
143 this.isLoadingResults = false;
145 }, (error: ERRORDATA): void => {
146 this.isLoadingResults = false;
147 this.restService.handleError(error, 'post');
148 this.activeModal.close();