Skip to content

Commit 4bdd857

Browse files
committed
[wip] Add orders/show/shipment component
1 parent aacc797 commit 4bdd857

File tree

14 files changed

+171
-10
lines changed

14 files changed

+171
-10
lines changed

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

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<div class="<%= stimulus_id %>" data-controller="<%= stimulus_id %>">
22
<div class="rounded p-2">
33
<%= render component('ui/panel').new do |panel| %>
4+
<% panel.with_menu t(".edit_shipment"), solidus_admin.order_shipments_path(@order, shipment_id: @shipment.id) %>
5+
46
<% panel.with_section(wide: true, high: true) do %>
57
<section class="border-gray-100 border-t w-full first-of-type:border-t-0 p-6">
68
<h2>
79
<span class="text-xl">
8-
#<%= @index %>: <%= @shipment.number %> from <%= @shipment.stock_location.name %> <%= render component('ui/badge').new(name: @shipment.state.titleize) %>
10+
<%= t('.title', index: @index, number: @shipment.number, location: @shipment.stock_location.name) %>
11+
<%= render component('ui/badge').new(name: @shipment.state.titleize) %>
912
</span>
1013
</h2>
1114
</section>
@@ -16,10 +19,20 @@
1619
<table class="table-auto w-full">
1720
<thead>
1821
<tr>
19-
<th class="text-left body-small-bold text-gray-800 bg-gray-15 px-6 py-3 leading-none"><%= t(".product") %></th>
20-
<th class="text-left body-small-bold text-gray-800 bg-gray-15 px-6 py-3 leading-none w-16"><%= t(".quantity") %></th>
21-
<th class="text-left body-small-bold text-gray-800 bg-gray-15 px-6 py-3 leading-none w-16 whitespace-nowrap"><%= t(".total") %></th>
22-
<th class="text-left body-small-bold text-gray-800 bg-gray-15 px-6 py-3 leading-none w-16"><span class="sr-only"><%= t(".actions") %></span></th>
22+
<th class="text-left body-small-bold text-gray-800 bg-gray-15 px-6 py-3 leading-none">
23+
<%= t(".product") %>
24+
</th>
25+
<th class="text-left body-small-bold text-gray-800 bg-gray-15 px-6 py-3 leading-none w-16">
26+
<%= t(".quantity") %>
27+
</th>
28+
<th class="text-left body-small-bold text-gray-800 bg-gray-15 px-6 py-3 leading-none w-16 whitespace-nowrap">
29+
<%= t(".total") %>
30+
</th>
31+
<th class="text-left body-small-bold text-gray-800 bg-gray-15 px-6 py-3 leading-none w-16">
32+
<span class="sr-only">
33+
<%= t(".actions") %>
34+
</span>
35+
</th>
2336
</tr>
2437
</thead>
2538
<tbody>
@@ -75,8 +88,6 @@
7588
</span>
7689
<%= @shipment.shipping_method.name %> - <%= @shipment.display_cost %>
7790
</label>
78-
79-
<%= render component("ui/icon").new(name: 'edit-line', class: 'w-5 h-5 cursor-pointer') %>
8091
</li>
8192
<li class="flex justify-between py-1.5">
8293
<label class="flex flex-col w-full py-1.5">
@@ -85,8 +96,6 @@
8596
</span>
8697
<%= @shipment.tracking || t(".none") %>
8798
</label>
88-
89-
<%= render component("ui/icon").new(name: 'edit-line', class: 'w-5 h-5 cursor-pointer') %>
9099
</li>
91100
</ul>
92101
</td>

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

+3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# frozen_string_literal: true
22

