Skip to content

Commit 2d6d261

Browse files
committed
Admin stock items: Load modal with turbo frame
1 parent cccc529 commit 2d6d261

File tree

4 files changed

+93
-110
lines changed

4 files changed

+93
-110
lines changed
Lines changed: 74 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,84 @@
1-
<div
2-
data-controller="<%= stimulus_id %>"
3-
data-<%= stimulus_id %>-initial-count-on-hand-value="<%= @stock_item.count_on_hand_was || @stock_item.count_on_hand %>"
4-
data-action="input-><%= stimulus_id %>#updateCountOnHand"
5-
>
6-
<%= turbo_frame_tag :edit_stock_item_modal do %>
7-
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
8-
<%= form_for @stock_item, url: solidus_admin.stock_item_path(@stock_item), html: { id: form_id } do |f| %>
9-
<div class="flex flex-col gap-6 pb-4">
10-
<div class="flex gap-4">
11-
<%= link_to spree.edit_admin_product_variant_path(
12-
@stock_item.variant.product,
13-
@stock_item.variant,
14-
), class: 'hover:bg-gray-25 rounded p-1 w-1/2 border border-gray-100' do %>
15-
<%= render component("ui/resource_item").new(
16-
thumbnail:
17-
(
18-
@stock_item.variant.images.first ||
19-
@stock_item.variant.product.gallery.images.first
20-
)&.url(:small),
21-
title: @stock_item.variant.name,
22-
subtitle:
23-
"#{@stock_item.variant.sku}#{@stock_item.variant.options_text.presence&.prepend(" - ")}",
24-
) %>
25-
<% end %>
26-
<%= link_to spree.edit_admin_stock_location_path(@stock_item.stock_location), class: 'hover:bg-gray-25 rounded p-1 w-1/2 border border-gray-100' do %>
27-
<%= render component("ui/resource_item").new(
28-
title: @stock_item.stock_location.name,
29-
subtitle: "#{Spree::StockLocation.model_name.human} #{@stock_item.stock_location.code}",
30-
) %>
31-
<% end %>
32-
</div>
33-
34-
<%= render component("ui/forms/field").text_field(
35-
f,
36-
:count_on_hand,
37-
disabled: true,
38-
value: @stock_item.count_on_hand_was || @stock_item.count_on_hand,
39-
"data-#{stimulus_id}-target": 'countOnHand',
40-
) %>
41-
<%= render component("ui/forms/field").new(
42-
label: t(".quantity_adjustment"),
43-
hint: t(".quantity_adjustment_hint_html"),
44-
) do %>
45-
<%= render component("ui/forms/input").new(
46-
value: params[:quantity_adjustment] || 0,
47-
name: :quantity_adjustment,
48-
type: :number,
49-
step: 1,
50-
"data-#{stimulus_id}-target": 'quantityAdjustment',
1+
<%= turbo_frame_tag :resource_modal, target: "_top" do %>
2+
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
3+
<%= form_for @stock_item, url: form_url, html: { id: form_id } do |f| %>
4+
<div
5+
class="flex flex-col gap-6 pb-4"
6+
data-controller="<%= stimulus_id %>"
7+
data-<%= stimulus_id %>-initial-count-on-hand-value="<%= @stock_item.count_on_hand_was || @stock_item.count_on_hand %>"
8+
data-action="input-><%= stimulus_id %>#updateCountOnHand"
9+
>
10+
<div class="flex gap-4">
11+
<%= link_to spree.edit_admin_product_variant_path(
12+
@stock_item.variant.product,
13+
@stock_item.variant,
14+
),
15+
data: {turbo_frame: "_top"},
16+
class: 'hover:bg-gray-25 rounded p-1 w-1/2 border border-gray-100' do %>
17+
<%= render component("ui/resource_item").new(
18+
thumbnail:
19+
(
20+
@stock_item.variant.images.first ||
21+
@stock_item.variant.product.gallery.images.first
22+
)&.url(:small),
23+
title: @stock_item.variant.name,
24+
subtitle:
25+
"#{@stock_item.variant.sku}#{@stock_item.variant.options_text.presence&.prepend(" - ")}",
26+
) %>
27+
<% end %>
28+
<%= link_to spree.edit_admin_stock_location_path(@stock_item.stock_location),
29+
data: {turbo_frame: "_top"},
30+
class: 'hover:bg-gray-25 rounded p-1 w-1/2 border border-gray-100' do %>
31+
<%= render component("ui/resource_item").new(
32+
title: @stock_item.stock_location.name,
33+
subtitle: "#{Spree::StockLocation.model_name.human} #{@stock_item.stock_location.code}",
5134
) %>
5235
<% end %>
53-
54-
<%= render component("ui/forms/switch_field").new(
55-
name: "#{f.object_name}[backorderable]",
56-
label: Spree::StockItem.human_attribute_name(:backorderable),
57-
error: f.object.errors[:backorderable],
58-
hint: t(".backorderable_hint_html"),
59-
checked: f.object.backorderable?,
60-
include_hidden: true,
61-
) %>
6236
</div>
63-
<% end %>
6437

65-
<% modal.with_actions do %>
66-
<form method="dialog">
67-
<%= render component("ui/button").new(
68-
scheme: :secondary,
69-
text: t(".cancel"),
38+
<%= render component("ui/forms/field").text_field(
39+
f,
40+
:count_on_hand,
41+
readonly: true,
42+
value: @stock_item.count_on_hand_was || @stock_item.count_on_hand,
43+
"data-#{stimulus_id}-target": 'countOnHand',
44+
) %>
45+
<%= render component("ui/forms/field").new(
46+
label: t(".quantity_adjustment"),
47+
hint: t(".quantity_adjustment_hint_html"),
48+
) do %>
49+
<%= render component("ui/forms/input").new(
50+
value: params[:quantity_adjustment] || 0,
51+
name: :quantity_adjustment,
52+
type: :number,
53+
step: 1,
54+
"data-#{stimulus_id}-target": 'quantityAdjustment',
7055
) %>
71-
</form>
56+
<% end %>
57+
58+
<%= render component("ui/forms/switch_field").new(
59+
name: "#{f.object_name}[backorderable]",
60+
label: Spree::StockItem.human_attribute_name(:backorderable),
61+
error: f.object.errors[:backorderable],
62+
hint: t(".backorderable_hint_html"),
63+
checked: f.object.backorderable?,
64+
include_hidden: true,
65+
) %>
66+
</div>
67+
<% end %>
7268

69+
<% modal.with_actions do %>
70+
<form method="dialog">
7371
<%= render component("ui/button").new(
74-
tag: :button,
75-
text: t(".submit"),
76-
form: form_id,
72+
scheme: :secondary,
73+
text: t(".cancel"),
7774
) %>
78-
<% end %>
75+
</form>
76+
77+
<%= render component("ui/button").new(
78+
tag: :button,
79+
text: t(".submit"),
80+
form: form_id,
81+
) %>
7982
<% end %>
8083
<% end %>
81-
82-
<%= render component("stock_items/index").new(page: @page) %>
83-
</div>
84+
<% end %>
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
# frozen_string_literal: true
22

3-
class SolidusAdmin::StockItems::Edit::Component < SolidusAdmin::BaseComponent
4-
def initialize(stock_item:, page:)
5-
@stock_item = stock_item
6-
@page = page
7-
end
8-
3+
class SolidusAdmin::StockItems::Edit::Component < SolidusAdmin::Resources::Edit::Component
94
def title
105
[
116
"#{Spree::StockLocation.model_name.human}: #{@stock_item.stock_location.name}",
127
].join(' / ')
138
end
14-
15-
def form_id
16-
"#{stimulus_id}-#{dom_id(@stock_item)}"
17-
end
189
end

admin/app/components/solidus_admin/stock_items/index/component.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def search_url
1313
solidus_admin.stock_items_path
1414
end
1515

16-
def row_url(stock_item)
17-
solidus_admin.edit_stock_item_path(stock_item, _turbo_frame: :edit_stock_item_modal)
16+
def edit_path(stock_item)
17+
solidus_admin.edit_stock_item_path(stock_item, **search_filter_params)
1818
end
1919

2020
def scopes
@@ -90,7 +90,9 @@ def name_column
9090
{
9191
header: :name,
9292
data: ->(stock_item) do
93-
content_tag :div, stock_item.variant.name
93+
link_to stock_item.variant.name, edit_path(stock_item),
94+
data: { turbo_frame: :resource_modal },
95+
class: 'body-link'
9496
end
9597
}
9698
end
@@ -99,7 +101,9 @@ def sku_column
99101
{
100102
header: :sku,
101103
data: ->(stock_item) do
102-
content_tag :div, stock_item.variant.sku
104+
link_to stock_item.variant.sku, edit_path(stock_item),
105+
data: { turbo_frame: :resource_modal },
106+
class: 'body-link'
103107
end
104108
}
105109
end
@@ -168,6 +172,6 @@ def count_on_hand_column
168172
end
169173

170174
def turbo_frames
171-
%w[edit_stock_item_modal]
175+
%w[resource_modal]
172176
end
173177
end

admin/app/controllers/solidus_admin/stock_items_controller.rb

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# frozen_string_literal: true
22

33
module SolidusAdmin
4-
class StockItemsController < SolidusAdmin::BaseController
4+
class StockItemsController < SolidusAdmin::ResourcesController
55
include SolidusAdmin::ControllerHelpers::Search
6-
before_action :load_stock_items, only: [:index, :edit, :update]
7-
before_action :load_stock_item, only: [:edit, :update]
6+
before_action :load_stock_items, only: [:index]
87

98
search_scope(:all, default: true) { _1 }
109
search_scope(:back_orderable) { _1.where(backorderable: true) }
@@ -18,31 +17,23 @@ def index
1817
end
1918
end
2019

21-
def edit
22-
respond_to do |format|
23-
format.html { render component('stock_items/edit').new(stock_item: @stock_item, page: @page) }
24-
end
25-
end
26-
2720
def update
2821
quantity_adjustment = params[:quantity_adjustment].to_i
29-
@stock_item.assign_attributes(stock_item_params)
22+
@stock_item.assign_attributes(permitted_resource_params)
3023
@stock_item.stock_movements.build(quantity: quantity_adjustment, originator: current_solidus_admin_user)
3124

3225
if @stock_item.save
33-
respond_to do |format|
34-
format.html { redirect_to solidus_admin.stock_items_path, status: :see_other }
35-
format.turbo_stream { render turbo_stream: '<turbo-stream action="refresh" />' }
36-
end
26+
redirect_to after_update_path, status: :see_other
3727
else
38-
respond_to do |format|
39-
format.html { render component('stock_items/edit').new(stock_item: @stock_item, page: @page), status: :unprocessable_entity }
40-
end
28+
page_component = edit_component.new(@stock_item)
29+
render_resource_form_with_errors(page_component)
4130
end
4231
end
4332

4433
private
4534

35+
def resource_class = Spree::StockItem
36+
4637
def load_stock_items
4738
@stock_items = apply_search_to(
4839
Spree::StockItem.reorder(nil),
@@ -56,11 +47,7 @@ def load_stock_items
5647
})
5748
end
5849

59-
def load_stock_item
60-
@stock_item = Spree::StockItem.find(params[:id])
61-
end
62-
63-
def stock_item_params
50+
def permitted_resource_params
6451
params.require(:stock_item).permit(:backorderable)
6552
end
6653
end

0 commit comments

Comments
 (0)