From 9c0de99ef4d5cf5db337b1969523b36a9d212a2a Mon Sep 17 00:00:00 2001 From: cv5ch <176032962+cv5ch@users.noreply.github.com> Date: Fri, 22 Aug 2025 13:47:59 +0200 Subject: [PATCH] Only one success message when updating configs --- .../simple-forms/formconfig.component.ts | 92 ++++++++++--------- 1 file changed, 51 insertions(+), 41 deletions(-) diff --git a/src/app/core/_components/forms/simple-forms/formconfig.component.ts b/src/app/core/_components/forms/simple-forms/formconfig.component.ts index b8a1e9a0..28bd7119 100644 --- a/src/app/core/_components/forms/simple-forms/formconfig.component.ts +++ b/src/app/core/_components/forms/simple-forms/formconfig.component.ts @@ -1,4 +1,4 @@ -import { Subscription } from 'rxjs'; +import { Subscription, forkJoin } from 'rxjs'; import { Component, OnDestroy, OnInit } from '@angular/core'; import { FormGroup } from '@angular/forms'; @@ -170,49 +170,59 @@ export class FormConfigComponent implements OnInit, OnDestroy { } /** - * Handles the submission of the form. - * @param form - The form object containing the updated values. + * Handles the submission of the form using parallel requests for all changed fields. + * Shows a single success message when all updates complete. + * @param formValues - The current form values emitted from the dynamic form. */ - onFormSubmit(form: FormGroup) { - const currentFormValues = form; - const initialFormValues = this.formValues; - const changedFields: Record = {}; - - for (const key in currentFormValues) { - if (Object.prototype.hasOwnProperty.call(currentFormValues, key)) { - const currentValue = currentFormValues[key]; - const initialValue = initialFormValues[key]; - - if (currentValue !== initialValue) { - // Convert boolean values to 1 (true) or 0 (false) - changedFields[key] = typeof currentValue === 'boolean' ? (currentValue ? 1 : 0) : currentValue; - } + onFormSubmit(formValues: Record) { + if (!formValues) return; + + const changedFields = this.getChangedFields(formValues); + + if (Object.keys(changedFields).length === 0) return; + + // Prepare all update observables + const updateRequests = Object.keys(changedFields).map((key) => { + const id = this.formIds[key]; + const value = changedFields[key]; + return this.gs.update(SERV.CONFIGS, id, { item: key, value: String(value) }); + }); + + // Execute all updates in parallel + this.mySubscription = forkJoin(updateRequests).subscribe({ + next: () => { + // Mark all fields as updated + Object.keys(changedFields).forEach((key) => this.uicService.onUpdatingCheck(key)); + + // Show a single success message + this.alert.showSuccessMessage(`Saved ${this.title}`); + }, + error: (err) => { + console.error(`Error updating ${this.title}`, err); + this.alert.showErrorMessage(`Failed to save ${this.title}`); } - } - - const fieldKeys = Object.keys(changedFields); - let index = 0; - - const showAlertsSequentially = () => { - if (index < fieldKeys.length) { - const key = fieldKeys[index]; - const id = this.formIds[key]; - const valueUpdate = changedFields[key]; - const arr = { item: key, value: String(valueUpdate) }; - - this.mySubscription = this.gs.update(SERV.CONFIGS, id, arr).subscribe(() => { - this.uicService.onUpdatingCheck(key); - this.alert.showSuccessMessage(`Saved ${key}`); - - // Delay showing the next alert by 2000 milliseconds (2 seconds) - setTimeout(() => { - index++; - showAlertsSequentially(); - }, 2000); - }); + }); + } + + /** + * Collects all fields that have changed compared to the initial form values. + * Converts boolean values to 1 (true) or 0 (false) for backend compatibility. + * + * @param currentValues The current values from the form. + * @returns An object where keys are the changed field names and values are the updated values. + */ + private getChangedFields(currentValues: Record): Record { + const initialValues = this.formValues; + + return Object.keys(currentValues).reduce>((changedFields, key) => { + const initialValue = initialValues[key]; + const currentValue = currentValues[key]; + + if (currentValue !== initialValue) { + changedFields[key] = typeof currentValue === 'boolean' ? (currentValue ? 1 : 0) : currentValue; } - }; - showAlertsSequentially(); + return changedFields; + }, {}); } }