Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin] Add product properties admin UI #5926

Closed
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
@@ -1,6 +1,10 @@
# frozen_string_literal: true

class SolidusAdmin::Properties::Index::Component < SolidusAdmin::UI::Pages::Index::Component
def title
t('solidus_admin.properties.title')
end

def model_class
Spree::Property
end
Expand All @@ -17,11 +21,18 @@ def row_url(property)
spree.admin_property_path(property)
end

def turbo_frames
%w[
new_property_modal
]
end

def page_actions
render component("ui/button").new(
tag: :a,
text: t('.add'),
href: spree.new_admin_property_path,
href: solidus_admin.new_property_path,
data: { turbo_frame: :new_property_modal },
icon: "add-line",
)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%= turbo_frame_tag :new_property_modal 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| %>
<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") %>
</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("properties/index").new(page: @page) %>
12 changes: 12 additions & 0 deletions admin/app/components/solidus_admin/properties/new/component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 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
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
title: "New Property"
cancel: "Cancel"
submit: "Add Property"
66 changes: 58 additions & 8 deletions admin/app/controllers/solidus_admin/properties_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,53 @@
include SolidusAdmin::ControllerHelpers::Search

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

set_page_and_extract_portion_from(
properties,
)
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

Check warning on line 36 in admin/app/controllers/solidus_admin/properties_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/properties_controller.rb#L36

Added line #L36 was not covered by tests
end

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

Check warning on line 44 in admin/app/controllers/solidus_admin/properties_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/properties_controller.rb#L44

Added line #L44 was not covered by tests

respond_to do |format|
format.html do
page_component = component('properties/new').new(page: @page, property: @property)
render page_component, status: :unprocessable_entity

Check warning on line 49 in admin/app/controllers/solidus_admin/properties_controller.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/properties_controller.rb#L46-L49

Added lines #L46 - L49 were not covered by tests
end
end
end
end

def destroy
@properties = Spree::Property.where(id: params[:id])

Expand All @@ -29,5 +62,22 @@
flash[:notice] = t('.success')
redirect_to properties_path, status: :see_other
end

private

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

set_page_and_extract_portion_from(
properties,
)
end

def property_params
params.require(:property).permit(:name, :presentation)
end
end
end
2 changes: 2 additions & 0 deletions admin/config/locales/properties.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ en:
solidus_admin:
properties:
title: "Properties"
create:
success: "Property was successfully created."
destroy:
success: "Properties were successfully removed."
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 @@
end

admin_resources :promotions, only: [:index, :destroy]
admin_resources :properties, only: [:index, :destroy]
admin_resources :properties, only: [:index, :destroy, :new, :create]
admin_resources :option_types, only: [:index, :destroy], sortable: true
admin_resources :taxonomies, only: [:index, :destroy], sortable: true
admin_resources :promotion_categories, only: [:index, :destroy]
Expand Down
29 changes: 29 additions & 0 deletions admin/spec/features/properties_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,33 @@
expect(page).not_to have_content("Type prop")
expect(Spree::Property.count).to eq(1)
end

context "when creating a new product property" do
before do
visit "/admin/properties"
click_on "Add new"
expect(page).to have_content("New Property")
expect(page).to be_axe_clean
end

it "opens and closes new property modal" do
expect(page).to have_selector("dialog")
within("dialog") { click_on "Cancel" }
expect(page).not_to have_selector("dialog")
end

it "enables creation of a new property" do
within("dialog") do
fill_in "Name", with: "new prop"
fill_in "Presentation", with: "New prop"

click_on "Add Property"
end

expect(page).not_to have_selector("dialog")
expect(page).to have_content("Property was successfully created.")

expect(page).to have_content("New prop")
end
end
end
2 changes: 1 addition & 1 deletion bin/dev
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ if ! test -d sandbox; then
bin/sandbox
fi

export PORT=3000
export PORT=${PORT:-3000}
forkata marked this conversation as resolved.
Show resolved Hide resolved
exec foreman start -f Procfile.dev "$@"