mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-03 02:21:33 +00:00
Instead of selecting the controller, send an event handled by stripe-cards
Using a query selector to find controller in order to call method could be dangerous as the DOM can change. Using an event should be more robust.
This commit is contained in:
@@ -12,17 +12,12 @@ export default class extends Controller {
|
||||
|
||||
selectPaymentMethod(event) {
|
||||
this.setPaymentMethod(event.target.dataset.paymentmethodId);
|
||||
|
||||
const stripeCardSelector =
|
||||
this.application.getControllerForElementAndIdentifier(
|
||||
document
|
||||
.querySelector(
|
||||
`[data-paymentmethod-id="${event.target.dataset.paymentmethodId}"]`
|
||||
)
|
||||
.querySelector('[data-controller="stripe-cards"]'),
|
||||
"stripe-cards"
|
||||
);
|
||||
stripeCardSelector?.initSelectedCard();
|
||||
// Send an event to the right (ie. the one with the same paymentmethodId)
|
||||
// StripeCardsController to initialize the form elements with the selected card
|
||||
const customEvent = new CustomEvent("stripecards:initSelectedCard", {
|
||||
detail: event.target.dataset.paymentmethodId,
|
||||
});
|
||||
document.dispatchEvent(customEvent);
|
||||
}
|
||||
|
||||
setPaymentMethod(paymentMethodContainerId) {
|
||||
|
||||
Reference in New Issue
Block a user