Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
3.4 KB
Raw Blame
<%# locals: (form:) %>

<% action = form.object %>
<% rule = action.rule %>

<li data-controller="rule--actions" data-rule--actions-action-executors-value="<%= rule.action_executors.to_json %>" class="<%= class_names("flex gap-3", action.executor.type == "split" ? "items-start" : "items-center") %>">
  <%= form.hidden_field :_destroy, value: false, data: { rule__actions_target: "destroyField" } %>

  <div class="grid grid-cols-1 gap-2 items-center h-full w-full">
    <div class="grow">
      <%= form.select :action_type, rule.action_executors.map { |executor| [ executor.label, executor.key ] }, {}, data: { action: "rule--actions#handleActionTypeChange" } %>
    </div>

    <%
      action_value_classes = if action.executor.type == "split"
        "w-full"
      else
        "min-w-1/2 flex items-center gap-2"
      end
    %>
    <%= tag.div class: class_names(action_value_classes, { "hidden": action.executor.type == "function" }),
                data: { rule__actions_target: "actionValue" } do %>
      <%# Initial rendering based on the action executor type. %>
      <%# Subsequent renders are injected by the Stimulus controller, which uses the templates from below. %>
      <% unless action.executor.type == "split" %>
        <span class="font-medium text-primary uppercase text-xs"><%= t("rules.actions.to_label") %></span>
      <% end %>
      <% if action.executor.type == "select" %>
        <%= form.select :value, action.options || [], {} %>
      <% elsif action.executor.type == "multi_select" %>
        <%= render DS::TagSelect.new(
              form: form,
              tags: rule.family.tags.alphabetically,
              selected_ids: action.value.to_s.split(","),
              attribute: :value,
              show_label: false
            ) %>
      <% elsif action.executor.type == "text" %>
        <%= form.text_field :value, placeholder: t("rules.actions.value_placeholder") %>
      <% elsif action.executor.type == "split" %>
        <%= render "rule/actions/split_builder", form: form, action: action %>
      <% end %>
    <% end %>
  </div>

  <%= icon(
    "trash-2",
    size: "sm",
    as_button: true,
    data: { action: "rule--actions#remove", rule__actions_destroy_param: action.persisted? }) %>

  <%# Templates for different input types - these will be cloned and used by the Stimulus controller %>
  <template data-rule--actions-target="selectTemplate">
    <span class="font-medium text-primary uppercase text-xs"><%= t("rules.actions.to_label") %></span>
    <%= form.select :value, [], {} %>
  </template>

  <template data-rule--actions-target="multiSelectTemplate">
    <span class="font-medium text-primary uppercase text-xs"><%= t("rules.actions.to_label") %></span>
    <%= render DS::TagSelect.new(
          form: form,
          tags: rule.family.tags.alphabetically,
          selected_ids: [],
          attribute: :value,
          show_label: false
        ) %>
  </template>

  <template data-rule--actions-target="textTemplate">
    <span class="font-medium text-primary uppercase text-xs"><%= t("rules.actions.to_label") %></span>
    <%= form.text_field :value, placeholder: t("rules.actions.value_placeholder") %>
  </template>

  <template data-rule--actions-target="splitTemplate">
    <%= render "rule/actions/split_builder", form: form, action: action, blank: true %>
  </template>

  <%# The function type doesn't need an input, so no template is required.%>
</li>