Skip to content

Commit 0a55391

Browse files
committed
Add "Select address" feature
Implemented the functionality for the 'Select Address' feature. This addition includes dynamic handling of address selection, enabling users to pick from a list of existing associated addresses.
1 parent c6efd71 commit 0a55391

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

admin/app/components/solidus_admin/orders/show/address/component.html.erb

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
1-
<div class="<%= stimulus_id %>">
1+
<div class="<%= stimulus_id %>" data-controller="<%= stimulus_id %>">
22
<%= render component("orders/show").new(order: @order) %>
3+
34
<%= render component("ui/modal").new(title: t(".title.#{@type}"), close_path: solidus_admin.order_path(@order)) do |modal| %>
45
<%= form_for @order, url: solidus_admin.send("order_#{@type}_address_path", @order), html: { id: form_id } do |form| %>
56
<div class="w-full flex flex-col mb-4">
6-
<h2 class="text-sm mb-4 font-semibold"><%= t(".subtitle.#{@type}") %></h2>
7+
<div class="flex justify-between items-center mb-4">
8+
<h2 class="text-sm font-semibold">
9+
<%= t(".subtitle.#{@type}") %>
10+
</h2>
11+
12+
<% if @user&.addresses&.any? %>
13+
<details class="text-black text-sm" data-controller="details-click-outside" data-<%= stimulus_id %>-target="addresses">
14+
<summary
15+
class="text-left flex cursor-pointer select-none"
16+
data-action="keydown.esc-><%= stimulus_id %>#close"
17+
>
18+
<%= t(".select_address") %>
19+
<%= render component("ui/icon").new(name: 'arrow-down-s-fill', class: 'w-5 h-5') %>
20+
</summary>
21+
22+
<div class="absolute mr-4 right-0 bg-white border border-gray-100 rounded-lg py-2 mt-1 shadow-lg z-10 min-w-[16rem] max-h-[26rem] overflow-y-auto">
23+
<% @user.addresses.each do |address| %>
24+
<%= tag.a(
25+
href: solidus_admin.send("order_#{@type}_address_path", @order, address_id: address.id),
26+
class: 'block text-black text-sm hover:bg-gray-50 p-2 mx-2 w-auto rounded-lg',
27+
) do %>
28+
<%= format_address(address) %>
29+
<% end %>
30+
<% end %>
31+
</div>
32+
</details>
33+
<% end %>
34+
</div>
35+
736
<div class="w-full flex gap-4">
837
<%= render component('ui/forms/address').new(address: @address, name: "order[#{@type}_address_attributes]") %>
938
</div>

admin/app/components/solidus_admin/orders/show/address/component.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ class SolidusAdmin::Orders::Show::Address::Component < SolidusAdmin::BaseCompone
55

66
VALID_TYPES = ['ship', 'bill'].freeze
77

8-
def initialize(order:, address:, type: 'ship')
8+
def initialize(order:, address:, user: nil, type: 'ship')
99
@order = order
10-
@type = validate_address_type(type)
10+
@user = user
1111
@address = address
12+
@type = validate_address_type(type)
1213
end
1314

1415
def form_id
@@ -24,6 +25,23 @@ def use_attribute
2425
end
2526
end
2627

28+
def format_address(address)
29+
safe_join([
30+
address.name,
31+
tag.br,
32+
address.address1,
33+
tag.br,
34+
address.address2,
35+
address.city,
36+
address.zipcode,
37+
address.state&.name,
38+
tag.br,
39+
address.country.name,
40+
tag.br,
41+
address.phone,
42+
], " ")
43+
end
44+
2745
def validate_address_type(type)
2846
VALID_TYPES.include?(type) ? type : raise(ArgumentError, "Invalid address type: #{type}")
2947
end

admin/app/components/solidus_admin/orders/show/address/component.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ en:
44
save: Save
55
cancel: Cancel
66
back: Back
7+
select_address: Select address
78
title:
89
ship: Edit Shipping Address
910
bill: Edit Billing Address

admin/app/controllers/solidus_admin/addresses_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def show
1414
format.html do
1515
render component('orders/show/address').new(
1616
order: @order,
17+
user: @order.user,
1718
address: address,
1819
type: address_type,
1920
)
@@ -35,6 +36,7 @@ def update
3536
format.html do
3637
render component('orders/show/address').new(
3738
order: @order,
39+
user: @order.user,
3840
address: @order.send("#{address_type}_address"),
3941
type: address_type,
4042
status: :unprocessable_entity,

0 commit comments

Comments
 (0)