Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
6.6 KB
Raw Blame
<%# locals: (entry:, account_currencies:, manual_accounts:, categories:, merchants:, tags:) %>
<% auto_generate_names = Current.family.auto_generate_transaction_names? %>

<%= styled_form_with model: entry, url: transactions_path, class: "space-y-4", data: { controller: auto_generate_names ? "transaction-form transaction-name-preview category-suggestion" : "transaction-form category-suggestion", action: "formdata->category-suggestion#syncName hw-combobox:selection->category-suggestion#onNameSuggestionSelected turbo:before-cache@document->transaction-form#refreshIdempotencyKey pageshow@window->transaction-form#refreshIdempotencyKeyIfPersisted", transaction_form_exchange_rate_url_value: exchange_rate_path, transaction_form_account_currencies_value: account_currencies.to_json } do |f| %>
  <% if entry.errors.any? %>
    <%= render "shared/form_errors", model: entry %>
  <% end %>

  <section data-controller="transaction-type-tabs">
    <%= render "shared/transaction_type_tabs", active_tab: params[:nature] == "inflow" ? "income" : "expense", account_id: params[:account_id] %>

    <%= f.hidden_field :nature, value: params[:nature] || "outflow", data: { "transaction-type-tabs-target": "natureField" } %>
    <%= f.hidden_field :entryable_type, value: "Transaction" %>
    <%# Anti-double-submit token: a fresh UUID per page load, echoed back on
        submit. TransactionsController#create uses it to recognize a repeat
        submission (double-click, retry, or a genuine concurrent race) and
        avoid creating a duplicate transaction.
        transaction_form_controller#refreshIdempotencyKey replaces this value
        on turbo:before-cache (Turbo's own snapshot cache) and on a
        persisted pageshow (the browser's native bfcache), so a page
        restored via either path - browser back, duplicated tab - can't
        replay a token that already committed an entry and silently
        redirect a distinct submission onto that old one. %>
    <%= hidden_field_tag "entry[idempotency_key]", new_transaction_idempotency_key, data: { "transaction-form-target": "idempotencyKey" } %>
  </section>

  <section class="space-y-2">
    <div class="form-field combobox">
      <%= f.combobox :name,
            name_suggestions_transactions_path,
            free_text: true,
            autocomplete: :list,
            label: t(".description"),
            placeholder: auto_generate_names ? t(".description_placeholder_optional") : t(".description_placeholder"),
            required: !auto_generate_names,
            input: { data: { transaction_name_preview_target: "name", category_suggestion_target: "name" } } %>
    </div>

    <% if @entry.account_id %>
      <%= f.hidden_field :account_id, data: { transaction_form_target: "account" } %>
    <% else %>
      <%= f.collection_select :account_id, manual_accounts, :id, :name, { prompt: t(".account_prompt"), label: t(".account"), selected: Current.user.default_account_for_transactions&.id, variant: :logo, searchable: true }, required: true, class: "form-field__input text-ellipsis", data: { transaction_form_target: "account", action: "change->transaction-form#checkCurrencyDifference" } %>
    <% end %>

    <%= f.money_field :amount,
      label: t(".amount"),
      required: true,
      container_class: "money-field-wrapper",
      amount_data: { transaction_form_target: "amount", action: "input->transaction-form#onAmountChange" },
      currency_data: { transaction_form_target: "currency", action: "change->transaction-form#onCurrencyChange" } %>

    <div data-category-suggestion-target="categoryField" data-action="change->category-suggestion#onCategorySelected">
      <%= f.fields_for :entryable do |ef| %>
        <%= render DS::CategorySelect.new(
        form: ef,
        categories: categories,
        selected_id: ef.object.category_id,
        blank_label: t(".category_prompt"),
        locked: Current.family.categories_locked?
      ) %>
      <% end %>
    </div>

    <%= f.date_field :date,
      label: t(".date"),
      required: true,
      min: Entry.min_supported_date,
      value: f.object.date || Date.current,
      data: { transaction_form_target: "date", action: "change->transaction-form#checkCurrencyDifference" } %>

    <% convert_input = capture do %>
      <%= f.fields_for :entryable do |ef| %>
        <%= ef.number_field :exchange_rate,
          label: t("shared.exchange_rate_tabs.exchange_rate"),
          min: "0.00000000000001",
          step: "0.00000000000001",
          placeholder: "1.0",
          class: "form-field__input",
          data: {
            transaction_form_target: "exchangeRateField",
            action: "input->transaction-form#onConvertExchangeRateChange"
          } %>
      <% end %>
    <% end %>

    <% destination_input = capture do %>
      <%= number_field_tag :destination_amount,
        nil,
        id: "transaction_form_destination_amount",
        class: "form-field__input",
        autocomplete: "off",
        min: "0",
        step: "0.00000001",
        placeholder: "92",
        data: {
          transaction_form_target: "destinationAmount",
          action: "input->transaction-form#onCalculateRateDestinationAmountChange"
        } %>
    <% end %>

    <%= render "shared/exchange_rate_tabs",
      controller_id: "transaction-form",
      controller_key: "transaction_form",
      help_text: t("shared.exchange_rate_tabs.exchange_rate_help"),
      convert_tab_label: t("shared.exchange_rate_tabs.convert_tab"),
      calculate_rate_tab_label: t("shared.exchange_rate_tabs.calculate_rate_tab"),
      destination_amount_label: t("shared.exchange_rate_tabs.destination_amount"),
      exchange_rate_label: t("shared.exchange_rate_tabs.exchange_rate"),
      convert_input: convert_input,
      destination_input: destination_input %>
  </section>

  <%= render DS::Disclosure.new(title: t(".details")) do %>
    <section class="space-y-2">
      <%= f.fields_for :entryable do |ef| %>
        <%= render DS::MerchantSelect.new(
              form: ef,
              method: :merchant_id,
              merchants: merchants,
              selected_id: ef.object.merchant_id,
              include_blank: t(".none"),
              label: t(".merchant_label")
            ) %>
        <%= render DS::TagSelect.new(
              form: ef,
              tags: tags,
              selected_ids: ef.object.tag_ids
            ) %>
      <% end %>
      <%= f.text_area :notes,
                      label: t(".note_label"),
                      placeholder: t(".note_placeholder"),
                      rows: 5,
                      "data-auto-submit-form-target": "auto" %>
    </section>
  <% end %>

  <section>
    <%= f.submit t(".submit") %>
  </section>
<% end %>