33
class SolidusAdmin::Orders::Show::Shipment::Component < SolidusAdmin::BaseComponent
4+
include SolidusAdmin::Layout::PageHelpers
5+
46
def initialize(shipment:, index:)
57
@shipment = shipment
8+
@order = shipment.order
69
@index = index
710
end
811

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
en:
2+
title: "#%{index}: %{number} from %{location}"
23
product: Product
34
quantity: Quantity
45
total: Total Price
56
actions: Actions
67
none: No tracking details provided
8+
edit_shipment: Edit shipment
79
inventory_states:
810
backordered: Backordered
911
canceled: Canceled
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<div class="<%= stimulus_id %>">
2+
<%= render component("orders/show").new(order: @order) %>
3+
<%= render component("ui/modal").new(title: t(".title", number: @shipment.number), close_path: close_path) do |modal| %>
4+
<%= form_for @shipment, url: solidus_admin.order_shipments_path(@order, shipment_id: @shipment.id), html: { id: form_id } do |f| %>
5+
<%= render component("ui/forms/field").text_field(f, :tracking) %>
6+
<%= render component("ui/forms/field").select(
7+
f,
8+
:selected_shipping_rate_id,
9+
@shipment.shipping_rates.map { |sr| [sr.shipping_method.name, sr.id] },
10+
) %>
11+
<% end %>
12+
13+
<% modal.with_actions do %>
14+
<%= render component("ui/button").new(tag: :a, scheme: :secondary, href: close_path, type: :submit, text: t('.cancel')) %>
15+
<%= render component("ui/button").new(form: form_id, type: :submit, text: t('.submit')) %>
16+
<% end %>
17+
<% end %>
18+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Controller } from '@hotwired/stimulus'
2+
3+
export default class extends Controller {
4+
static targets = ['output']
5+
6+
typed(event) {
7+
this.text = event.currentTarget.value
8+
this.render()
9+
}
10+
11+
render() {
12+
this.outputTarget.innerText = this.text
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
class SolidusAdmin::Orders::Show::Shipment::Edit::Component < SolidusAdmin::BaseComponent
4+
include SolidusAdmin::Layout::PageHelpers
5+
6+
def initialize(shipment:)
7+
@order = shipment.order
8+
@shipment = shipment
9+
end
10+
11+
def form_id
12+
dom_id(@order, "#{stimulus_id}_shipment_form_#{@shipment.id}")
13+
end
14+
15+
def close_path
16+
@close_path ||= solidus_admin.order_path(@order)
17+
end
18+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
en:
2+
title: "Edit shipment %{number}"
3+
submit: "Save"
4+
cancel: "Cancel"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
class SolidusAdmin::ShipmentsController < SolidusAdmin::BaseController
4+
include Spree::Core::ControllerHelpers::StrongParameters
5+
6+
before_action :load_order, :load_shipment, only: [:show, :update]
7+
8+
def show
9+
render component('orders/show/shipment/edit').new(shipment: @shipment)
10+
end
11+
12+
def update
13+
if @shipment.update_attributes_and_order(shipment_params)
14+
redirect_to order_path(@order), status: :see_other, notice: t('.success')
15+
else
16+
flash.now[:error] = @order.errors[:base].join(", ") if @order.errors[:base].any?
17+
18+
respond_to do |format|
19+
format.html do
20+
render component('orders/show/shipment/edit').new(order: @order), status: :unprocessable_entity
21+
end
22+
end
23+
end
24+
end
25+
26+
private
27+
28+
def load_order
29+
@order = Spree::Order.find_by!(number: params[:order_id])
30+
end
31+
32+
def load_shipment
33+
@shipment = @order.shipments.find_by(id: params[:shipment_id])
34+
end
35+
36+
def shipment_params
37+
if params[:shipment] && !params[:shipment].empty?
38+
params.require(:shipment).permit(permitted_shipment_attributes)
39+
else
40+
{}
41+
end
42+
end
43+
44+
def authorization_subject
45+
@order || Spree::Order
46+
end
47+
end

admin/config/locales/shipments.en.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
en:
2+
solidus_admin:
3+
shipments:
4+
title: "Shipments"
5+
update:
6+
success: "Shipment was updated successfully"
7+
error: "Shipment could not be updated"

admin/config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
resource :customer
3939
resource :ship_address, only: [:show, :edit, :update], controller: "addresses", type: "ship"
4040
resource :bill_address, only: [:show, :edit, :update], controller: "addresses", type: "bill"
41+
resource :shipments
4142

4243
member do
4344
get :variants_for

admin/lib/solidus_admin/configuration.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Configuration < Spree::Preferences::Configuration
4444
# Setting this to `true` enables access to alpha stage features that might still be in testing or development.
4545
# Use with caution, as these features may not be fully stable or complete.
4646
# Default: false
47-
preference :enable_alpha_features, :boolean, default: false
47+
preference :enable_alpha_features, :boolean, default: true
4848

4949
alias enable_alpha_features? enable_alpha_features
5050

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
# @component "orders/show/shipment/edit"
4+
class SolidusAdmin::Orders::Show::Shipment::Edit::ComponentPreview < ViewComponent::Preview
5+
include SolidusAdmin::Preview
6+
7+
def overview
8+
render_with_template
9+
end
10+
11+
# @param shipment text
12+
def playground(shipment: "shipment")
13+
render component("orders/show/shipment/edit").new(shipment: shipment)
14+
end
15+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="mb-8">
2+
<h6 class="text-gray-500 mb-3 mt-0">
3+
Scenario 1
4+
</h6>
5+
6+
<%= render current_component.new(shipment: "shipment") %>
7+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe SolidusAdmin::Orders::Show::Shipment::Edit::Component, type: :component do
6+
it "renders the overview preview" do
7+
render_preview(:overview)
8+
end
9+
10+
# it "renders something useful" do
11+
# render_inline(described_class.new(shipment: "shipment"))
12+
#
13+
# expect(page).to have_text "Hello, components!"
14+
# expect(page).to have_css '.value'
15+
# end
16+
end

0 commit comments

Comments
 (0)