Skip to content
Draft
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
@@ -0,0 +1,45 @@
<div
id="calculator_fields"
data-controller="calculators--form"
>
<div>
<div class="mb-3">
<%= render component("ui/forms/field").select(
@form.object_name,
:calculator_type,
@calculators.map { |c| [c.description, c.name] },
object: @form.object,
value: @form.object.calculator_type,
label: t('spree.calculator'),
data: {
"calculators--form-target": "select",
action: "change->calculators--form#toggle"
}
) %>
</div>

<% @calculators.each do |calculator_class| %>
<% calculator = @form.object.calculator.class == calculator_class ? @form.object.calculator : calculator_class.new %>
<div
data-calculators--form-target="preferences"
data-calculator-type="<%= calculator_class %>"
>
<%= @form.fields_for :calculator, calculator do |calculator_form| %>
<% calculator.admin_form_preference_names.each do |name| %>
<div class="mb-3">
<%=
current_value = params.dig(
:shipping_method,
:calculator_attributes,
"preferred_#{name}".to_sym
).presence || calculator_form.object.preferences[name]

render field_for(name, current_value, calculator)
%>
</div>
<% end %>
<% end %>
</div>
<% end %>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static targets = ["select", "preferences"]

connect() {
this.toggle()
}

toggle() {
const selectedType = this.selectTarget.value

this.preferencesTargets.forEach((el) => {
const isActive = el.dataset.calculatorType === selectedType

el.classList.toggle("hidden", !isActive)

el.querySelectorAll("input, select, textarea, button").forEach(input => {
input.disabled = !isActive
})
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

class SolidusAdmin::Calculators::Form::Component < SolidusAdmin::BaseComponent
def initialize(form:, calculators:)
@form = form
@calculators = calculators
end

def field_for(preference_name, current_value, calculator)
field_type = calculator.preference_type(preference_name)

component("ui/preference_fields/#{field_type}").new(
type: params.dig(:shipping_method, :calculator_type) || calculator.type,
name: "shipping_method[calculator_attributes][preferred_#{preference_name}]",
preference_name:,
value: (
(field_type == :boolean) ? current_value&.to_s&.presence : current_value
) || calculator.preference_default(preference_name)
)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= page id: :resource_form do %>
<%= page_header do %>
<%= page_header_back(back_url) %>
<%= page_header_title(@shipping_method.name) %>
<%= page_header_actions do %>
<%= render component("ui/button").discard(path: back_url) %>
<%= render component("ui/button").save(form: form_id) %>
<% end %>
<% end %>
<%= render component("shipping_methods/form").new(shipping_method: @shipping_method, url: form_url, id: form_id) %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class SolidusAdmin::ShippingMethods::Edit::Component < SolidusAdmin::Resources::Edit::Component
include SolidusAdmin::Layout::PageHelpers
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<%= form_for @shipping_method, url: @url, html: { id: @id } do |f| %>
<%= page_with_sidebar do %>
<%= page_with_sidebar_main do %>
<%= render component("ui/panel").new(title: t(".details")) do |panel| %>
<% panel.with_section(class: "flex flex-col gap-4") do %>
<div class="flex gap-4 w-full">
<%= render component("ui/forms/field").text_field(f, :name, required: true) %>
<%= render component("ui/forms/field").text_field(f, :admin_name) %>
</div>
<div class="flex gap-4 w-full">
<%= render component("ui/forms/field").text_field(f, :code) %>
<%= render component("ui/forms/field").text_field(f, :carrier) %>
<%= render component("ui/forms/field").text_field(f, :service_level) %>
</div>
<%= render component("ui/forms/field").text_field(
f,
:tracking_url,
placeholder: t('spree.tracking_url_placeholder')
) %>
<% end %>
<% end %>

<%= render component("ui/panel").new(title: t('.pricing')) do |panel| %>
<% panel.with_section do %>
<%= render component("calculators/form").new(
form: f,
calculators: @calculators
) %>
<% end %>
<% end %>

<%= render component("ui/panel").new(title: t(".taxation")) do |panel| %>
<% panel.with_section do %>
<%= render component("ui/forms/field").select(
f.object_name,
:tax_category_id,
Spree::TaxCategory.pluck(:name, :id),
object: @shipping_method,
value: @shipping_method.tax_category_id,
include_blank: true,
label: t(".tax_category")
) %>
<% end %>
<% end %>
<% end %>

<%= page_with_sidebar_aside do %>
<%= render component("ui/panel").new(title: t('.availability')) do |panel| %>
<% panel.with_section do %>
<div class="flex flex-col gap-4">
<%= hidden_field_tag "shipping_method[store_ids][]", "" %>
<%= render component("ui/forms/field").select(
f.object_name,
:store_ids,
Spree::Store.pluck(:name, :id),
object: @shipping_method,
value: @shipping_method.store_ids,
multiple: true,
label: t(".stores")
) %>

<%= hidden_field_tag "shipping_method[zone_ids][]", "" %>
<%= render component("ui/forms/field").select(
f.object_name,
:zone_ids,
Spree::Zone.pluck(:name, :id),
object: @shipping_method,
value: @shipping_method.zone_ids,
multiple: true,
label: t(".zones")
) %>

<div data-controller="shipping-methods--form">
<%= render component("ui/forms/checkbox").new(
object_name: f.object_name,
method: :available_to_all,
checked: f.object.available_to_all,
data: {
"shipping-methods--form-target": "stockLocationCheckbox",
"action": "change->shipping-methods--form#toggle"
}
) do |checkbox| %>
<%= checkbox.with_label(text: Spree::ShippingMethod.human_attribute_name(:available_to_all), weight: :semibold, size: :xs) %>
<% end %>

<div data-shipping-methods--form-target="stockLocations">
<%= hidden_field_tag "shipping_method[stock_location_ids][]", "" %>
<%= render component("ui/forms/field").select(
f.object_name,
:stock_location_ids,
Spree::StockLocation.order_default.pluck(:name, :id),
object: @shipping_method,
value: @shipping_method.stock_location_ids,
multiple: true,
label: nil,
placeholder: t('.stock_locations_placeholder')
) %>
</div>
</div>

<%= render component("ui/forms/checkbox").new(
object_name: f.object_name,
method: :available_to_users,
checked: f.object.available_to_users
) do |checkbox| %>
<%= checkbox.with_label(text: Spree::ShippingMethod.human_attribute_name(:available_to_users), weight: :semibold, size: :xs) %>
<% end %>
</div>
<% end %>
<% end %>

<%= render component("ui/panel").new(title: t(".organization")) do |panel| %>
<% panel.with_section do %>
<%= hidden_field_tag "shipping_method[shipping_category_ids][]", "" %>
<%= render component("ui/forms/field").select(
f.object_name,
:shipping_category_ids,
Spree::ShippingCategory.pluck(:name, :id),
object: @shipping_method,
value: @shipping_method.shipping_category_ids,
multiple: true,
label: t(".shipping_categories"),
required: true
) %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static targets = ["stockLocationCheckbox", "stockLocations"]

connect() {
this.toggle();
}

toggle() {
if (this.stockLocationCheckboxTarget.checked) {
this.stockLocationsTarget.classList.add("hidden");
} else {
this.stockLocationsTarget.classList.remove("hidden");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class SolidusAdmin::ShippingMethods::Form::Component < SolidusAdmin::BaseComponent
include SolidusAdmin::Layout::PageHelpers

def initialize(shipping_method:, id:, url:)
@shipping_method = shipping_method
@id = id
@url = url
@calculators = Rails.application.config.spree.calculators.shipping_methods
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
en:
details: "Details"
zones: "Zones"
shipping_categories: "Shipping Categories"
tax_category: "Tax Category"
availability: "Availability"
stores: "Stores"
stock_locations_placeholder: "Choose stock locations"
organization: "Organization"
taxation: "Taxation"
pricing: "Pricing"
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def model_class
Spree::ShippingMethod
end

def row_url(shipping_method)
spree.edit_admin_shipping_method_path(shipping_method)
def edit_path(shipping_method)
solidus_admin.edit_shipping_method_path(shipping_method)
end

def search_url
Expand All @@ -21,7 +21,7 @@ def page_actions
render component("ui/button").new(
tag: :a,
text: t(".add"),
href: spree.new_admin_shipping_method_path,
href: solidus_admin.new_shipping_method_path,
icon: "add-line",
class: "align-self-end w-full"
)
Expand All @@ -42,7 +42,10 @@ def columns
[
{
header: :name,
data: -> { [_1.admin_name.presence, _1.name].compact.join(" / ") }
data: ->(shipping_method) do
name = [shipping_method.admin_name.presence, shipping_method.name].compact.join(" / ")
link_to name, edit_path(shipping_method), class: "body-link"
end
},
{
header: :zone,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= page id: :resource_form do %>
<%= page_header do %>
<%= page_header_back(back_url) %>
<%= page_header_title(t('.title')) %>
<%= page_header_actions do %>
<%= render component("ui/button").discard(path: back_url) %>
<%= render component("ui/button").save(form: form_id) %>
<% end %>
<% end %>
<%= render component("shipping_methods/form").new(shipping_method: @shipping_method, url: form_url, id: form_id) %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class SolidusAdmin::ShippingMethods::New::Component < SolidusAdmin::Resources::New::Component
include SolidusAdmin::Layout::PageHelpers
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
en:
title: "New Shipping Method"
Loading
Loading