Skip to content

Commit

Permalink
Admin product properties: Load modal with turbo frame
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed Jan 22, 2025
1 parent 90e0333 commit f128ab6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 145 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= turbo_frame_tag :edit_property_modal do %>
<%= turbo_frame_tag :resource_modal, target: "_top" 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| %>
<%= form_for @property, url: form_url, html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
<%= render component("ui/forms/field").text_field(f, :presentation, class: "required") %>
Expand All @@ -14,4 +14,3 @@
<% end %>
<% end %>
<% end %>
<%= render component("properties/index").new(page: @page) %>
10 changes: 1 addition & 9 deletions admin/app/components/solidus_admin/properties/edit/component.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
# 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
class SolidusAdmin::Properties::Edit::Component < SolidusAdmin::Resources::Edit::Component
end
22 changes: 12 additions & 10 deletions admin/app/components/solidus_admin/properties/index/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@ def search_url
solidus_admin.properties_path
end

def row_url(property)
solidus_admin.edit_property_path(property, _turbo_frame: :edit_property_modal)
def edit_path(property)
solidus_admin.edit_property_path(property, **search_filter_params)
end

def turbo_frames
%w[
new_property_modal
edit_property_modal
]
%w[resource_modal]
end

def page_actions
render component("ui/button").new(
tag: :a,
text: t('.add'),
href: solidus_admin.new_property_path, data: { turbo_frame: :new_property_modal },
href: solidus_admin.new_property_path(**search_filter_params),
data: { turbo_frame: :resource_modal },
icon: "add-line",
)
end
Expand All @@ -41,7 +39,7 @@ def batch_actions
[
{
label: t('.batch_actions.delete'),
action: solidus_admin.properties_path,
action: solidus_admin.properties_path(**search_filter_params),
method: :delete,
icon: 'delete-bin-7-line',
},
Expand All @@ -59,7 +57,9 @@ def name_column
{
header: :name,
data: ->(property) do
content_tag :div, property.name
link_to property.name, edit_path(property),
data: { turbo_frame: :resource_modal },
class: 'body-link'
end
}
end
Expand All @@ -68,7 +68,9 @@ def presentation_column
{
header: :presentation,
data: ->(property) do
content_tag :div, property.presentation
link_to property.presentation, edit_path(property),
data: { turbo_frame: :resource_modal },
class: 'body-link'
end
}
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= turbo_frame_tag :new_property_modal do %>
<%= turbo_frame_tag :resource_modal, target: "_top" do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @property, url: solidus_admin.properties_path, html: { id: form_id } do |f| %>
<%= form_for @property, url: form_url, html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
<%= render component("ui/forms/field").text_field(f, :name, class: "required") %>
<%= render component("ui/forms/field").text_field(f, :presentation, class: "required") %>
Expand All @@ -14,5 +14,3 @@
<% end %>
<% end %>
<% end %>

<%= render component("properties/index").new(page: @page) %>
10 changes: 1 addition & 9 deletions admin/app/components/solidus_admin/properties/new/component.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
# frozen_string_literal: true

class SolidusAdmin::Properties::New::Component < SolidusAdmin::BaseComponent
def initialize(page:, property:)
@page = page
@property = property
end

def form_id
dom_id(@property, "#{stimulus_id}_new_property_form")
end
class SolidusAdmin::Properties::New::Component < SolidusAdmin::Resources::New::Component
end
110 changes: 4 additions & 106 deletions admin/app/controllers/solidus_admin/properties_controller.rb
Original file line number Diff line number Diff line change
@@ -1,117 +1,15 @@
# frozen_string_literal: true

module SolidusAdmin
class PropertiesController < SolidusAdmin::BaseController
include SolidusAdmin::ControllerHelpers::Search

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

def index
set_index_page

respond_to do |format|
format.html { render component('properties/index').new(page: @page) }
end
end

def new
@property = Spree::Property.new

set_index_page

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

def create
@property = Spree::Property.new(property_params)

if @property.save
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: '<turbo-stream action="refresh" />'
end
end
else
set_index_page

respond_to do |format|
format.html do
page_component = component('properties/new').new(page: @page, property: @property)
render page_component, status: :unprocessable_entity
end
end
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: '<turbo-stream action="refresh" />'
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])

Spree::Property.transaction do
@properties.destroy_all
end

flash[:notice] = t('.success')
redirect_to properties_path, status: :see_other
end

class PropertiesController < SolidusAdmin::ResourcesController
private

def set_property
@property = Spree::Property.find(params[:id])
end
def resource_class = Spree::Property

def property_params
def permitted_resource_params
params.require(:property).permit(:name, :presentation)
end

def set_index_page
properties = apply_search_to(
Spree::Property.unscoped.order(id: :desc),
param: :q,
)

set_page_and_extract_portion_from(properties)
end
def resources_collection = Spree::Property.unscoped
end
end
8 changes: 4 additions & 4 deletions admin/spec/features/properties_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

it "updates the property" do
visit "/admin/properties"
find_row("Color").click
click_on "Color"

fill_in "Name", with: "Size"
fill_in "Presentation", with: "Cool Size"
Expand All @@ -68,7 +68,7 @@

it "shows validation errors" do
visit "/admin/properties"
find_row("Color").click
click_on "Color"

fill_in "Name", with: ""
click_on "Update Property"
Expand All @@ -83,7 +83,7 @@

it "updates the property" do
visit "/admin/properties"
find_row("Color").click
click_on "Color"

fill_in "Name", with: "Size"
fill_in "Presentation", with: "Cool Size"
Expand All @@ -97,7 +97,7 @@

it "shows validation errors" do
visit "/admin/properties"
find_row("Color").click
click_on "Color"
expect(page).to have_field("Name", with: "Color")
fill_in "Name", with: ""
click_on "Update Property"
Expand Down

0 comments on commit f128ab6

Please sign in to comment.