From 717277c4664df30338a2fac05cbaf4e98a797263 Mon Sep 17 00:00:00 2001 From: hatz Date: Sun, 11 May 2025 12:30:58 -0500 Subject: [PATCH] Simplify logic since we don't process subconditions --- .../controllers/rule/conditions_controller.js | 2 -- .../controllers/rules_controller.js | 28 ++++++------------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/app/javascript/controllers/rule/conditions_controller.js b/app/javascript/controllers/rule/conditions_controller.js index e2e08240..d0c12941 100644 --- a/app/javascript/controllers/rule/conditions_controller.js +++ b/app/javascript/controllers/rule/conditions_controller.js @@ -24,7 +24,6 @@ export default class extends Controller { // Find the parent rules controller before removing the condition const rulesEl = this.element.closest('[data-controller~="rules"]'); - // Remove the condition if (e.params.destroy) { this.destroyFieldTarget.value = true; this.element.classList.add("hidden"); @@ -37,7 +36,6 @@ export default class extends Controller { const rulesController = this.application.getControllerForElementAndIdentifier(rulesEl, "rules"); if (rulesController && typeof rulesController.updateConditionPrefixes === "function") { rulesController.updateConditionPrefixes(); - console.log("updated prefixes") } } } diff --git a/app/javascript/controllers/rules_controller.js b/app/javascript/controllers/rules_controller.js index ac136944..366e0471 100644 --- a/app/javascript/controllers/rules_controller.js +++ b/app/javascript/controllers/rules_controller.js @@ -12,7 +12,7 @@ export default class extends Controller { ]; connect() { - // Update condition prefixes on first connection (form render) + // Update condition prefixes on first connection (form render on edit) this.updateConditionPrefixes(); } @@ -57,32 +57,22 @@ export default class extends Controller { // This is also called by the rule/conditions_controller when a subcondition is removed updateConditionPrefixes() { // Update conditions - this.#updatePrefixesForList(this.conditionsListTarget); + const conditions = Array.from(this.conditionsListTarget.children); - // Update subconditions for each condition group - // We currently only support a single level of subconditions - const groupSubLists = this.conditionsListTarget.querySelectorAll('[data-rule--conditions-target="subConditionsList"]'); - groupSubLists.forEach((subList) => { - this.#updatePrefixesForList(subList); - }); - } + let conditionIndex = 0; - // Helper to update prefixes for a given list - #updatePrefixesForList(listEl) { - const items = Array.from(listEl.children); - let conditionIdx = 0; - items.forEach((item) => { - // Only process visible items, this prevents conditions that are marked for removal and hidden + conditions.forEach((condition) => { + // Only process visible conditions, this prevents conditions that are marked for removal and hidden // from being added to the index. This is important when editing a rule. - if (!item.classList.contains('hidden')) { - const prefixEl = item.querySelector('[data-condition-prefix]'); + if (!condition.classList.contains('hidden')) { + const prefixEl = condition.querySelector('[data-condition-prefix]'); if (prefixEl) { - if (conditionIdx === 0) { + if (conditionIndex === 0) { prefixEl.classList.add('hidden'); } else { prefixEl.classList.remove('hidden'); } - conditionIdx++; + conditionIndex++; } } });