-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77b9f54
commit ba1f9e1
Showing
11 changed files
with
211 additions
and
5 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
admin/app/components/solidus_admin/orders/address/component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<div class="<%= stimulus_id %>" data-controller="<%= stimulus_id %>"> | ||
<%= page do %> | ||
<%= page_header do %> | ||
<%= page_header_back(solidus_admin.edit_order_path(@order)) %> | ||
<%= page_header_title(t(".title", address: t(".#{@type}_address"))) %> | ||
<%= page_header_actions do %> | ||
<%= render component("ui/button").new( | ||
tag: :a, | ||
scheme: :secondary, | ||
text: t(".cancel"), | ||
href: solidus_admin.edit_order_path(@order) | ||
) %> | ||
|
||
<%= render component("ui/button").new( | ||
tag: :button, | ||
text: t(".save"), | ||
form: form_id | ||
) %> | ||
<% end %> | ||
<% end %> | ||
|
||
<%= page_with_sidebar do %> | ||
<%= page_with_sidebar_main do %> | ||
<%= render component('ui/panel').new do %> | ||
<%= form_for @order, url: solidus_admin.send("order_#{@type}_address_path", @order), html: { id: form_id } do |f| %> | ||
<div class="w-full flex flex-col mb-4"> | ||
<h2 class="text-sm mb-4 font-semibold"><%= t(".#{@type}_address") %></h2> | ||
<div class="w-full flex gap-4"> | ||
<%= f.fields_for :"#{@type}_address" do |form| %> | ||
<%= render component('ui/forms/address').new(form: form, disabled: false) %> | ||
<% end %> | ||
</div> | ||
</div> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
|
||
<%= page_footer do %> | ||
<%= page_footer_actions do %> | ||
<%= render component("ui/button").new( | ||
tag: :a, | ||
scheme: :secondary, | ||
text: t(".cancel"), | ||
href: solidus_admin.edit_order_path(@order) | ||
) %> | ||
|
||
<%= render component("ui/button").new( | ||
tag: :button, | ||
text: t(".save"), | ||
form: form_id | ||
) %> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
</div> |
14 changes: 14 additions & 0 deletions
14
admin/app/components/solidus_admin/orders/address/component.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Controller } from '@hotwired/stimulus' | ||
|
||
export default class extends Controller { | ||
static targets = ['output'] | ||
|
||
typed(event) { | ||
this.text = event.currentTarget.value | ||
this.render() | ||
} | ||
|
||
render() { | ||
this.outputTarget.innerText = this.text | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
admin/app/components/solidus_admin/orders/address/component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
class SolidusAdmin::Orders::Address::Component < SolidusAdmin::BaseComponent | ||
include SolidusAdmin::Layout::PageHelpers | ||
|
||
VALID_TYPES = ['ship', 'bill'].freeze | ||
|
||
def initialize(order:, type: "ship") | ||
@order = order | ||
@type = validate_address_type(type) | ||
end | ||
|
||
def form_id | ||
@form_id ||= "#{stimulus_id}--form-#{@type}-#{@order.id}" | ||
end | ||
|
||
private | ||
|
||
def validate_address_type(type) | ||
VALID_TYPES.include?(type) ? type : raise(ArgumentError, "Invalid address type: #{type}") | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
admin/app/components/solidus_admin/orders/address/component.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Add your component translations here. | ||
# Use the translation in the example in your template with `t(".hello")`. | ||
en: | ||
save: Save | ||
cancel: Cancel | ||
back: Back | ||
title: "Edit %{address}" | ||
ship_address: Shipping Address | ||
bill_address: Billing Address |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
admin/app/controllers/solidus_admin/addresses_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin | ||
class AddressesController < BaseController | ||
include Spree::Core::ControllerHelpers::StrongParameters | ||
|
||
before_action :load_order | ||
before_action :validate_address_type | ||
|
||
def new | ||
address = @order.send("#{address_type}_address") | ||
@order.send("build_#{address_type}_address", country_id: default_country_id) if address.nil? | ||
address ||= @order.send("#{address_type}_address") | ||
address.country_id ||= default_country_id if address.country.nil? | ||
|
||
respond_to do |format| | ||
format.html { render component('orders/address').new(order: @order, type: address_type) } | ||
end | ||
end | ||
|
||
def update | ||
if @order.contents.update_cart(order_params) | ||
flash[:success] = t('spree.customer_details_updated') | ||
redirect_to edit_order_url(@order) | ||
else | ||
respond_to do |format| | ||
format.html { render component('orders/address').new(order: @order, type: address_type) } | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def address_type | ||
params[:address_type].presence_in(%w[bill ship]) | ||
end | ||
|
||
def validate_address_type | ||
unless address_type | ||
flash[:error] = t('spree.address_type_invalid') | ||
redirect_to spree.admin_order_url(@order) | ||
end | ||
end | ||
|
||
def default_country_id | ||
@default_country_id ||= begin | ||
country = Spree::Country.default | ||
country.id if Spree::Country.available.exists?(id: country.id) | ||
end | ||
end | ||
|
||
def load_order | ||
@order = Spree::Order.find_by!(number: params[:order_id]) | ||
authorize! action_name, @order | ||
end | ||
|
||
def order_params | ||
params.require(:order).permit( | ||
bill_address_attributes: permitted_address_attributes, | ||
ship_address_attributes: permitted_address_attributes | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
admin/spec/components/previews/solidus_admin/orders/address/component_preview.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
# @component "orders/address" | ||
class SolidusAdmin::Orders::Address::ComponentPreview < ViewComponent::Preview | ||
include SolidusAdmin::Preview | ||
|
||
def overview | ||
render_with_template | ||
end | ||
|
||
# @param address text | ||
# @param type text | ||
def playground(address: "address", type: "type") | ||
render component("orders/address").new(address: address, type: type) | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
...spec/components/previews/solidus_admin/orders/address/component_preview/overview.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<div class="mb-8"> | ||
<h6 class="text-gray-500 mb-3 mt-0"> | ||
Scenario 1 | ||
</h6> | ||
|
||
<%= render current_component.new(address: "address", type: "type") %> | ||
</div> |
16 changes: 16 additions & 0 deletions
16
admin/spec/components/solidus_admin/orders/address/component_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe SolidusAdmin::Orders::Address::Component, type: :component do | ||
it "renders the overview preview" do | ||
render_preview(:overview) | ||
end | ||
|
||
# it "renders something useful" do | ||
# render_inline(described_class.new(address: "address", type: "type")) | ||
# | ||
# expect(page).to have_text "Hello, components!" | ||
# expect(page).to have_css '.value' | ||
# end | ||
end |