diff --git a/admin/app/components/solidus_admin/properties/edit/component.html.erb b/admin/app/components/solidus_admin/properties/edit/component.html.erb new file mode 100644 index 0000000000..b68489b2f6 --- /dev/null +++ b/admin/app/components/solidus_admin/properties/edit/component.html.erb @@ -0,0 +1,17 @@ +<%= turbo_frame_tag :edit_property_modal do %> + <%= render component("ui/modal").new(title: t(".title")) do |modal| %> + <%= form_for @property, url: solidus_admin.property_path(@property), html: { id: form_id } do |f| %> +
+ <%= render component("ui/forms/field").text_field(f, :name, class: "required") %> + <%= render component("ui/forms/field").text_field(f, :presentation, class: "required") %> +
+ <% 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("properties/index").new(page: @page) %> \ No newline at end of file diff --git a/admin/app/components/solidus_admin/properties/edit/component.rb b/admin/app/components/solidus_admin/properties/edit/component.rb new file mode 100644 index 0000000000..b2ed34d911 --- /dev/null +++ b/admin/app/components/solidus_admin/properties/edit/component.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class SolidusAdmin::Properties::Edit::Component < SolidusAdmin::BaseComponent + def initialize(page:, property:) + @page = page + @property = property + end + + def form_id + dom_id(@property, "#{stimulus_id}_edit_property_form") + end +end diff --git a/admin/app/components/solidus_admin/properties/edit/component.yml b/admin/app/components/solidus_admin/properties/edit/component.yml new file mode 100644 index 0000000000..bed9d213c8 --- /dev/null +++ b/admin/app/components/solidus_admin/properties/edit/component.yml @@ -0,0 +1,4 @@ +en: + title: "Edit Property" + cancel: "Cancel" + submit: "Update Property" diff --git a/admin/app/components/solidus_admin/properties/index/component.rb b/admin/app/components/solidus_admin/properties/index/component.rb index fdb5fd7c1f..2603c6c435 100644 --- a/admin/app/components/solidus_admin/properties/index/component.rb +++ b/admin/app/components/solidus_admin/properties/index/component.rb @@ -14,12 +14,13 @@ def search_url end def row_url(property) - spree.edit_admin_property_path(property) + spree.edit_admin_property_path(property, _turbo_frame: :edit_property_modal) end def turbo_frames %w[ new_property_modal + edit_property_modal ] end diff --git a/admin/app/components/solidus_admin/properties/new/component.rb b/admin/app/components/solidus_admin/properties/new/component.rb index 3f06b334cc..55e8806c68 100644 --- a/admin/app/components/solidus_admin/properties/new/component.rb +++ b/admin/app/components/solidus_admin/properties/new/component.rb @@ -5,7 +5,7 @@ def initialize(page:, property:) @page = page @property = property end - + def form_id dom_id(@property, "#{stimulus_id}_new_property_form") end diff --git a/admin/app/controllers/solidus_admin/properties_controller.rb b/admin/app/controllers/solidus_admin/properties_controller.rb index 996ad43f7f..feff935a27 100644 --- a/admin/app/controllers/solidus_admin/properties_controller.rb +++ b/admin/app/controllers/solidus_admin/properties_controller.rb @@ -4,6 +4,8 @@ module SolidusAdmin class PropertiesController < SolidusAdmin::BaseController include SolidusAdmin::ControllerHelpers::Search + before_action :find_property, only: %i[edit update] + def index set_index_page @@ -58,6 +60,39 @@ def create end end + def edit + set_index_page + + respond_to do |format| + format.html { render component('properties/edit').new(page: @page, property: @property) } + end + end + + def update + if @property.update(property_params) + respond_to do |format| + flash[:notice] = t('.success') + + format.html do + redirect_to solidus_admin.properties_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('properties/edit').new(page: @page, property: @property) + render page_component, status: :unprocessable_entity + end + end + end + end + def destroy @properties = Spree::Property.where(id: params[:id]) @@ -71,6 +106,10 @@ def destroy private + def find_property + @property = Spree::Property.find(params[:id]) + end + def property_params params.require(:property).permit(:name, :presentation) end diff --git a/admin/config/locales/properties.en.yml b/admin/config/locales/properties.en.yml index e866685476..b5b4b0e3d7 100644 --- a/admin/config/locales/properties.en.yml +++ b/admin/config/locales/properties.en.yml @@ -6,3 +6,5 @@ en: success: "Properties were successfully removed." create: success: "Property was successfully created." + update: + success: "Property was successfully updated." diff --git a/admin/config/routes.rb b/admin/config/routes.rb index a04633c366..70ef4c7e50 100644 --- a/admin/config/routes.rb +++ b/admin/config/routes.rb @@ -56,7 +56,7 @@ end admin_resources :promotions, only: [:index, :destroy] - admin_resources :properties, only: [:index, :new, :create, :destroy] + admin_resources :properties, except: [:show] admin_resources :option_types, only: [:index, :destroy], sortable: true admin_resources :taxonomies, only: [:index, :destroy], sortable: true admin_resources :promotion_categories, only: [:index, :destroy] diff --git a/admin/spec/features/properties_spec.rb b/admin/spec/features/properties_spec.rb index 79b6716b6b..83c19a961e 100644 --- a/admin/spec/features/properties_spec.rb +++ b/admin/spec/features/properties_spec.rb @@ -22,7 +22,7 @@ expect(Spree::Property.count).to eq(1) end - context "creating a new property" do + context "creating a new property" do it "creates a new product property" do visit "/admin/properties" click_on "Add new" @@ -78,4 +78,34 @@ expect(Spree::Property.count).to eq(1) end end + + context "editing an existing property" do + let!(:property) { create(:property, name: "Color", presentation: "Cool Color") } + + it "updates the property" do + visit "/admin/properties" + # Find the row containing the property with name "Color" + find('tr', text: 'Color').click + + fill_in "Name", with: "Size" + fill_in "Presentation", with: "Cool Size" + click_on "Update Property" + + expect(page).to have_content("Property was successfully updated.") + expect(page).to have_content("Size") + expect(page).to have_content("Cool Size") + expect(Spree::Property.count).to eq(1) + end + + it "shows validation errors" do + visit "/admin/properties" + find('tr', text: 'Color').click + + fill_in "Name", with: "" + click_on "Update Property" + + expect(page).to have_content("can't be blank") + expect(Spree::Property.count).to eq(1) + end + end end diff --git a/backend/spec/controllers/spree/admin/properties_controller_spec.rb b/backend/spec/controllers/spree/admin/properties_controller_spec.rb index 14b224b1e3..a78d2f71e8 100644 --- a/backend/spec/controllers/spree/admin/properties_controller_spec.rb +++ b/backend/spec/controllers/spree/admin/properties_controller_spec.rb @@ -20,11 +20,4 @@ expect(assigns(:collection)).to be_empty end end - - context "#show" do - it "redirects to edit when a property is found" do - get :show, params: { id: property.id } - expect(response).to redirect_to(edit_admin_property_path(property)) - end - end end