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 { 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';
32 * @Component takes SwitchProjectComponent.html as template url
35 templateUrl: './SwitchProjectComponent.html',
36 styleUrls: ['./SwitchProjectComponent.scss']
38 /** Exporting a class @exports SwitchProjectComponent */
39 export class SwitchProjectComponent implements OnInit {
40 /** Invoke service injectors @public */
41 public injector: Injector;
43 /** dataService to pass the data from one component to another @public */
44 public dataService: DataService;
46 /** Varaibles to hold http client @public */
47 public httpClient: HttpClient;
49 /** Instance for active modal service @public */
50 public activeModal: NgbActiveModal;
52 /** FormGroup instance added to the form @ html @public */
53 public switchProjectForm: FormGroup;
55 /** Form submission Add */
56 public submitted: boolean = false;
58 /** Check the Projects loading results @public */
59 public isLoadingResults: boolean = false;
61 /** Give the message for the loading @public */
62 public message: string = 'PLEASEWAIT';
64 /** Controls the header form @private */
65 private headers: HttpHeaders;
67 /** Input contains component objects @private */
68 @Input() private params: URLPARAMS;
70 /** Instance of the rest service @private */
71 private restService: RestService;
73 /** FormBuilder instance added to the formBuilder @private */
74 private formBuilder: FormBuilder;
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);
84 /** convenience getter for easy access to form fields */
85 get f(): FormGroup['controls'] { return this.switchProjectForm.controls; }
88 * Lifecyle Hooks the trigger before component is instantiate
90 public ngOnInit(): void {
91 this.initializeForm();
94 /** initialize Forms @public */
95 public initializeForm(): void {
96 this.switchProjectForm = this.formBuilder.group({
97 password: ['', [Validators.required]]
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'
111 const payLoad: {} = JSON.stringify({
112 username: this.params.username,
113 password: this.switchProjectForm.value.password,
114 project_id: this.params.projectID
116 const apiURLHeader: APIURLHEADER = {
117 url: environment.GENERATETOKEN_URL,
118 httpOptions: { headers: this.headers }
120 this.restService.postResource(apiURLHeader, payLoad).subscribe((data: LOCALSTORAGE) => {
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();
130 this.isLoadingResults = false;
132 }, (error: ERRORDATA) => {
133 this.isLoadingResults = false;
134 this.restService.handleError(error, 'post');
135 this.activeModal.close();