From a4b52a62d131b14a3571a9593bf5dc610e68df83 Mon Sep 17 00:00:00 2001 From: andrea longhi Date: Tue, 12 Mar 2024 17:45:02 +0100 Subject: [PATCH 1/4] Add edit and update tax category routes --- admin/config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/config/routes.rb b/admin/config/routes.rb index 37003949356..5b1822dcbfe 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -51,7 +51,7 @@ admin_resources :option_types, only: [:index, :destroy], sortable: true admin_resources :taxonomies, only: [:index, :destroy], sortable: true admin_resources :promotion_categories, only: [:index, :destroy] - admin_resources :tax_categories, only: [:new, :index, :create, :destroy] + admin_resources :tax_categories, except: [:show] admin_resources :tax_rates, only: [:index, :destroy] admin_resources :payment_methods, only: [:index, :destroy], sortable: true admin_resources :stock_items, only: [:index, :edit, :update] From db655311cce0e787b9a3547ebeb61ccef2553213 Mon Sep 17 00:00:00 2001 From: andrea longhi Date: Tue, 12 Mar 2024 17:45:44 +0100 Subject: [PATCH 2/4] Add edit tax category modal turbo frame This new Turbo frame will be filled with the modal for editing the tax category. --- .../solidus_admin/tax_categories/index/component.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/admin/app/components/solidus_admin/tax_categories/index/component.rb b/admin/app/components/solidus_admin/tax_categories/index/component.rb index c435fb1da7c..c2ce2b16ca2 100644 --- a/admin/app/components/solidus_admin/tax_categories/index/component.rb +++ b/admin/app/components/solidus_admin/tax_categories/index/component.rb @@ -2,7 +2,7 @@ class SolidusAdmin::TaxCategories::Index::Component < SolidusAdmin::Taxes::Component def row_url(tax_category) - spree.edit_admin_tax_category_path(tax_category) + spree.edit_admin_tax_category_path(tax_category, _turbo_frame: :edit_tax_category_modal) end def model_class @@ -24,7 +24,10 @@ def page_actions end def turbo_frames - %w[new_tax_category_modal] + %w[ + new_tax_category_modal + edit_tax_category_modal + ] end def search_key From 055e6f06a454afcc757676d5bde6306dcd736784 Mon Sep 17 00:00:00 2001 From: andrea longhi Date: Tue, 12 Mar 2024 17:46:02 +0100 Subject: [PATCH 3/4] Add tax_categories/edit component The component renders the edit form inside a modal on top of the tax categories list, inside a Turbo frame. --- .../tax_categories/edit/component.html.erb | 28 +++++++++++++++++++ .../tax_categories/edit/component.rb | 12 ++++++++ .../tax_categories/edit/component.yml | 8 ++++++ 3 files changed, 48 insertions(+) create mode 100644 admin/app/components/solidus_admin/tax_categories/edit/component.html.erb create mode 100644 admin/app/components/solidus_admin/tax_categories/edit/component.rb create mode 100644 admin/app/components/solidus_admin/tax_categories/edit/component.yml diff --git a/admin/app/components/solidus_admin/tax_categories/edit/component.html.erb b/admin/app/components/solidus_admin/tax_categories/edit/component.html.erb new file mode 100644 index 00000000000..a5feeef7251 --- /dev/null +++ b/admin/app/components/solidus_admin/tax_categories/edit/component.html.erb @@ -0,0 +1,28 @@ +<%= turbo_frame_tag :edit_tax_category_modal do %> + <%= render component("ui/modal").new(title: t(".title")) do |modal| %> + <%= form_for @tax_category, url: solidus_admin.tax_category_path(@tax_category), html: { id: form_id } do |f| %> +
+ <%= render component("ui/forms/field").text_field(f, :name) %> + <%= render component("ui/forms/field").text_field(f, :tax_code) %> + <%= render component("ui/forms/field").text_field(f, :description) %> + +
+ <% modal.with_actions do %> +
+ <%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %> +
+ <%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %> + <% end %> + <% end %> + <% end %> +<% end %> + +<%= render component("tax_categories/index").new(page: @page) %> diff --git a/admin/app/components/solidus_admin/tax_categories/edit/component.rb b/admin/app/components/solidus_admin/tax_categories/edit/component.rb new file mode 100644 index 00000000000..31ddbd13d72 --- /dev/null +++ b/admin/app/components/solidus_admin/tax_categories/edit/component.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class SolidusAdmin::TaxCategories::Edit::Component < SolidusAdmin::TaxCategories::Index::Component + def initialize(page:, tax_category:) + @page = page + @tax_category = tax_category + end + + def form_id + dom_id(@tax_category, "#{stimulus_id}_edit_tax_category_form") + end +end diff --git a/admin/app/components/solidus_admin/tax_categories/edit/component.yml b/admin/app/components/solidus_admin/tax_categories/edit/component.yml new file mode 100644 index 00000000000..6e3f8562048 --- /dev/null +++ b/admin/app/components/solidus_admin/tax_categories/edit/component.yml @@ -0,0 +1,8 @@ +# Add your component translations here. +# Use the translation in the example in your template with `t(".hello")`. +en: + title: "Edit Tax Category" + cancel: "Cancel" + submit: "Update Tax Category" + hints: + is_default: "When checked, this tax category will be selected by default when creating new products or variants." From f38fbd67fdef378667c5a5a8a8524fb45a974536 Mon Sep 17 00:00:00 2001 From: andrea longhi Date: Tue, 12 Mar 2024 17:47:43 +0100 Subject: [PATCH 4/4] Add tax category edit and update actions This completes the feature for editing an existing tax category. --- .../tax_categories_controller.rb | 41 +++++++++++++++++++ admin/config/locales/tax_categories.en.yml | 2 + 2 files changed, 43 insertions(+) diff --git a/admin/app/controllers/solidus_admin/tax_categories_controller.rb b/admin/app/controllers/solidus_admin/tax_categories_controller.rb index cc9e833a2ba..5bea5239211 100644 --- a/admin/app/controllers/solidus_admin/tax_categories_controller.rb +++ b/admin/app/controllers/solidus_admin/tax_categories_controller.rb @@ -4,6 +4,8 @@ module SolidusAdmin class TaxCategoriesController < SolidusAdmin::BaseController include SolidusAdmin::ControllerHelpers::Search + before_action :find_tax_category, only: %i[edit update] + def new @tax_category = Spree::TaxCategory.new @@ -14,6 +16,16 @@ def new end end + def edit + @tax_category = Spree::TaxCategory.find(params[:id]) + + set_index_page + + respond_to do |format| + format.html { render component('tax_categories/edit').new(page: @page, tax_category: @tax_category) } + end + end + def create @tax_category = Spree::TaxCategory.new(tax_category_params) @@ -43,6 +55,31 @@ def create end end + def update + if @tax_category.update(tax_category_params) + respond_to do |format| + flash[:notice] = t('.success') + + format.html do + redirect_to solidus_admin.tax_categories_path, status: :see_other + end + + format.turbo_stream do + render turbo_stream: '' + end + end + else + set_index_page + + respond_to do |format| + format.html do + page_component = component('tax_categories/edit').new(page: @page, tax_category: @tax_category) + render page_component, status: :unprocessable_entity + end + end + end + end + def index set_index_page @@ -67,6 +104,10 @@ def load_tax_category authorize! action_name, @tax_category end + def find_tax_category + @tax_category = Spree::TaxCategory.find(params[:id]) + end + def tax_category_params params.require(:tax_category).permit(:name, :description, :is_default, :tax_code) end diff --git a/admin/config/locales/tax_categories.en.yml b/admin/config/locales/tax_categories.en.yml index c86f9400f42..b049ad67e00 100644 --- a/admin/config/locales/tax_categories.en.yml +++ b/admin/config/locales/tax_categories.en.yml @@ -6,3 +6,5 @@ en: success: "Tax categories were successfully removed." create: success: "Tax category was successfully created." + update: + success: "Tax category was successfully updated."