mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-09 15:35:22 +02:00
Use data condition to update the prefix on conditions, condition groups, and subconditions
This commit is contained in:
parent
559acbaa50
commit
849e820225
3 changed files with 37 additions and 26 deletions
|
@ -48,13 +48,33 @@ export default class extends Controller {
|
|||
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 items = this.conditionsListTarget.querySelectorAll('[data-condition-prefix]');
|
||||
items.forEach((el, idx) => {
|
||||
if (idx === 0) {
|
||||
el.classList.add('hidden');
|
||||
} else {
|
||||
el.classList.remove('hidden');
|
||||
// Update conditions
|
||||
this.#updatePrefixesForList(this.conditionsListTarget);
|
||||
|
||||
// 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);
|
||||
});
|
||||
}
|
||||
|
||||
// Helper to update prefixes for a given list
|
||||
#updatePrefixesForList(listEl) {
|
||||
const items = Array.from(listEl.children);
|
||||
let conditionIdx = 0;
|
||||
items.forEach((item) => {
|
||||
const prefixEl = item.querySelector('[data-condition-prefix]');
|
||||
if (prefixEl) {
|
||||
if (conditionIdx === 0) {
|
||||
prefixEl.classList.add('hidden');
|
||||
} else {
|
||||
prefixEl.classList.remove('hidden');
|
||||
}
|
||||
conditionIdx++;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,18 +1,14 @@
|
|||
<%# locals: (form:, show_prefix: true) %>
|
||||
<%# locals: (form:) %>
|
||||
|
||||
<% condition = form.object %>
|
||||
<% rule = condition.rule %>
|
||||
|
||||
<li data-controller="rule--conditions" data-rule--conditions-condition-filters-value="<%= rule.condition_filters.to_json %>" class="flex items-center gap-3">
|
||||
<% if form.index.to_i > 0 && show_prefix %>
|
||||
<div class="pl-4">
|
||||
<span class="font-medium uppercase text-xs" data-condition-prefix>and</span>
|
||||
</div>
|
||||
<% else %>
|
||||
<div class="pl-4">
|
||||
<span class="font-medium uppercase text-xs hidden" data-condition-prefix>and</span>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%# Show prefix on conditions, except the first one %>
|
||||
<div class="pl-2" data-condition-prefix>
|
||||
<span class="font-medium uppercase text-xs">and</span>
|
||||
</div>
|
||||
<div class="grow flex gap-2 items-center h-full">
|
||||
<%= form.hidden_field :_destroy, value: false, data: { rule__conditions_target: "destroyField" } %>
|
||||
|
||||
|
|
|
@ -9,15 +9,10 @@
|
|||
|
||||
<div class="flex items-center justify-between gap-2">
|
||||
<div class="flex items-center gap-2">
|
||||
<% unless form.index == 0 %>
|
||||
<div class="pl-2">
|
||||
<span class="font-medium uppercase text-xs" data-condition-prefix>and</span>
|
||||
</div>
|
||||
<% else %>
|
||||
<div class="pl-2">
|
||||
<span class="font-medium uppercase text-xs hidden" data-condition-prefix>and</span>
|
||||
</div>
|
||||
<% end %>
|
||||
<%# Show prefix on condition groups, except the first one %>
|
||||
<div class="pl-2" data-condition-prefix>
|
||||
<span class="font-medium uppercase text-xs">and</span>
|
||||
</div>
|
||||
<p class="text-sm text-secondary">match</p>
|
||||
<%= form.select :operator, [["all", "and"], ["any", "or"]], { container_class: "w-fit" }, data: { rules_target: "operatorField" } %>
|
||||
<p class="text-sm text-secondary">of the following conditions</p>
|
||||
|
@ -40,7 +35,7 @@
|
|||
|
||||
<ul data-rule--conditions-target="subConditionsList" class="space-y-3">
|
||||
<%= form.fields_for :sub_conditions do |scf| %>
|
||||
<%= render "rule/conditions/condition", form: scf, show_prefix: false %>
|
||||
<%= render "rule/conditions/condition", form: scf %>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue