Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
9.4 KB
Raw Blame
<%= turbo_frame_tag "category_dropdown" do %>
  <%# Creating is only offered where the category can then be assigned: a user who %>
  <%# can't annotate this account would get a new category the assignment then rejects. %>
  <% can_create = can_annotate_account?(@transaction.entry.account) && !Current.family.categories_locked? && !Current.user&.guest? %>
  <div class="flex flex-col relative"
       data-controller="list-filter category-quick-create"
       data-category-quick-create-create-url-value="<%= categories_path(format: :json) %>"
       data-category-quick-create-color-value="<%= Category::COLORS.sample %>"
       data-category-quick-create-label-template-value="<%= t(".create", name: "__NAME__") %>"
       data-category-quick-create-error-message-value="<%= t(".create_error") %>"
       data-category-quick-create-assign-error-message-value="<%= t(".assign_error") %>"
       data-category-quick-create-parent-picker-label-value="<%= t(".choose_parent", name: "__NAME__") %>"
       data-category-quick-create-existing-names-value="<%= @categories.map(&:name).to_json %>">
    <div class="grow p-1.5">
      <div class="relative flex items-center bg-container border border-secondary rounded-lg">
        <input
          placeholder="<%= t(".search_placeholder") %>"
          autocomplete="nope"
          type="search"
          role="combobox"
          aria-label="<%= t(".search_placeholder") %>"
          aria-expanded="false"
          aria-autocomplete="list"
          class="bg-container placeholder:text-sm placeholder:text-secondary font-normal h-10 relative pl-10 w-full border-none rounded-lg focus:outline-hidden focus:ring-0"
          data-list-filter-target="input"
          data-category-quick-create-target="input"
          data-action="list-filter#filter category-quick-create#update keydown->list-filter#handleKeydown keydown.enter->category-quick-create#createOnEnter">
          <%= icon("search", class: "absolute inset-0 ml-2 transform top-1/2 -translate-y-1/2") %>
        </div>
      </div>
      <% parent_options = can_create ? @categories.select { |category| category.parent_id.nil? }.sort_by { |category| category.name.downcase } : [] %>
      <div data-list-filter-target="list" data-category-quick-create-target="list" role="listbox" class="flex flex-col gap-0.5 p-1.5 mt-0.5 mr-2 max-h-64 overflow-y-scroll scrollbar">
        <div class="pb-2 pl-4 mr-2 text-secondary hidden" data-list-filter-target="emptyMessage">
          <%= t(".no_categories") %>
        </div>
        <% if can_create %>
          <%# Shown by category-quick-create when the search matches no existing category name. %>
          <%# Creating goes through the same update form as picking a row, so the list row %>
          <%# refreshes and the "create a rule?" prompt still appears. %>
          <%= form_with url: transaction_category_path(@transaction.entry, grouped: params[:grouped]),
                        method: :patch,
                        data: { turbo_frame: "category_dropdown", category_quick_create_target: "assignForm" } do |f| %>
            <%= hidden_field_tag "entry[entryable_type]", "Transaction", id: nil %>
            <%= hidden_field_tag "entry[entryable_attributes][id]", @transaction.id, id: nil %>
            <%= hidden_field_tag "entry[entryable_attributes][category_id]", nil, id: nil, data: { category_quick_create_target: "categoryIdField" } %>
          <% end %>
          <%= render DS::ActionRow.new(
                icon: "plus",
                hidden: true,
                data: { category_quick_create_target: "createButton", action: "category-quick-create#create" }
              ) do %>
            <span data-category-quick-create-target="createLabel"></span>
          <% end %>
          <% if parent_options.any? %>
            <%= render DS::ActionRow.new(
                  text: t(".create_as_subcategory"),
                  icon: "corner-down-right",
                  tone: :secondary,
                  hidden: true,
                  data: { category_quick_create_target: "createAsSubcategory", action: "category-quick-create#showParentPicker" }
                ) %>
          <% end %>
          <p class="hidden px-2 pb-1 text-xs text-destructive" role="alert" data-category-quick-create-target="error"></p>
        <% end %>
        <% if @recent_categories.any? %>
          <div data-list-filter-target="recentSection">
            <p class="pl-2 pb-1 text-xs font-medium text-secondary uppercase tracking-wide"><%= t(".recent") %></p>
            <% @recent_categories.each do |category| %>
              <%= render "category/dropdowns/row", category: category, id_prefix: "recent_category_option" %>
            <% end %>
            <%= render "shared/ruler", classes: "my-2" %>
          </div>
        <% end %>
        <% if @categories.any? %>
          <% Category::Group.for(@categories).each do |group| %>
            <%= render "category/dropdowns/row", category: group.category %>

            <% group.subcategories.each do |category| %>
              <%= render "category/dropdowns/row", category: category %>
            <% end %>
          <% end %>
        <% else %>
          <div class="flex justify-center items-center py-12">
            <div class="text-center flex flex-col items-center max-w-[500px]">
              <p class="text-sm text-secondary font-normal mb-4"><%= t(".empty") %></p>

              <% unless Current.family.categories_locked? || Current.user&.guest? %>
                <%= render DS::Button.new(
                text: t(".bootstrap"),
                variant: "outline",
                href: bootstrap_categories_path,
                method: :post,
                data: { turbo_frame: :_top }) %>
              <% end %>
            </div>
          </div>
        <% end %>
      </div>

      <%# Shown in place of the list after "Add as a subcategory…". Only top-level %>
      <%# categories can be parents (categories are at most two levels deep). %>
      <% if parent_options.any? %>
        <div class="hidden flex-col gap-0.5 p-1.5 mt-0.5 mr-2 max-h-64 overflow-y-scroll scrollbar"
             data-category-quick-create-target="parentPicker">
          <p class="px-2 pb-1 text-xs text-secondary" data-category-quick-create-target="parentPickerLabel"></p>
          <% parent_options.each do |parent| %>
            <%= render DS::ActionRow.new(tone: :secondary, data: { parent_id: parent.id, action: "category-quick-create#createUnderParent" }) do %>
              <%= render partial: "categories/badge", locals: { category: parent } %>
            <% end %>
          <% end %>
          <%= render DS::ActionRow.new(
                text: t(".back"),
                icon: "arrow-left",
                tone: :secondary,
                data: { action: "category-quick-create#hideParentPicker" }
              ) %>
        </div>
      <% end %>

      <%= render "shared/ruler", classes: "my-2" %>

      <div class="relative p-1.5 w-full">
        <% if @transaction.category %>
          <%= button_to transaction_path(@transaction.entry),
                      method: :patch,
                      data: { turbo_frame: dom_id(@transaction.entry) },
                      params: { grouped: params[:grouped], entry: { entryable_type: "Transaction", entryable_attributes: { id: @transaction.id, category_id: nil } } },
                      class: "flex text-sm font-medium items-center gap-2 text-secondary w-full rounded-lg p-2 hover:bg-container-inset-hover" do %>
            <%= icon("minus") %>

            <%= t(".clear") %>
          <% end %>
        <% end %>

        <% unless @transaction.transfer? %>
          <%= link_to new_transaction_transfer_match_path(@transaction.entry),
                  class: "flex text-sm font-medium items-center gap-2 text-secondary w-full rounded-lg p-2 hover:bg-container-inset-hover",
                  data: { turbo_frame: "modal" } do %>
            <%= icon("refresh-cw") %>

            <p><%= t(".match_transfer") %></p>
          <% end %>
        <% end %>

        <% if @transaction.splittable? %>
          <%= link_to new_transaction_split_path(@transaction.entry),
                  class: "flex text-sm font-medium items-center gap-2 text-secondary w-full rounded-lg p-2 hover:bg-container-inset-hover",
                  data: { turbo_frame: "modal" } do %>
            <%= icon("split") %>

            <p><%= t("splits.show.button") %></p>
          <% end %>
        <% end %>

        <div class="flex text-sm font-medium items-center gap-2 text-secondary w-full rounded-lg p-2">
          <div class="flex items-center gap-2">
            <%= form_with url: transaction_path(@transaction.entry),
                        method: :patch,
                        data: { controller: "auto-submit-form" } do |f| %>
              <%= f.hidden_field "entry[excluded]", value: !@transaction.entry.excluded %>
              <%= hidden_field_tag :grouped, "true" if params[:grouped] == "true" %>
              <%= f.check_box "entry[excluded]",
                          checked: @transaction.entry.excluded,
                          class: "checkbox checkbox--light",
                          data: { auto_submit_form_target: "auto", autosubmit_trigger_event: "change" } %>
            <% end %>
          </div>

          <p><%= t(".one_time", type: @transaction.entry.amount.negative? ? t(".income") : t(".expense")) %></p>

          <span class="text-orange-500 ml-auto">
            <%= icon("asterisk", color: "current") %>
          </span>
        </div>
      </div>
    </div>
  <% end %>