Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Show disabled import options when no accounts exist (#977)

Juan José Mata8264943f238b
diff --git a/app/views/imports/_import_option.html.erb b/app/views/imports/_import_option.html.erb
new file mode 100644
index 0000000000..b7c73ffa72
--- /dev/null
+++ b/app/views/imports/_import_option.html.erb
@@ -0,0 +1,37 @@
+<%# locals: (type:, label:, icon_name: nil, icon_bg_class: nil, icon_text_class: nil, enabled:, disabled_message: nil, button_class: "flex items-center justify-between p-4 group cursor-pointer w-full", image: nil) %>
+<li>
+ <% if enabled %>
+ <%= button_to imports_path(import: { type: type }), class: button_class, data: { turbo: false } do %>
+ <div class="flex items-center gap-2">
+ <% if image %>
+ <%= image_tag(image, alt: "#{label} logo", class: "w-8 h-8 rounded-md") %>
+ <% else %>
+ <div class="<%= icon_bg_class %> rounded-md w-8 h-8 flex items-center justify-center">
+ <span class="<%= icon_text_class %>"><%= icon(icon_name, color: "current") %></span>
+ </div>
+ <% end %>
+ <span class="text-sm text-primary group-hover:text-secondary"><%= label %></span>
+ </div>
+ <%= icon("chevron-right") %>
+ <% end %>
+ <% else %>
+ <div class="flex items-center justify-between p-4 w-full opacity-50 cursor-not-allowed" aria-disabled="true">
+ <div class="flex items-center gap-2">
+ <% if image %>
+ <%= image_tag(image, alt: "#{label} logo", class: "w-8 h-8 rounded-md") %>
+ <% else %>
+ <div class="<%= icon_bg_class %> rounded-md w-8 h-8 flex items-center justify-center">
+ <span class="<%= icon_text_class %>"><%= icon(icon_name, color: "current") %></span>
+ </div>
+ <% end %>
+ <div>
+ <span class="text-sm text-primary block"><%= label %></span>
+ <span class="text-xs text-secondary block"><%= disabled_message %></span>
+ </div>
+ </div>
+ <%= icon("lock", size: "sm", class: "text-secondary") %>
+ </div>
+ <% end %>
+
+ <%= render "shared/ruler" %>
+</li>
diff --git a/app/views/imports/new.html.erb b/app/views/imports/new.html.erb
index 45ff294159..0b8c1a4904 100644
--- a/app/views/imports/new.html.erb
+++ b/app/views/imports/new.html.erb
@@ -2,6 +2,9 @@
<% dialog.with_header(title: t(".title"), subtitle: t(".description")) %>
<% dialog.with_body do %>
+ <% has_accounts = Current.family.accounts.any? %>
+ <% requires_account_message = t(".requires_account") %>
+
<div class="rounded-xl bg-container-inset p-1">
<h3 class="uppercase text-secondary text-xs font-medium px-3 py-1.5"><%= t(".sources") %></h3>
<ul class="bg-container shadow-border-xs rounded-lg">
@@ -25,120 +28,65 @@
</li>
<% end %>
- <% if Current.family.accounts.any? && (params[:type].nil? || params[:type] == "TransactionImport") %>
- <li>
- <%= button_to imports_path(import: { type: "TransactionImport" }), class: "flex items-center justify-between p-4 group cursor-pointer w-full", data: { turbo: false } do %>
- <div class="flex items-center gap-2">
- <div class="bg-indigo-500/5 rounded-md w-8 h-8 flex items-center justify-center">
- <span class="text-indigo-500">
- <%= icon("file-spreadsheet", color: "current") %>
- </span>
- </div>
- <span class="text-sm text-primary group-hover:text-secondary">
- <%= t(".import_transactions") %>
- </span>
- </div>
- <%= icon("chevron-right") %>
- <% end %>
-
- <%= render "shared/ruler" %>
- </li>
+ <% if params[:type].nil? || params[:type] == "TransactionImport" %>
+ <%= render "imports/import_option",
+ type: "TransactionImport",
+ icon_name: "file-spreadsheet",
+ icon_bg_class: "bg-indigo-500/5",
+ icon_text_class: "text-indigo-500",
+ label: t(".import_transactions"),
+ enabled: has_accounts,
+ disabled_message: requires_account_message %>
<% end %>
- <% if Current.family.accounts.any? && (params[:type].nil? || params[:type] == "TradeImport") %>
- <li>
- <%= button_to imports_path(import: { type: "TradeImport" }), class: "flex items-center justify-between p-4 group cursor-pointer w-full", data: { turbo: false } do %>
- <div class="flex items-center gap-2">
- <div class="bg-yellow-500/5 rounded-md w-8 h-8 flex items-center justify-center">
- <span class="text-yellow-500">
- <%= icon("square-percent", color: "current") %>
- </span>
- </div>
- <span class="text-sm text-primary group-hover:text-secondary">
- <%= t(".import_portfolio") %>
- </span>
- </div>
- <%= icon("chevron-right") %>
- <% end %>
-
- <%= render "shared/ruler" %>
- </li>
+ <% if params[:type].nil? || params[:type] == "TradeImport" %>
+ <%= render "imports/import_option",
+ type: "TradeImport",
+ icon_name: "square-percent",
+ icon_bg_class: "bg-yellow-500/5",
+ icon_text_class: "text-yellow-500",
+ label: t(".import_portfolio"),
+ enabled: has_accounts,
+ disabled_message: requires_account_message %>
<% end %>
<% if params[:type].nil? || params[:type] == "AccountImport" %>
- <li>
- <%= button_to imports_path(import: { type: "AccountImport" }), class: "flex items-center justify-between p-4 group cursor-pointer w-full", data: { turbo: false } do %>
- <div class="flex items-center gap-2">
- <div class="bg-violet-500/5 rounded-md w-8 h-8 flex items-center justify-center">
- <span class="text-violet-500">
- <%= icon("building", color: "current") %>
- </span>
- </div>
- <span class="text-sm text-primary group-hover:text-secondary">
- <%= t(".import_accounts") %>
- </span>
- </div>
- <%= icon("chevron-right") %>
- <% end %>
-
- <%= render "shared/ruler" %>
- </li>
+ <%= render "imports/import_option",
+ type: "AccountImport",
+ icon_name: "building",
+ icon_bg_class: "bg-violet-500/5",
+ icon_text_class: "text-violet-500",
+ label: t(".import_accounts"),
+ enabled: true %>
<% end %>
<% if params[:type].nil? || params[:type] == "CategoryImport" %>
- <li>
- <%= button_to imports_path(import: { type: "CategoryImport" }), class: "flex items-center justify-between p-4 group cursor-pointer w-full", data: { turbo: false } do %>
- <div class="flex items-center gap-2">
- <div class="bg-blue-500/5 rounded-md w-8 h-8 flex items-center justify-center">
- <span class="text-blue-500">
- <%= icon("shapes", color: "current") %>
- </span>
- </div>
- <span class="text-sm text-primary group-hover:text-secondary">
- <%= t(".import_categories") %>
- </span>
- </div>
- <%= icon("chevron-right") %>
- <% end %>
-
- <%= render "shared/ruler" %>
- </li>
+ <%= render "imports/import_option",
+ type: "CategoryImport",
+ icon_name: "shapes",
+ icon_bg_class: "bg-blue-500/5",
+ icon_text_class: "text-blue-500",
+ label: t(".import_categories"),
+ enabled: true %>
<% end %>
<% if params[:type].nil? || params[:type] == "RuleImport" %>
- <li>
- <%= button_to imports_path(import: { type: "RuleImport" }), class: "flex items-center justify-between p-4 group cursor-pointer w-full", data: { turbo: false } do %>
- <div class="flex items-center gap-2">
- <div class="bg-green-500/5 rounded-md w-8 h-8 flex items-center justify-center">
- <span class="text-green-500">
- <%= icon("workflow", color: "current") %>
- </span>
- </div>
- <span class="text-sm text-primary group-hover:text-secondary">
- <%= t(".import_rules") %>
- </span>
- </div>
- <%= icon("chevron-right") %>
- <% end %>
-
- <%= render "shared/ruler" %>
- </li>
+ <%= render "imports/import_option",
+ type: "RuleImport",
+ icon_name: "workflow",
+ icon_bg_class: "bg-green-500/5",
+ icon_text_class: "text-green-500",
+ label: t(".import_rules"),
+ enabled: true %>
<% end %>
- <% if Current.family.accounts.any? && (params[:type].nil? || params[:type] == "MintImport" || params[:type] == "TransactionImport") %>
- <li>
- <%= button_to imports_path(import: { type: "MintImport" }), class: "flex items-center justify-between p-4 group w-full", data: { turbo: false } do %>
- <div class="flex items-center gap-2">
- <%= image_tag("mint-logo.jpeg", alt: "Mint logo", class: "w-8 h-8 rounded-md") %>
- <span class="text-sm text-primary">
- <%= t(".import_mint") %>
- </span>
- </div>
- <%= icon("chevron-right") %>
- <% end %>
-
- <%= render "shared/ruler" %>
- </li>
+ <% if params[:type].nil? || params[:type] == "MintImport" || params[:type] == "TransactionImport" %>
+ <%= render "imports/import_option",
+ type: "MintImport",
+ image: "mint-logo.jpeg",
+ label: t(".import_mint"),
+ enabled: has_accounts,
+ disabled_message: requires_account_message %>
<% end %>
<% if (params[:type].nil? || params[:type].in?(%w[DocumentImport PdfImport])) && @document_upload_extensions.any? %>
diff --git a/config/locales/views/imports/en.yml b/config/locales/views/imports/en.yml
index 740f9b2bd4..6be06ff265 100644
--- a/config/locales/views/imports/en.yml
+++ b/config/locales/views/imports/en.yml
@@ -104,6 +104,7 @@ en:
import_transactions: Import transactions
import_file: Import document
import_file_description: AI-powered analysis for PDFs and searchable upload for other supported files
+ requires_account: Import accounts first to unlock this option.
resume: Resume %{type}
sources: Sources
title: New Import
diff --git a/test/controllers/imports_controller_test.rb b/test/controllers/imports_controller_test.rb
index 8c37385193..72f04c1cf0 100644
--- a/test/controllers/imports_controller_test.rb
+++ b/test/controllers/imports_controller_test.rb
@@ -23,6 +23,20 @@ class ImportsControllerTest < ActionDispatch::IntegrationTest
assert_select "turbo-frame#modal"
end
+ test "shows disabled account-dependent imports when family has no accounts" do
+ sign_in users(:empty)
+
+ get new_import_url
+
+ assert_response :success
+ assert_select "button", text: "Import accounts"
+ assert_select "button", text: "Import transactions", count: 0
+ assert_select "button", text: "Import investments", count: 0
+ assert_select "button", text: "Import from Mint", count: 0
+ assert_select "span", text: "Import accounts first to unlock this option.", count: 3
+ assert_select "div[aria-disabled=true]", count: 3
+ end
+
test "creates import" do
assert_difference "Import.count", 1 do
post imports_url, params: {