NG-UI Added support for the OSM Repository
[osm/NG-UI.git] / src / app / osm-repositories / osm-repo-create-update / OsmRepoCreateUpdateComponent.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 OsmRepoCreateUpdateComponent.ts
20  */
21 import { Component, Injector, Input, OnInit } from '@angular/core';
22 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
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, OSMREPO_TYPES, TYPESECTION } from 'CommonModel';
27 import { environment } from 'environment';
28 import { OSMRepoDetails } from 'OsmRepoModel';
29 import { RestService } from 'RestService';
30 import { SharedService } from 'SharedService';
31 /**
32  * Creating Component
33  * @Component takes OsmRepoCreateUpdateComponent.html as template url
34  */
35 @Component({
36   selector: 'app-osm-repo-create-update',
37   templateUrl: './OsmRepoCreateUpdateComponent.html',
38   styleUrls: ['./OsmRepoCreateUpdateComponent.scss']
39 })
40 export class OsmRepoCreateUpdateComponent implements OnInit {
41   /** To inject services @public */
42   public injector: Injector;
43
44   /** Instance of the rest service @public */
45   public restService: RestService;
46
47   /** Instance for active modal service @public */
48   public activeModal: NgbActiveModal;
49
50   /** Setting OSM Repo types in array @public */
51   public osmrepoType: TYPESECTION[];
52
53   /** FormGroup osm repo added to the form @ html @public */
54   public osmrepoForm: FormGroup;
55
56   /** Form submission Add */
57   public submitted: boolean = false;
58
59   /** Check the loading results for loader status @public */
60   public isLoadingResults: boolean = false;
61
62   /** Give the message for the loading @public */
63   public message: string = 'PLEASEWAIT';
64
65   /** Contains osm repo create or edit @public */
66   public getCreateupdateType: string;
67
68   /** To inject input type services For creation or edit @public */
69   @Input() public createupdateType: string;
70
71   /** To inject input type services for ID @public */
72   @Input() public osmrepoid: string;
73
74   /** Contains all methods related to shared @public */
75   public sharedService: SharedService;
76
77   /** FormBuilder instance added to the formBuilder @private */
78   private formBuilder: FormBuilder;
79
80   /** Notifier service to popup notification @private */
81   private notifierService: NotifierService;
82
83   /** Contains tranlsate instance @private */
84   private translateService: TranslateService;
85
86   constructor(injector: Injector) {
87     this.injector = injector;
88     this.formBuilder = this.injector.get(FormBuilder);
89     this.restService = this.injector.get(RestService);
90     this.activeModal = this.injector.get(NgbActiveModal);
91     this.notifierService = this.injector.get(NotifierService);
92     this.translateService = this.injector.get(TranslateService);
93     this.sharedService = this.injector.get(SharedService);
94   }
95
96   /** convenience getter for easy access to form fields */
97   get f(): FormGroup['controls'] { return this.osmrepoForm.controls; }
98
99   public ngOnInit(): void {
100     this.osmrepoType = OSMREPO_TYPES;
101     this.osmRepoFormInitialize();
102     this.getCreateupdateType = this.createupdateType;
103     if (this.getCreateupdateType === 'Edit') {
104       this.fetchAssigndata();
105     }
106   }
107
108   /** On modal initializing forms  @public */
109   public osmRepoFormInitialize(): void {
110     this.osmrepoForm = this.formBuilder.group({
111       name: [null, [Validators.required]],
112       type: [null, [Validators.required]],
113       url: [null, [Validators.required, Validators.pattern(this.sharedService.REGX_URL_PATTERN)]],
114       description: [null, [Validators.required]]
115     });
116   }
117
118   /** Assign and get the osm repo details for edit section @public */
119   public fetchAssigndata(): void {
120     this.isLoadingResults = true;
121     this.restService.getResource(environment.OSMREPOS_URL + '/' + this.osmrepoid).subscribe((data: OSMRepoDetails) => {
122       this.osmrepoForm.setValue({
123         name: data.name,
124         type: data.type,
125         url: data.url,
126         description: data.description
127       });
128       this.isLoadingResults = false;
129     }, (error: ERRORDATA) => {
130       this.restService.handleError(error, 'get');
131       this.isLoadingResults = false;
132     });
133   }
134
135   /** On modal submit osm Repo will called @public */
136   public osmRepoSubmit(): void {
137     this.submitted = true;
138     this.sharedService.cleanForm(this.osmrepoForm);
139     if (this.osmrepoForm.invalid) {
140       return;
141     }
142     this.isLoadingResults = true;
143     const modalData: MODALCLOSERESPONSEDATA = {
144       message: 'Done'
145     };
146     if (this.getCreateupdateType === 'Add') {
147       const apiURLHeader: APIURLHEADER = {
148         url: environment.OSMREPOS_URL
149       };
150       this.addOsmRepo(apiURLHeader, modalData);
151     } else if (this.getCreateupdateType === 'Edit') {
152       const apiURLHeader: APIURLHEADER = {
153         url: environment.OSMREPOS_URL + '/' + this.osmrepoid
154       };
155       this.editOsmRepo(apiURLHeader, modalData);
156     } else {
157       this.notifierService.notify('error', this.translateService.instant('ERROR'));
158     }
159   }
160
161   /** This function used to add the osm repo @public */
162   public addOsmRepo(apiURLHeader: APIURLHEADER, modalData: MODALCLOSERESPONSEDATA): void {
163     this.restService.postResource(apiURLHeader, this.osmrepoForm.value).subscribe(() => {
164       this.activeModal.close(modalData);
165       this.notifierService.notify('success', this.translateService.instant('PAGE.OSMREPO.CREATEDSUCCESSFULLY'));
166       this.isLoadingResults = false;
167     }, (error: ERRORDATA) => {
168       this.isLoadingResults = false;
169       this.restService.handleError(error, 'post');
170     });
171   }
172
173   /** This function used to Edit the osm repo @public */
174   public editOsmRepo(apiURLHeader: APIURLHEADER, modalData: MODALCLOSERESPONSEDATA): void {
175     this.restService.patchResource(apiURLHeader, this.osmrepoForm.value).subscribe(() => {
176       this.activeModal.close(modalData);
177       this.notifierService.notify('success', this.translateService.instant('PAGE.OSMREPO.UPDATEDSUCCESSFULLY'));
178       this.isLoadingResults = false;
179     }, (error: ERRORDATA) => {
180       this.isLoadingResults = false;
181       this.restService.handleError(error, 'patch');
182     });
183   }
184 }