diff --git a/app/javascript/controllers/color_avatar_controller.js b/app/javascript/controllers/color_avatar_controller.js index 276ae0234b2..41b7568b330 100644 --- a/app/javascript/controllers/color_avatar_controller.js +++ b/app/javascript/controllers/color_avatar_controller.js @@ -3,7 +3,7 @@ import { Controller } from "@hotwired/stimulus"; // Connects to data-controller="color-avatar" // Used by the transaction merchant form to show a preview of what the avatar will look like export default class extends Controller { - static targets = ["name", "avatar"]; + static targets = ["name", "avatar", "selection"]; connect() { this.nameTarget.addEventListener("input", this.handleNameChange); @@ -25,4 +25,10 @@ export default class extends Controller { this.avatarTarget.style.borderColor = `color-mix(in srgb, ${color} 10%, white)`; this.avatarTarget.style.color = color; } + + handleParentChange(e) { + const parent = e.currentTarget.value; + const visibility = typeof parent === "string" && parent !== "" ? "hidden" : "visible" + this.selectionTarget.style.visibility = visibility + } } diff --git a/app/models/category.rb b/app/models/category.rb index d0ccaa60311..76a83866549 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -14,6 +14,8 @@ class Category < ApplicationRecord validate :category_level_limit validate :nested_category_matches_parent_classification + before_create :inherit_color_from_parent + scope :alphabetically, -> { order(:name) } scope :roots, -> { where(parent_id: nil) } scope :incomes, -> { where(classification: "income") } @@ -85,6 +87,12 @@ def default_categories end end + def inherit_color_from_parent + if subcategory? + self.color = parent.color + end + end + def replace_and_destroy!(replacement) transaction do transactions.update_all category_id: replacement&.id diff --git a/app/views/categories/_form.html.erb b/app/views/categories/_form.html.erb index 884d5509e37..fd9a8513aec 100644 --- a/app/views/categories/_form.html.erb +++ b/app/views/categories/_form.html.erb @@ -7,7 +7,7 @@ <%= render partial: "shared/color_avatar", locals: { name: category.name, color: category.color } %> -
+
<% Category::COLORS.each do |color| %>