Initial Commit - NG UI
[osm/NG-UI.git] / src / app / utilities / project-link / ProjectLinkComponent.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 Project Link Component.
20  */
21 import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Injector, OnInit } from '@angular/core';
22 import { environment } from 'environment';
23 import { ProjectData } from 'ProjectModel';
24 import { ProjectService } from 'ProjectService';
25 import { RestService } from 'RestService';
26 import { UserDetail } from 'UserModel';
27 /**
28  * Creating component
29  * @Component takes ProjectLinkComponent.html as template url
30  */
31 @Component({
32   selector: 'app-project-link',
33   templateUrl: './ProjectLinkComponent.html',
34   styleUrls: ['./ProjectLinkComponent.scss'],
35   changeDetection: ChangeDetectionStrategy.OnPush
36 })
37 /** Exporting a class @exports ProjectLinkComponent */
38 export class ProjectLinkComponent implements OnInit {
39   /** Invoke service injectors @public */
40   public injector: Injector;
41   /** Variables holds the selected project @public */
42   public selectedProject: string;
43   /** To get the value from the nspackage via valuePrepareFunction default Property of ng-smarttable @public */
44   public value: ProjectData;
45   /** Variables holds all the projects @public */
46   public projectList: {}[] = [];
47   /** Check the project is present for the user @public */
48   public isPresent: boolean = false;
49   /** Set timeout @private */
50   private timeOut: number = 10;
51   /** Instance of the rest service @private */
52   private restService: RestService;
53   /** Holds all project details @private */
54   private projectService: ProjectService;
55   /** Detect changes for the User Input */
56   private cd: ChangeDetectorRef;
57   constructor(injector: Injector) {
58     this.injector = injector;
59     this.projectService = this.injector.get(ProjectService);
60     this.restService = this.injector.get(RestService);
61     this.cd = this.injector.get(ChangeDetectorRef);
62   }
63
64   public ngOnInit(): void {
65     this.selectedProject = localStorage.getItem('project');
66     this.getAdminProjects();
67   }
68
69   /** Get the admin projects to be selectable @public */
70   public getAdminProjects(): void {
71     const username: string = localStorage.getItem('username');
72     this.restService.getResource(environment.USERS_URL + '/' + username).subscribe((projects: UserDetail) => {
73       this.projectList = projects.project_role_mappings;
74       this.isPresent = this.projectList.some((item: ProjectData) => item.project === this.value.project);
75       setTimeout(() => {
76         this.cd.detectChanges();
77       }, this.timeOut);
78     });
79   }
80
81 }