mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 21:29:38 +02:00
* Fix AND prefix on rule form This new condition prefix data target is used to ensure the AND prefix is added/removed to additional conditions/groups when there aren't any saved conditions yet. * Ensure the condition group "Add condition" button is type button to avoid form submission * Add prefix update when removing a subcondition * Use data condition to update the prefix on conditions, condition groups, and subconditions * Use condition remove instead of element remove for condition groups so prefix logic runs * Add back explicit show_prefixes to ensure subconditions never have a prefix * Run the prefix update runs on a load of a form, which handles prefixes on an edit since no conditions change * Ensure saved items that are marked for removal don't impact the index * Simplify logic since we don't process subconditions * Clean up comments * Add primary_condition_title field to rule model
78 lines
2 KiB
JavaScript
78 lines
2 KiB
JavaScript
import { Controller } from "@hotwired/stimulus";
|
|
|
|
// Connects to data-controller="rules"
|
|
export default class extends Controller {
|
|
static targets = [
|
|
"conditionTemplate",
|
|
"conditionGroupTemplate",
|
|
"actionTemplate",
|
|
"conditionsList",
|
|
"actionsList",
|
|
"effectiveDateInput",
|
|
];
|
|
|
|
connect() {
|
|
// Update condition prefixes on first connection (form render on edit)
|
|
this.updateConditionPrefixes();
|
|
}
|
|
|
|
addConditionGroup() {
|
|
this.#appendTemplate(
|
|
this.conditionGroupTemplateTarget,
|
|
this.conditionsListTarget,
|
|
);
|
|
this.updateConditionPrefixes();
|
|
}
|
|
|
|
addCondition() {
|
|
this.#appendTemplate(
|
|
this.conditionTemplateTarget,
|
|
this.conditionsListTarget,
|
|
);
|
|
this.updateConditionPrefixes();
|
|
}
|
|
|
|
addAction() {
|
|
this.#appendTemplate(this.actionTemplateTarget, this.actionsListTarget);
|
|
}
|
|
|
|
clearEffectiveDate() {
|
|
this.effectiveDateInputTarget.value = "";
|
|
}
|
|
|
|
#appendTemplate(templateEl, listEl) {
|
|
const html = templateEl.innerHTML.replaceAll(
|
|
"IDX_PLACEHOLDER",
|
|
this.#uniqueKey(),
|
|
);
|
|
|
|
listEl.insertAdjacentHTML("beforeend", html);
|
|
}
|
|
|
|
#uniqueKey() {
|
|
return Date.now();
|
|
}
|
|
|
|
// Updates the prefix visibility of all conditions and condition groups
|
|
// This is also called by the rule/conditions_controller when a subcondition is removed
|
|
updateConditionPrefixes() {
|
|
const conditions = Array.from(this.conditionsListTarget.children);
|
|
let conditionIndex = 0;
|
|
|
|
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 (!condition.classList.contains('hidden')) {
|
|
const prefixEl = condition.querySelector('[data-condition-prefix]');
|
|
if (prefixEl) {
|
|
if (conditionIndex === 0) {
|
|
prefixEl.classList.add('hidden');
|
|
} else {
|
|
prefixEl.classList.remove('hidden');
|
|
}
|
|
conditionIndex++;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|