Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(categories): update avatar preview when picking a color (#3554)

Justin McBride244609c46efd
diff --git a/app/views/categories/_color_avatar.html.erb b/app/views/categories/_color_avatar.html.erb
index 8e690c3b8a..abefba842e 100644
--- a/app/views/categories/_color_avatar.html.erb
+++ b/app/views/categories/_color_avatar.html.erb
@@ -3,6 +3,6 @@
<span
data-color-icon-picker-target="avatar"
class="w-14 h-14 flex items-center justify-center rounded-full"
- style="background-color: color-mix(in oklab, <%= category.color %> 10%, transparent); color: <%= category.color %>">
+ style="--avatar-color: <%= category.color %>; background-color: color-mix(in oklab, var(--avatar-color) 10%, transparent); color: var(--avatar-color)">
<%= icon(category.lucide_icon, size: "2xl", color: "current") %>
</span>
diff --git a/test/system/categories_test.rb b/test/system/categories_test.rb
index f1cd39d2db..c3eff0be3e 100644
--- a/test/system/categories_test.rb
+++ b/test/system/categories_test.rb
@@ -58,6 +58,21 @@ class CategoriesTest < ApplicationSystemTestCase
assert_equal "pizza", @user.family.categories.find_by!(name: "Takeout").lucide_icon
end
+ test "picking a color updates the avatar preview" do
+ visit categories_url
+ click_link I18n.t("categories.new.new_category")
+
+ avatar = find("[data-color-icon-picker-target='avatar']")
+ initial_color = avatar.evaluate_script("getComputedStyle(this).color")
+
+ find("summary[aria-label='#{I18n.t("categories.form.trigger_label")}']").click
+
+ color = Category::COLORS.find { |c| hex_to_rgb(c) != initial_color }
+ find("input[type='radio'][value='#{color}']", visible: :all).find(:xpath, "..").click
+
+ assert_equal hex_to_rgb(color), avatar.evaluate_script("getComputedStyle(this).color")
+ end
+
test "reopening the icon picker clears the previous search" do
visit categories_url
click_link I18n.t("categories.new.new_category")
@@ -118,4 +133,9 @@ class CategoriesTest < ApplicationSystemTestCase
assert_operator page_scroll_width, :<=, viewport_width
end
+
+ private
+ def hex_to_rgb(hex)
+ "rgb(#{hex.delete_prefix("#").scan(/../).map { |c| c.to_i(16) }.join(", ")})"
+ end
end