Feature 10941: User Management Enhancements
[osm/NG-UI.git] / src / app / utilities / warning / WarningComponent.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: SANDHYA JS (sandhya.j@tataelxsi.co.in)
17 */
18 /**
19  * @file WarningConfiguration Model
20  */
21 import { HttpHeaders } from '@angular/common/http';
22 import { Component, Injector, Input } from '@angular/core';
23 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
24 import { TranslateService } from '@ngx-translate/core';
25 import { NotifierService } from 'angular-notifier';
26 import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, UNLOCKPARAMS } from 'CommonModel';
27 import { environment } from 'environment';
28 import { RestService } from 'RestService';
29 /**
30  * Creating component
31  * @Component takes WarningComponent.html as template url
32  */
33 @Component({
34     selector: 'app-warning',
35     templateUrl: './WarningComponent.html',
36     styleUrls: ['./WarningComponent.scss']
37 })
38 /** Exporting a class @exports WarningComponent */
39 export class WarningComponent {
40     /** To inject services @public */
41     public injector: Injector;
42
43     /** Instance for active modal service @public */
44     public activeModal: NgbActiveModal;
45
46     /** Check the loading results @public */
47     public isLoad: Boolean = false;
48
49     /** Contains title data @public */
50     @Input()
51     public heading: string;
52
53     /** Contains body data @public */
54     @Input()
55     public confirmationMessage: string;
56
57     /** Contains footer data @public */
58     @Input()
59     public submitMessage: string;
60
61     /** Give the message for the loading @public */
62     public message: string = 'PLEASEWAIT';
63
64     /** Contains id of the admin user @public */
65     @Input()
66     public id: string;
67
68     /** Holds which action to perform @public */
69     @Input()
70     public action: boolean = false;
71
72     /** Contains editType data @public */
73     @Input()
74     public editType: string;
75
76     /** handle translate @public */
77     public translateService: TranslateService;
78
79     /** Controls the header form @private */
80     private headers: HttpHeaders;
81
82     /** Instance of the rest service @private */
83     private restService: RestService;
84
85     /** Notifier service to popup notification @private */
86     private notifierService: NotifierService;
87
88     constructor(injector: Injector) {
89         this.injector = injector;
90         this.activeModal = this.injector.get(NgbActiveModal);
91         this.restService = this.injector.get(RestService);
92         this.translateService = this.injector.get(TranslateService);
93         this.notifierService = this.injector.get(NotifierService);
94     }
95
96     /**
97      * Lifecyle Hooks the trigger before component is instantiate
98      */
99     public ngOnInit(): void {
100         //empty
101     }
102
103     /** Close the modal  @public */
104     public closeModal(getMessage: string): void {
105         this.activeModal.close({ message: getMessage });
106     }
107
108     /**
109      * called on submit @private onSubmit
110      */
111     public onSubmit(): void {
112         this.isLoad = true;
113         const modalData: MODALCLOSERESPONSEDATA = {
114             message: 'Done'
115         };
116         const id: string = localStorage.getItem('user_id');
117         const payLoad: UNLOCKPARAMS = {};
118         if (this.editType === 'unlock') {
119             payLoad.system_admin_id = id;
120             payLoad.unlock = true;
121         } else {
122             payLoad.system_admin_id = id;
123             payLoad.renew = true;
124         }
125         const apiURLHeader: APIURLHEADER = {
126             url: environment.USERS_URL + '/' + this.id,
127             httpOptions: { headers: this.headers }
128         };
129         this.restService.patchResource(apiURLHeader, payLoad).subscribe((result: {}): void => {
130             this.activeModal.close(modalData);
131             this.isLoad = false;
132             if (this.editType === 'unlock') {
133                 this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.UNLOCKUSER'));
134             } else {
135                 this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.RENEWUSER'));
136             }
137         }, (error: ERRORDATA): void => {
138             this.restService.handleError(error, 'put');
139             this.isLoad = false;
140         }, (): void => {
141             this.isLoad = false;
142         });
143     }
144 }