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,44 @@
<div
id="calculator_fields"
class="js-calculator-fields no-border-bottom"
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: 'Calculator',
class: "fullwidth",
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
class="js-calculator-preferences"
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">
<%= render component("calculators/preference_fields/#{calculator.preference_type(name)}").new(
form: calculator_form,
attribute: "preferred_#{name}",
label: t(name, scope: 'spree', default: name.to_s.humanize)
) %>
</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,8 @@
# frozen_string_literal: true

class SolidusAdmin::Calculators::Form::Component < SolidusAdmin::BaseComponent
def initialize(form:, calculators:)
@form = form
@calculators = calculators
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= render component("ui/forms/field").text_field(
@form,
@attribute,
type: :number,
step: '0.01',
class: 'fullwidth'
) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class SolidusAdmin::Calculators::PreferenceFields::Decimal::Component < SolidusAdmin::BaseComponent
def initialize(form:, attribute:, label:)
@form = form
@attribute = attribute
@label = label
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= render component("ui/forms/field").text_field(
@form,
@attribute,
type: :number,
step: '1',
class: 'fullwidth'
) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class SolidusAdmin::Calculators::PreferenceFields::Integer::Component < SolidusAdmin::BaseComponent
def initialize(form:, attribute:, label:)
@form = form
@attribute = attribute
@label = label
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= render component("ui/forms/field").text_field(
@form,
@attribute,
class: 'fullwidth'
) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class SolidusAdmin::Calculators::PreferenceFields::String::Component < SolidusAdmin::BaseComponent
def initialize(form:, attribute:, label:)
@form = form
@attribute = attribute
@label = label
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%= page do %>
<%= page_header do %>
<%= page_header_back(back_url) %>
<%= page_header_title(@shipping_method.name) %>
<%= page_header_actions do %>
<%= render component("ui/button").new(
tag: :a,
text: t(".discard"),
scheme: :secondary,
href: back_url
) %>
<%= render component("ui/button").new(text: t(".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,6 @@
# Add your component translations here.
# Use the translation in the example in your template with `t(".hello")`.
en:
back: Back
discard: Discard
save: Save
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<%= 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) %>
<%= 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.all.map { |tc| [tc.name, tc.id] },
object: @shipping_method,
value: @shipping_method.tax_category_id,
class: "fullwidth"
) %>
<% 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.all.map { |tc| [tc.name, tc.id] },
object: @shipping_method,
value: @shipping_method.store_ids,
class: "fullwidth",
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.all.map { |tc| [tc.name, tc.id] },
object: @shipping_method,
value: @shipping_method.zone_ids,
class: "fullwidth",
multiple: true,
label: t(".zones")
) %>

<div data-controller="shipping-methods--form">
<label>
<%= hidden_field_tag "#{f.object_name}[available_to_all]", "0" %>
<%= render component("ui/forms/checkbox").new(
name: "#{f.object_name}[available_to_all]",
value: "1",
checked: f.object.available_to_all,
data: {
"shipping-methods--form-target": "stockLocationCheckbox",
"action": "change->shipping-methods--form#toggle"
}
) %>
<span class="font-semibold text-xs ml-2">
<%= Spree::ShippingMethod.human_attribute_name :available_to_all %>
</span>
</label>

<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.map { |tc| [tc.name, tc.id] },
object: @shipping_method,
value: @shipping_method.stock_location_ids,
class: "fullwidth",
multiple: true,
label: nil,
placeholder: t('.stock_locations_placeholder')
) %>
</div>
</div>

<label class="flex gap-2 items-center">
<%= hidden_field_tag "#{f.object_name}[available_to_users]", "0" %>
<%= render component("ui/forms/checkbox").new(
name: "#{f.object_name}[available_to_users]",
value: "1",
checked: f.object.available_to_users
) %>
<span class="font-semibold text-xs ml-2">
<%= Spree::ShippingMethod.human_attribute_name :available_to_users %>
</span>
</label>
</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.all.map { |tc| [tc.name, tc.id] },
object: @shipping_method,
value: @shipping_method.shipping_category_ids,
class: "fullwidth",
multiple: true,
label: t(".shipping_categories")
) %>
<% 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,16 @@
# Add your component translations here.
# Use the translation in the example in your template with `t(".hello")`.
en:
details: "Details"
zones: "Zones"
shipping_categories: "Shipping Categories"
tax_category: "Tax Category"
availability: "Availability"
stock_locations: "Stock Locations"
stores: "Stores"
Calculator: "Calculator"
stock_locations_placeholder: "Choose stock locations"
organization: "Organization"
taxation: "Taxation"
pricing: "Pricing"

Loading
Loading