Skip to content

Commit 9c5577b

Browse files
committed
add creation and editing product properties using UI components
1 parent c8f23a7 commit 9c5577b

File tree

11 files changed

+261
-3
lines changed

11 files changed

+261
-3
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<%= turbo_frame_tag :edit_property_modal do %>
2+
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
3+
<%= form_for @property, url: solidus_admin.property_path(@property), html: { id: form_id } do |f| %>
4+
<div class="flex flex-col gap-6 pb-4">
5+
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
6+
<%= render component("ui/forms/field").text_field(f, :presentation, class: "required") %>
7+
</div>
8+
<% modal.with_actions do %>
9+
<form method="dialog">
10+
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
11+
</form>
12+
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
13+
<% end %>
14+
<% end %>
15+
<% end %>
16+
<% end %>
17+
<%= render component("properties/index").new(page: @page) %>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
class SolidusAdmin::Properties::Edit::Component < SolidusAdmin::BaseComponent
4+
def initialize(page:, property:)
5+
@page = page
6+
@property = property
7+
end
8+
9+
def form_id
10+
dom_id(@property, "#{stimulus_id}_edit_property_form")
11+
end
12+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
en:
2+
title: "Edit Property"
3+
cancel: "Cancel"
4+
submit: "Update Property"

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,21 @@ def search_url
1414
end
1515

1616
def row_url(property)
17-
spree.edit_admin_property_path(property)
17+
spree.edit_admin_property_path(property, _turbo_frame: :edit_property_modal)
18+
end
19+
20+
def turbo_frames
21+
%w[
22+
new_property_modal
23+
edit_property_modal
24+
]
1825
end
1926

2027
def page_actions
2128
render component("ui/button").new(
2229
tag: :a,
2330
text: t('.add'),
24-
href: spree.new_admin_property_path,
31+
href: solidus_admin.new_property_path, data: { turbo_frame: :new_property_modal },
2532
icon: "add-line",
2633
)
2734
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<%= turbo_frame_tag :new_property_modal do %>
2+
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
3+
<%= form_for @property, url: solidus_admin.properties_path, html: { id: form_id } do |f| %>
4+
<div class="flex flex-col gap-6 pb-4">
5+
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
6+
<%= render component("ui/forms/field").text_field(f, :presentation, class: "required") %>
7+
</div>
8+
<% modal.with_actions do %>
9+
<form method="dialog">
10+
<%= render component("ui/button").new(scheme: :secondary, text: t('.cancel')) %>
11+
</form>
12+
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
13+
<% end %>
14+
<% end %>
15+
<% end %>
16+
<% end %>
17+
18+
<%= render component("properties/index").new(page: @page) %>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
class SolidusAdmin::Properties::New::Component < SolidusAdmin::BaseComponent
4+
def initialize(page:, property:)
5+
@page = page
6+
@property = property
7+
end
8+
9+
def form_id
10+
dom_id(@property, "#{stimulus_id}_new_property_form")
11+
end
12+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
en:
2+
title: "New Property"
3+
cancel: "Cancel"
4+
submit: "Add Property"

admin/app/controllers/solidus_admin/properties_controller.rb

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ module SolidusAdmin
44
class PropertiesController < SolidusAdmin::BaseController
55
include SolidusAdmin::ControllerHelpers::Search
66

7+
before_action :find_property, only: %i[edit update]
8+
79
def index
10+
set_index_page
11+
812
properties = apply_search_to(
913
Spree::Property.order(created_at: :desc, id: :desc),
1014
param: :q,
@@ -19,6 +23,76 @@ def index
1923
end
2024
end
2125

26+
def new
27+
@property = Spree::Property.new
28+
29+
set_index_page
30+
31+
respond_to do |format|
32+
format.html { render component('properties/new').new(page: @page, property: @property) }
33+
end
34+
end
35+
36+
def create
37+
@property = Spree::Property.new(property_params)
38+
39+
if @property.save
40+
respond_to do |format|
41+
flash[:notice] = t('.success')
42+
43+
format.html do
44+
redirect_to solidus_admin.properties_path, status: :see_other
45+
end
46+
47+
format.turbo_stream do
48+
render turbo_stream: '<turbo-stream action="refresh" />'
49+
end
50+
end
51+
else
52+
set_index_page
53+
54+
respond_to do |format|
55+
format.html do
56+
page_component = component('properties/new').new(page: @page, property: @property)
57+
render page_component, status: :unprocessable_entity
58+
end
59+
end
60+
end
61+
end
62+
63+
def edit
64+
set_index_page
65+
66+
respond_to do |format|
67+
format.html { render component('properties/edit').new(page: @page, property: @property) }
68+
end
69+
end
70+
71+
def update
72+
if @property.update(property_params)
73+
respond_to do |format|
74+
flash[:notice] = t('.success')
75+
76+
format.html do
77+
redirect_to solidus_admin.properties_path, status: :see_other
78+
end
79+
80+
format.turbo_stream do
81+
render turbo_stream: '<turbo-stream action="refresh" />'
82+
end
83+
end
84+
else
85+
set_index_page
86+
87+
respond_to do |format|
88+
format.html do
89+
page_component = component('properties/edit').new(page: @page, property: @property)
90+
render page_component, status: :unprocessable_entity
91+
end
92+
end
93+
end
94+
end
95+
2296
def destroy
2397
@properties = Spree::Property.where(id: params[:id])
2498

@@ -29,5 +103,24 @@ def destroy
29103
flash[:notice] = t('.success')
30104
redirect_to properties_path, status: :see_other
31105
end
106+
107+
private
108+
109+
def find_property
110+
@property = Spree::Property.find(params[:id])
111+
end
112+
113+
def property_params
114+
params.require(:property).permit(:name, :presentation)
115+
end
116+
117+
def set_index_page
118+
properties = apply_search_to(
119+
Spree::Property.unscoped.order(id: :desc),
120+
param: :q,
121+
)
122+
123+
set_page_and_extract_portion_from(properties)
124+
end
32125
end
33126
end

admin/config/locales/properties.en.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ en:
44
title: "Properties"
55
destroy:
66
success: "Properties were successfully removed."
7+
create:
8+
success: "Property was successfully created."
9+
update:
10+
success: "Property was successfully updated."

admin/config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
end
5757

5858
admin_resources :promotions, only: [:index, :destroy]
59-
admin_resources :properties, only: [:index, :destroy]
59+
admin_resources :properties, except: [:show]
6060
admin_resources :option_types, only: [:index, :destroy], sortable: true
6161
admin_resources :taxonomies, only: [:index, :destroy], sortable: true
6262
admin_resources :promotion_categories, only: [:index, :destroy]

0 commit comments

Comments
 (0)