diff --git a/src/app/products/saving-products/create-saving-product/create-saving-product.component.html b/src/app/products/saving-products/create-saving-product/create-saving-product.component.html index 867619fa8..f60a295f3 100644 --- a/src/app/products/saving-products/create-saving-product/create-saving-product.component.html +++ b/src/app/products/saving-products/create-saving-product/create-saving-product.component.html @@ -144,7 +144,7 @@ } @if (savingProductFormValid && selectedTabIndex === totalTabs - 1) { - diff --git a/src/app/products/saving-products/create-saving-product/create-saving-product.component.ts b/src/app/products/saving-products/create-saving-product/create-saving-product.component.ts index 942b0f195..7207b8d78 100644 --- a/src/app/products/saving-products/create-saving-product/create-saving-product.component.ts +++ b/src/app/products/saving-products/create-saving-product/create-saving-product.component.ts @@ -70,6 +70,10 @@ export class CreateSavingProductComponent { savingProductsTemplate: any; accountingRuleData: string[] = []; selectedTabIndex = 0; + /** Guards against repeat clicks on Submit: the product name is unique in the database, so a + * second request for the same form fails with a duplicate key error even though the first one + * created the product. */ + submitting = false; /** * @param {ActivatedRoute} route Activated Route. @@ -162,8 +166,11 @@ export class CreateSavingProductComponent { } submit() { + if (this.submitting) { + return; + } + this.submitting = true; const savingProduct = this.prepareSavingProductPayload(); - console.log('Saving product submit payload:', savingProduct); this.productsService.createSavingProduct(savingProduct).subscribe({ next: (response: any) => { if (response?.rollbackTransaction) { @@ -181,6 +188,11 @@ export class CreateSavingProductComponent { { relativeTo: this.route } ); } + }, + error: () => { + // the error itself is reported by the global error handler; just let the user correct the + // form and try again + this.submitting = false; } }); }