mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
import { Controller } from "stimulus";
|
|
export default class extends Controller {
|
|
static targets = ["input"];
|
|
|
|
connect() {
|
|
this.inputTargets.forEach((i) => {
|
|
if (i.checked) {
|
|
this.doSelectPaymentMethod(i.dataset.paymentmethodId);
|
|
}
|
|
});
|
|
}
|
|
|
|
selectPaymentMethod(event) {
|
|
this.doSelectPaymentMethod(event.target.dataset.paymentmethodId);
|
|
|
|
const stripeCardSelector =
|
|
this.application.getControllerForElementAndIdentifier(
|
|
document
|
|
.getElementById(event.target.dataset.paymentmethodId)
|
|
.querySelector('[data-controller="stripe-cards"]'),
|
|
"stripe-cards"
|
|
);
|
|
stripeCardSelector?.initSelectedCard();
|
|
}
|
|
|
|
doSelectPaymentMethod(paymentMethodContainerId) {
|
|
Array.from(
|
|
document.getElementsByClassName("paymentmethod-container")
|
|
).forEach((e) => {
|
|
if (e.id === paymentMethodContainerId) {
|
|
e.style.display = "block";
|
|
this.removeDisabledAttributeOnInput(e);
|
|
} else {
|
|
e.style.display = "none";
|
|
this.addDisabledAttributeOnInput(e);
|
|
}
|
|
});
|
|
}
|
|
|
|
getFormElementsArray(container) {
|
|
return Array.from(container.querySelectorAll("input, select, textarea"));
|
|
}
|
|
|
|
addDisabledAttributeOnInput(container) {
|
|
this.getFormElementsArray(container).forEach((i) => {
|
|
i.disabled = true;
|
|
});
|
|
}
|
|
|
|
removeDisabledAttributeOnInput(container) {
|
|
this.getFormElementsArray(container).forEach((i) => {
|
|
i.disabled = false;
|
|
});
|
|
}
|
|
}
|