Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%= turbo_frame_tag :edit_shipping_category_modal do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @shipping_category, url: solidus_admin.shipping_category_path(@shipping_category), html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
<%= render component("ui/forms/field").text_field(f, :name) %>
</div>
<% modal.with_actions do %>
<form method="dialog">
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
</form>
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
<% end %>
<% end %>
<% end %>
<% end %>
<%= render component("shipping_categories/index").new(page: @page) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class SolidusAdmin::ShippingCategories::Edit::Component < SolidusAdmin::ShippingCategories::Index::Component
def initialize(page:, shipping_category:)
@page = page
@shipping_category = shipping_category
end

def form_id
dom_id(@shipping_category, "#{stimulus_id}_edit_shipping_category_form")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Add your component translations here.
# Use the translation in the example in your template with `t(".hello")`.
en:
title: "Edit Shipping Category"
cancel: "Cancel"
submit: "Update Shipping Category"
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ def page_actions
end

def turbo_frames
%w[new_shipping_category_modal]
%w[
new_shipping_category_modal
edit_shipping_category_modal
]
end

def row_url(shipping_category)
spree.edit_admin_shipping_category_path(shipping_category)
spree.edit_admin_shipping_category_path(shipping_category, _turbo_frame: :edit_shipping_category_modal)
end

def search_key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module SolidusAdmin
class ShippingCategoriesController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

before_action :find_shipping_category, only: %i[edit update]

def new
@shipping_category = Spree::ShippingCategory.new

Expand Down Expand Up @@ -51,6 +53,39 @@ def index
end
end

def edit
set_index_page

respond_to do |format|
format.html { render component('shipping_categories/edit').new(page: @page, shipping_category: @shipping_category) }
end
end

def update
if @shipping_category.update(shipping_category_params)
respond_to do |format|
flash[:notice] = t('.success')

format.html do
redirect_to solidus_admin.shipping_categories_path, status: :see_other
end

format.turbo_stream do
render turbo_stream: '<turbo-stream action="refresh" />'
end
end
else
set_index_page

respond_to do |format|
format.html do
page_component = component('shipping_categories/edit').new(page: @page, shipping_category: @shipping_category)
render page_component, status: :unprocessable_entity
end
end
end
end

def destroy
@shipping_category = Spree::ShippingCategory.find_by!(id: params[:id])

Expand All @@ -67,6 +102,10 @@ def load_shipping_category
authorize! action_name, @shipping_category
end

def find_shipping_category
@shipping_category = Spree::ShippingCategory.find(params[:id])
end

def shipping_category_params
params.require(:shipping_category).permit(:name)
end
Expand Down
2 changes: 2 additions & 0 deletions admin/config/locales/shipping_categories.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ en:
success: "Shipping categories were successfully removed."
create:
success: "Shipping category was successfully created."
update:
success: "Shipping category was successfully updated."
2 changes: 1 addition & 1 deletion admin/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
admin_resources :payment_methods, only: [:index, :destroy], sortable: true
admin_resources :stock_items, only: [:index, :edit, :update]
admin_resources :shipping_methods, only: [:index, :destroy]
admin_resources :shipping_categories, only: [:new, :index, :create, :destroy]
admin_resources :shipping_categories, except: [:show]
admin_resources :stock_locations, only: [:index, :destroy]
admin_resources :stores, only: [:index, :destroy]
admin_resources :zones, only: [:index, :destroy]
Expand Down
30 changes: 30 additions & 0 deletions admin/spec/features/shipping_categories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,34 @@
end
end
end

context "when editing an existing shipping category" do
let(:query) { "?page=1&q%5Bname_or_description_cont%5D=mail" }

before do
Spree::ShippingCategory.create(name: "Letter Mail")
visit "/admin/shipping_categories#{query}"
find_row("Letter Mail").click
expect(page).to have_content("Edit Shipping Category")
expect(page).to be_axe_clean
end

it "opens a modal" do
expect(page).to have_selector("dialog")
within("dialog") { click_on "Cancel" }
expect(page).not_to have_selector("dialog")
expect(page.current_url).to include(query)
end

it "successfully updates the existing shipping category" do
fill_in "Name", with: "Air Mail"

click_on "Update Shipping Category"
expect(page).to have_content("Shipping category was successfully updated.")
expect(page).to have_content("Air Mail")
expect(page).not_to have_content("Letter Mail")
expect(Spree::ShippingCategory.find_by(name: "Air Mail")).to be_present
expect(page.current_url).to include(query)
end
end
end