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 OsmRepoCreateUpdateComponent.ts
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';
33 * @Component takes OsmRepoCreateUpdateComponent.html as template url
36 selector: 'app-osm-repo-create-update',
37 templateUrl: './OsmRepoCreateUpdateComponent.html',
38 styleUrls: ['./OsmRepoCreateUpdateComponent.scss']
40 export class OsmRepoCreateUpdateComponent implements OnInit {
41 /** To inject services @public */
42 public injector: Injector;
44 /** Instance of the rest service @public */
45 public restService: RestService;
47 /** Instance for active modal service @public */
48 public activeModal: NgbActiveModal;
50 /** Setting OSM Repo types in array @public */
51 public osmrepoType: TYPESECTION[];
53 /** FormGroup osm repo added to the form @ html @public */
54 public osmrepoForm: FormGroup;
56 /** Form submission Add */
57 public submitted: boolean = false;
59 /** Check the loading results for loader status @public */
60 public isLoadingResults: boolean = false;
62 /** Give the message for the loading @public */
63 public message: string = 'PLEASEWAIT';
65 /** Contains osm repo create or edit @public */
66 public getCreateupdateType: string;
68 /** To inject input type services For creation or edit @public */
69 @Input() public createupdateType: string;
71 /** To inject input type services for ID @public */
72 @Input() public osmrepoid: string;
74 /** Contains all methods related to shared @public */
75 public sharedService: SharedService;
77 /** FormBuilder instance added to the formBuilder @private */
78 private formBuilder: FormBuilder;
80 /** Notifier service to popup notification @private */
81 private notifierService: NotifierService;
83 /** Contains tranlsate instance @private */
84 private translateService: TranslateService;
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);
96 /** convenience getter for easy access to form fields */
97 get f(): FormGroup['controls'] { return this.osmrepoForm.controls; }
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();
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]]
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({
126 description: data.description
128 this.isLoadingResults = false;
129 }, (error: ERRORDATA) => {
130 this.restService.handleError(error, 'get');
131 this.isLoadingResults = false;
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) {
142 this.isLoadingResults = true;
143 const modalData: MODALCLOSERESPONSEDATA = {
146 if (this.getCreateupdateType === 'Add') {
147 const apiURLHeader: APIURLHEADER = {
148 url: environment.OSMREPOS_URL
150 this.addOsmRepo(apiURLHeader, modalData);
151 } else if (this.getCreateupdateType === 'Edit') {
152 const apiURLHeader: APIURLHEADER = {
153 url: environment.OSMREPOS_URL + '/' + this.osmrepoid
155 this.editOsmRepo(apiURLHeader, modalData);
157 this.notifierService.notify('error', this.translateService.instant('ERROR'));
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');
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');