|
1 |
| -import {Component, Input, OnInit, TemplateRef, ViewChild} from '@angular/core'; |
| 1 | +import {ChangeDetectorRef, Component, Input, NgZone, OnInit, TemplateRef, ViewChild} from '@angular/core'; |
2 | 2 | import { LocalDataSource } from 'ng2-smart-table';
|
3 | 3 | import { NbDialogService, NbToastrService } from '@nebular/theme';
|
4 | 4 | import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
|
@@ -30,11 +30,15 @@ export class ManageAssetsComponent implements OnInit {
|
30 | 30 | editForm: FormGroup;
|
31 | 31 | _entityId: any;
|
32 | 32 | selectedAsset: any; // To store the selected asset for editing
|
| 33 | + isSubmitting = false; |
| 34 | + showSpinner = false; |
| 35 | + |
33 | 36 |
|
34 | 37 | data: ProjectAsset[] = []; // Initialize as an empty array
|
35 | 38 |
|
36 | 39 | constructor(private dialogService: NbDialogService, private toastrService: NbToastrService, private fb: FormBuilder,
|
37 |
| - private router: Router, private assetService: AssetService, private _route: ActivatedRoute) { |
| 40 | + private router: Router, private assetService: AssetService, private _route: ActivatedRoute, |
| 41 | + private cdr: ChangeDetectorRef) { |
38 | 42 | this._entityId = +this._route.snapshot.paramMap.get('projectid');
|
39 | 43 | if (!this._entityId) {
|
40 | 44 | this.router.navigate(['/pages/dashboard']);
|
@@ -185,18 +189,35 @@ export class ManageAssetsComponent implements OnInit {
|
185 | 189 | }
|
186 | 190 | }
|
187 | 191 | onSubmit(ref) {
|
188 |
| - return this.assetService.saveAsset(this._entityId, this.assetForm.value).subscribe(() => { |
| 192 | + |
| 193 | + // Disable the submit button and show the spinner |
| 194 | + this.isSubmitting = true; |
| 195 | + this.showSpinner = true; |
| 196 | + this.cdr.detectChanges(); // Manually trigger change detection |
| 197 | + |
| 198 | + // Make the request |
| 199 | + this.assetService.saveAsset(this._entityId, this.assetForm.value).subscribe( |
| 200 | + () => { |
189 | 201 | this.toastrService.show('Asset created', 'Success', { status: 'success' });
|
190 |
| - this.clearForm(); |
191 | 202 | this.loadAssets();
|
192 | 203 | ref.close();
|
193 | 204 | },
|
194 |
| - () => { |
| 205 | + (error) => { |
195 | 206 | this.toastrService.show('Asset not created, check all fields', 'Failure', { status: 'danger' });
|
196 |
| - }); |
197 |
| - ref.close(); |
| 207 | + // Handle the error case here |
| 208 | + }, |
| 209 | + () => { |
| 210 | + // Reset the submit button and hide the spinner once the request is complete |
| 211 | + this.isSubmitting = false; |
| 212 | + this.showSpinner = false; |
| 213 | + this.loadAssets(); |
| 214 | + ref.close(); |
| 215 | + }, |
| 216 | + ); |
198 | 217 | }
|
199 | 218 |
|
| 219 | + |
| 220 | + |
200 | 221 | onSaveEdit(ref) {
|
201 | 222 | if (this.editForm.valid) {
|
202 | 223 | // Update the data source with the edited values
|
|
0 commit comments