| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 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 User Settings Modal Component. |
| 20 | */ |
| 21 | |
| 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'; |
| 27 | |
| 28 | /** |
| 29 | * Creating component |
| 30 | * @Component takes UserSettingsComponent.html as template url |
| 31 | */ |
| 32 | @Component({ |
| 33 | templateUrl: './UserSettingsComponent.html', |
| 34 | styleUrls: ['./UserSettingsComponent.scss'] |
| 35 | }) |
| 36 | /** Exporting a class @exports UserSettingsComponent */ |
| 37 | export class UserSettingsComponent implements OnInit { |
| 38 | /** Invoke service injectors @public */ |
| 39 | public injector: Injector; |
| 40 | |
| 41 | /** Supported language list for the dropdown */ |
| 42 | public languageList: {}[]; |
| 43 | |
| 44 | /** FormGroup instance added to the form @ html @public */ |
| 45 | public usersettingsForm: FormGroup; |
| 46 | |
| 47 | /** Instance for active modal service @public */ |
| 48 | public activeModal: NgbActiveModal; |
| 49 | |
| 50 | /** Form submission Add */ |
| 51 | public submitted: boolean = false; |
| 52 | |
| 53 | /** FormBuilder instance added to the formBuilder @private */ |
| 54 | private formBuilder: FormBuilder; |
| 55 | |
| 56 | /** Instance for translate service @private */ |
| 57 | private translateService: TranslateService; |
| 58 | |
| 59 | /** Contains all methods related to shared @private */ |
| 60 | private sharedService: SharedService; |
| 61 | |
| 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); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Lifecyle Hooks the trigger before component is instantiate |
| 72 | */ |
| 73 | public ngOnInit(): void { |
| 74 | this.initializeSettings(); |
| 75 | } |
| 76 | |
| 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]] |
| 83 | }); |
| SANDHYA.JS | dc7a661 | 2023-05-10 23:01:35 +0530 | [diff] [blame^] | 84 | const setLanguage: string = sessionStorage.getItem('languageCode'); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 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); |
| 88 | } else { |
| 89 | // tslint:disable-next-line:no-backbone-get-set-outside-model |
| 90 | this.usersettingsForm.get('selectedLanguage').setValue('en'); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** convenience getter for easy access to form fields */ |
| 95 | get f(): FormGroup['controls'] { return this.usersettingsForm.controls; } |
| 96 | |
| 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; |
| SANDHYA.JS | dc7a661 | 2023-05-10 23:01:35 +0530 | [diff] [blame^] | 102 | sessionStorage.setItem('languageCode', this.usersettingsForm.value.selectedLanguage); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 103 | this.translateService.use(selectedLanguage); |
| 104 | location.reload(); |
| 105 | } |
| 106 | } |
| 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); |
| 110 | } |
| 111 | } |