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 User Settings Modal Component.
22 import { Component, Injector, OnInit } from '@angular/core';
23 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
24 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
25 import { TranslateService } from '@ngx-translate/core';
26 import { SharedService } from 'SharedService';
30 * @Component takes UserSettingsComponent.html as template url
33 templateUrl: './UserSettingsComponent.html',
34 styleUrls: ['./UserSettingsComponent.scss']
36 /** Exporting a class @exports UserSettingsComponent */
37 export class UserSettingsComponent implements OnInit {
38 /** Invoke service injectors @public */
39 public injector: Injector;
41 /** Supported language list for the dropdown */
42 public languageList: {}[];
44 /** FormGroup instance added to the form @ html @public */
45 public usersettingsForm: FormGroup;
47 /** Instance for active modal service @public */
48 public activeModal: NgbActiveModal;
50 /** Form submission Add */
51 public submitted: boolean = false;
53 /** FormBuilder instance added to the formBuilder @private */
54 private formBuilder: FormBuilder;
56 /** Instance for translate service @private */
57 private translateService: TranslateService;
59 /** Contains all methods related to shared @private */
60 private sharedService: SharedService;
62 constructor(injector: Injector) {
63 this.injector = injector;
64 this.formBuilder = this.injector.get(FormBuilder);
65 this.activeModal = this.injector.get(NgbActiveModal);
66 this.translateService = this.injector.get(TranslateService);
67 this.sharedService = this.injector.get(SharedService);
71 * Lifecyle Hooks the trigger before component is instantiate
73 public ngOnInit(): void {
74 this.initializeSettings();
77 /** Initialize user's settings */
78 public initializeSettings(): void {
79 this.languageList = this.sharedService.languageCodeList();
80 /** Initializing Form Action */
81 this.usersettingsForm = this.formBuilder.group({
82 selectedLanguage: [null, [Validators.required]]
84 const setLanguage: string = localStorage.getItem('languageCode');
85 if (setLanguage !== null && this.validateLanguageList(setLanguage)) {
86 // tslint:disable-next-line:no-backbone-get-set-outside-model
87 this.usersettingsForm.get('selectedLanguage').setValue(setLanguage);
89 // tslint:disable-next-line:no-backbone-get-set-outside-model
90 this.usersettingsForm.get('selectedLanguage').setValue('en');
94 /** convenience getter for easy access to form fields */
95 get f(): FormGroup['controls'] { return this.usersettingsForm.controls; }
97 /** On modal submit UserSettingsSubmit will called @public */
98 public usersettingsSubmit(): void {
99 this.submitted = true;
100 if (!this.usersettingsForm.invalid) {
101 const selectedLanguage: string = this.usersettingsForm.value.selectedLanguage;
102 localStorage.setItem('languageCode', this.usersettingsForm.value.selectedLanguage);
103 this.translateService.use(selectedLanguage);
107 /** Validate language code in the language list @private */
108 private validateLanguageList(setLanguage: string): boolean {
109 return this.languageList.some((item: { code: string }) => item.code === setLanguage);