Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
5.7 KB
Raw Blame
<%# locals: (rule:) %>

<%= styled_form_with model: rule, class: "space-y-6",
                     data: { controller: "rules", rule_registry_value: rule.registry.to_json } do |f| %>

  <%= f.hidden_field :resource_type, value: rule.resource_type %>

  <% if @rule.errors.any? %>
    <%= render "shared/form_errors", model: @rule %>
  <% end %>

  <section class="space-y-4">
    <div class="flex items-center gap-1 text-secondary">
      <%= icon "tag", size: "sm" %>
      <h3 class="text-sm font-medium text-primary"><%= t(".rule_name_label") %></h3>
    </div>
    <div class="ml-6">
      <%= f.text_field :name, placeholder: t(".rule_name_placeholder"), class: "form-field__input" %>
    </div>
  </section>

  <section class="space-y-4">
    <div class="flex items-center gap-1 text-secondary">
      <%= icon "folder", size: "sm" %>
      <h3 class="text-sm font-medium text-primary"><%= t(".group_label") %></h3>
    </div>
    <div class="ml-6 grid grid-cols-1 sm:grid-cols-2 gap-2">
      <%= f.select :rule_group_id,
            rule.family.rule_groups.alphabetically.map { |group| [ group.name, group.id ] },
            { include_blank: t(".no_group"), label: t(".group_select_label") } %>
      <%= f.text_field :new_rule_group_name, label: t(".new_group_label"), placeholder: t(".new_group_placeholder") %>
    </div>
    <p class="ml-6 text-xs text-secondary"><%= t(".group_hint") %></p>
  </section>

  <section class="space-y-4">
    <div class="flex items-center gap-1 text-secondary">
      <h3 class="text-sm font-medium text-primary">IF</h3>
    </div>

    <%# Condition Group template, used by Stimulus controller to add new conditions dynamically %>
    <template data-rules-target="conditionGroupTemplate">
      <%= f.fields_for :conditions, Rule::Condition.new(rule: rule, condition_type: "compound", operator: "and"), child_index: "IDX_PLACEHOLDER" do |cf| %>
        <%= render "rule/conditions/condition_group", form: cf %>
      <% end %>
    </template>

    <%# Condition template, used by Stimulus controller to add new conditions dynamically %>
    <template data-rules-target="conditionTemplate">
      <%= f.fields_for :conditions, Rule::Condition.new(rule: rule, condition_type: rule.condition_filters.first.key), child_index: "IDX_PLACEHOLDER" do |cf| %>
        <%= render "rule/conditions/condition", form: cf %>
      <% end %>
    </template>

    <div class="ml-6 space-y-4">
      <ul data-rules-target="conditionsList" class="space-y-3">
        <%= f.fields_for :conditions do |cf| %>
          <% if cf.object.compound? %>
            <%= render "rule/conditions/condition_group", form: cf %>
          <% else %>
            <%= render "rule/conditions/condition", form: cf %>
          <% end %>
        <% end %>
      </ul>

      <div class="flex items-center gap-2">
        <%= render DS::Button.new(text: t(".add_condition"), icon: "plus", variant: "ghost", type: "button", data: { action: "rules#addCondition" }) %>
        <%= render DS::Button.new(text: t(".add_condition_group"), icon: "copy-plus", variant: "ghost", type: "button", data: { action: "rules#addConditionGroup" }) %>
      </div>
    </div>
  </section>

  <section class="space-y-4">
    <div class="flex items-center gap-1 text-secondary">
      <h3 class="text-sm font-medium text-primary"><%= t(".then") %></h3>
    </div>

    <%# Action template, used by Stimulus controller to add new actions dynamically %>
    <template data-rules-target="actionTemplate">
      <%= f.fields_for :actions, Rule::Action.new(rule: rule, action_type: rule.action_executors.first.key), child_index: "IDX_PLACEHOLDER" do |af| %>
        <%= render "rule/actions/action", form: af %>
      <% end %>
    </template>

    <div class="ml-6 space-y-4">
      <ul data-rules-target="actionsList" class="space-y-3">
        <%= f.fields_for :actions do |af| %>
          <%= render "rule/actions/action", form: af %>
        <% end %>
      </ul>

      <%= render DS::Button.new(text: t(".add_action"), icon: "plus", variant: "ghost", type: "button", data: { action: "rules#addAction" }) %>
    </div>
  </section>

  <section class="space-y-4">
    <div class="flex items-center gap-1 text-secondary">
      <h3 class="text-sm font-medium text-primary">FOR</h3>
    </div>

    <div class="ml-6 space-y-3">
      <div class="flex items-center gap-2">
        <%= f.radio_button :effective_date_enabled, false, checked: rule.effective_date.nil?, data: { action: "rules#clearEffectiveDate" } %>
        <%= f.label :effective_date_enabled_false, t(".all_past_and_future", resource: rule.resource_type.pluralize), class: "text-sm text-primary" %>
      </div>

      <div class="flex items-center gap-2">
        <div class="flex items-center gap-2">
          <%= f.radio_button :effective_date_enabled, true, checked: rule.effective_date.present? %>
          <%= f.label :effective_date_enabled_true, t(".starting_from"), class: "text-sm text-primary" %>
        </div>

        <%= f.date_field :effective_date, container_class: "w-fit", data: { rules_target: "effectiveDateInput" } %>
      </div>
    </div>
  </section>

  <section class="flex items-center gap-4 justify-between">
    <div class="text-sm space-y-1">
      <h3 class="font-medium text-primary"><%= t(".stop_processing_label") %></h3>
      <p class="text-secondary"><%= t(".stop_processing_description") %></p>
    </div>
    <%= f.toggle :stop_processing %>
  </section>

  <section class="flex items-center gap-4 justify-between">
    <div class="text-sm space-y-1">
      <h3 class="font-medium text-primary"><%= t(".apply_immediately_label") %></h3>
      <p class="text-secondary"><%= t(".apply_immediately_description") %></p>
    </div>
    <%= f.toggle :apply_immediately %>
  </section>

  <%= f.submit %>
<% end %>