blob: 16ecadd496e895ee991a409d64b184cfd0a047e0 [file] [log] [blame]
Barath Kumar R403234e2020-07-07 15:48:58 +05301/*
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 */
21import { Component, Injector, Input, OnInit } from '@angular/core';
22import { FormBuilder, FormGroup, Validators } from '@angular/forms';
23import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
24import { TranslateService } from '@ngx-translate/core';
25import { NotifierService } from 'angular-notifier';
26import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, OSMREPO_TYPES, TYPESECTION } from 'CommonModel';
27import { environment } from 'environment';
28import { OSMRepoDetails } from 'OsmRepoModel';
29import { RestService } from 'RestService';
30import { 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})
40export 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 }
Barath Kumar R403234e2020-07-07 15:48:58 +0530142 const modalData: MODALCLOSERESPONSEDATA = {
143 message: 'Done'
144 };
145 if (this.getCreateupdateType === 'Add') {
146 const apiURLHeader: APIURLHEADER = {
147 url: environment.OSMREPOS_URL
148 };
149 this.addOsmRepo(apiURLHeader, modalData);
150 } else if (this.getCreateupdateType === 'Edit') {
151 const apiURLHeader: APIURLHEADER = {
152 url: environment.OSMREPOS_URL + '/' + this.osmrepoid
153 };
154 this.editOsmRepo(apiURLHeader, modalData);
155 } else {
156 this.notifierService.notify('error', this.translateService.instant('ERROR'));
157 }
158 }
159
160 /** This function used to add the osm repo @public */
161 public addOsmRepo(apiURLHeader: APIURLHEADER, modalData: MODALCLOSERESPONSEDATA): void {
SANDHYA.JS382448f2023-10-12 14:31:00 +0530162 this.isLoadingResults = true;
Barath Kumar R403234e2020-07-07 15:48:58 +0530163 this.restService.postResource(apiURLHeader, this.osmrepoForm.value).subscribe(() => {
SANDHYA.JS382448f2023-10-12 14:31:00 +0530164 this.isLoadingResults = false;
Barath Kumar R403234e2020-07-07 15:48:58 +0530165 this.activeModal.close(modalData);
166 this.notifierService.notify('success', this.translateService.instant('PAGE.OSMREPO.CREATEDSUCCESSFULLY'));
Barath Kumar R403234e2020-07-07 15:48:58 +0530167 }, (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 {
SANDHYA.JS382448f2023-10-12 14:31:00 +0530175 if (!this.osmrepoForm.dirty) {
176 this.notifierService.notify('warning', this.translateService.instant('PAGE.TOPOLOGY.DATAEMPTY'));
177 return;
178 }
179 this.isLoadingResults = true;
Barath Kumar R403234e2020-07-07 15:48:58 +0530180 this.restService.patchResource(apiURLHeader, this.osmrepoForm.value).subscribe(() => {
Barath Kumar R403234e2020-07-07 15:48:58 +0530181 this.isLoadingResults = false;
SANDHYA.JS382448f2023-10-12 14:31:00 +0530182 this.notifierService.notify('success', this.translateService.instant('PAGE.OSMREPO.UPDATEDSUCCESSFULLY'));
183 this.activeModal.close(modalData);
Barath Kumar R403234e2020-07-07 15:48:58 +0530184 }, (error: ERRORDATA) => {
185 this.isLoadingResults = false;
186 this.restService.handleError(error, 'patch');
187 });
188 }
189}