Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
5.0 KB
Raw Blame
<%# locals: (account:, url:, logo: true, turbo: account.persisted?) %>
<%# `logo: false` hides the icon block for forms whose controller does not
    save it (properties). With `turbo`, the form submits through Turbo inside
    the modal: an edit saved from an account's page returns to that page as a
    morphing refresh (its scroll position kept), and errors re-render in the
    modal. New accounts and the property wizard post the page as before. %>

<% if @error_message.present? %>
  <%= render DS::Alert.new(message: @error_message, variant: :error) %>
<% end %>

<%= styled_form_with model: account, url: url, scope: :account, data: (turbo ? {} : { turbo: false }), class: "flex flex-col gap-4 justify-between grow text-primary" do |form| %>
  <div class="grow space-y-2">
    <%= form.hidden_field :accountable_type %>
    <%= form.hidden_field :return_to, value: params[:return_to] %>

    <%= form.text_field :name, placeholder: t(".name_placeholder"), required: "required", label: t(".name_label") %>

    <% unless account.linked? %>
      <%= form.money_field :balance, label: t(".balance"), required: true, default_currency: Current.family.currency %>
    <% end %>

    <% if account.new_record? && !account.linked? %>
      <%= form.date_field :opening_balance_date,
          label: t(".opening_balance_date_label"),
          value: Time.zone.today - 2.years,
          required: true %>
    <% end %>

    <%= yield form %>

    <% if account.persisted? && account.supports_category_matcher? %>
      <div class="flex items-center justify-between gap-3 py-2">
        <div class="space-y-1">
          <p class="text-sm text-primary"><%= t(".enable_category_matcher_label") %></p>
          <p class="text-secondary text-sm"><%= t(".enable_category_matcher_hint") %></p>
        </div>
        <%= form.toggle :enable_category_matcher %>
      </div>
    <% end %>

    <% if account.persisted? && account.supports_trades? %>
      <div class="space-y-1">
        <%= form.select :cost_basis_method,
            Account.cost_basis_methods.keys.map { |method| [ t(".cost_basis_methods.#{method}"), method ] },
            { label: t(".cost_basis_method_label") } %>
        <p class="text-secondary text-sm"><%= t(".cost_basis_method_hint") %></p>
      </div>
    <% end %>

    <details class="group">
      <summary class="cursor-pointer text-sm text-secondary hover:text-primary flex items-center gap-1 py-2">
        <%= icon "chevron-right", size: "sm", class: "group-open:rotate-90 transition-transform" %>
        <%= t(".additional_details") %>
      </summary>

      <div class="space-y-2 mt-2 pl-4 border-l border-primary">
        <%= form.text_field :institution_name,
            label: t(".institution_name_label"),
            placeholder: account.provider&.institution_name || t(".institution_name_placeholder") %>

        <div class="space-y-1">
          <%= form.text_field :institution_domain,
              label: t(".institution_domain_label"),
              placeholder: account.provider&.institution_domain || t(".institution_domain_placeholder") %>
          <p class="text-xs text-secondary" data-testid="institution-domain-hint">
            <% if Setting.brand_fetch_client_id.present? %>
              <%= t(".institution_domain_hint") %>
            <% else %>
              <%= t(".institution_domain_missing_logo_key_hint") %>
              <% if self_hosted? && Current.user&.admin? %>
                <%= link_to t(".institution_domain_settings_link"), settings_hosting_path, class: "underline" %>
              <% end %>
            <% end %>
          </p>
        </div>

        <% if logo %>
          <div class="space-y-2"
               data-controller="account-logo-picker"
               data-account-logo-picker-pending-value="<%= t("accounts.logo_field.pending") %>"
               data-account-logo-picker-removal-value="<%= t("accounts.logo_field.removal") %>"
               data-account-logo-picker-provider-domain-value="<%= account.provider&.institution_domain %>">
            <%= form.label :logo, t(".logo_label"), class: "form-field__label" %>
            <%= render "accounts/logo_field", account: account %>
            <%= hidden_field_tag "#{form.object_name}[logo_choice]", nil, id: nil, data: { account_logo_picker_target: "choice" } %>
            <%= form.file_field :logo, accept: Account::LOGO_CONTENT_TYPES.join(","), class: "form-field__input",
                                data: { account_logo_picker_target: "file", action: "account-logo-picker#showFile" } %>
            <p class="text-xs text-secondary"><%= t(".logo_hint") %></p>
          </div>
        <% end %>

        <%= form.text_area :notes,
            label: t(".notes_label"),
            placeholder: t(".notes_placeholder"),
            rows: 4 %>

        <div class="flex items-center justify-between gap-2">
          <span class="text-sm"><%= t(".exclude_from_reports") %></span>
          <%= form.toggle :exclude_from_reports %>
        </div>
      </div>
    </details>
  </div>

  <%= form.submit %>
<% end %>

<% if logo %>
  <%= render "accounts/logo_forms", account: account %>
<% end %>