diff --git a/app/views/split_checkout/_payment.html.haml b/app/views/split_checkout/_payment.html.haml index 18459a21a2..bf02cf0e54 100644 --- a/app/views/split_checkout/_payment.html.haml +++ b/app/views/split_checkout/_payment.html.haml @@ -11,7 +11,8 @@ name: "order[payments_attributes][][payment_method_id]", checked: (payment_method.id == selected_payment_method), "data-action": "paymentmethod#selectPaymentMethod", - "data-paymentmethod-id": "paymentmethod#{payment_method.id}" + "data-paymentmethod-id": "paymentmethod#{payment_method.id}", + "data-paymentmethod-target": "input" = f.label :payment_method_id, "#{payment_method.name} (#{payment_or_shipping_price(payment_method, @order)})", for: "payment_method_#{payment_method.id}" = f.error_message_on :payment_method, standalone: true diff --git a/app/webpacker/controllers/paymentmethod_controller.js b/app/webpacker/controllers/paymentmethod_controller.js index f352430b71..7445048bf5 100644 --- a/app/webpacker/controllers/paymentmethod_controller.js +++ b/app/webpacker/controllers/paymentmethod_controller.js @@ -1,9 +1,20 @@ import { Controller } from "stimulus"; export default class extends Controller { - static targets = ["paymentMethod"]; + static targets = ["input"]; + + connect() { + this.inputTargets.forEach((i) => { + if (i.checked) { + this.doSelectPaymentMethod(i.dataset.paymentmethodId); + } + }); + } selectPaymentMethod(event) { - const paymentMethodContainerId = event.target.dataset.paymentmethodId; + this.doSelectPaymentMethod(event.target.dataset.paymentmethodId); + } + + doSelectPaymentMethod(paymentMethodContainerId) { Array.from( document.getElementsByClassName("paymentmethod-container") ).forEach((e) => { diff --git a/spec/javascripts/stimulus/paymentmethod_controller_test.js b/spec/javascripts/stimulus/paymentmethod_controller_test.js index aab38a9615..a82fdc53c0 100644 --- a/spec/javascripts/stimulus/paymentmethod_controller_test.js +++ b/spec/javascripts/stimulus/paymentmethod_controller_test.js @@ -9,23 +9,23 @@ describe("PaymentmethodController", () => { describe("#selectPaymentMethod", () => { beforeEach(() => { document.body.innerHTML = `
- - - + + + -