sutty-base-jekyll-theme/_packs/controllers/cart_payment_methods_controller.js
Nulo 3a0c41b736 Limpiar eventos cuando el elemento o controlador que los escucha desaparece
Squashed commit of the following:

commit 482eea28821868f03ace33562e7bd34ab9a4478f
Merge: 5f48528 1c128f2
Author: f <f@sutty.nl>
Date:   Thu Nov 25 18:31:35 2021 -0300

    Merge branch 'master' into limpiar-eventos

commit 5f48528c28b0709bd859a4dc52a830f60bfedc6e
Author: f <f@sutty.nl>
Date:   Thu Nov 25 18:23:23 2021 -0300

    pretty

commit 70d05bc90a6cb64d1c4bfc39f48388af3fbc3c18
Merge: c4f33c0 ff1bc21
Author: Nulo <nulo@sutty.nl>
Date:   Thu Oct 28 16:46:31 2021 -0300

    Merge branch 'master' into limpiar-eventos

commit c4f33c084058002a10fc0ec2137ffe045826cfd2
Author: f <f@sutty.nl>
Date:   Thu Oct 28 14:52:41 2021 -0300

    limpiar eventos
2021-11-25 21:40:44 +00:00

106 lines
2.7 KiB
JavaScript

import { CartBaseController } from "./cart_base_controller";
/*
* Retrieves payment methods and redirect to external checkouts
*/
export default class extends CartBaseController {
static targets = ["form", "submit", "specialInstructions"];
async connect() {
const orderToken = this.token;
const response = await this.spree.checkout.paymentMethods({ orderToken });
this.change_event = this._change_event.bind(this);
if (response.isFail()) {
this.handleFailure(response);
return;
}
const payment_methods = response.success().data;
const site = window.site;
const cart = this.cart;
const next = { url: this.data.get("nextUrl") };
const back = { url: this.data.get("backUrl") };
this.render({ payment_methods, site, cart, next, back });
}
async render(data = {}) {
const template = window.templates[this.data.get("template")];
this.element.innerHTML = await this.engine.parseAndRender(template, data);
if (!this.hasSubmitTarget) return;
this.formTarget.elements.forEach((p) =>
p.addEventListener("change", this.change_event)
);
}
disconnect() {
if (!this.hasSubmitTarget) return;
this.formTarget.elements.forEach((p) =>
p.removeEventListener("change", this.change_event)
);
}
_change_event(event) {
this.submitTarget.disabled = false;
}
async pay(event) {
event.preventDefault();
event.stopPropagation();
this.formDisabled = true;
const payment_method_id = this.formTarget.elements.payment_method_id.value;
const orderToken = this.token;
const special_instructions = this.specialInstructionsTarget.value.trim();
// XXX: Currently SpreeClient expects us to send payment source
// attributes as if it were a credit card.
let response = await this.spree.checkout.orderUpdate(
{ orderToken },
{
order: {
special_instructions,
payments_attributes: [{ payment_method_id }],
payment_source: {
[payment_method_id]: {
name: "Pepitx",
month: 12,
year: 2020,
},
},
},
}
);
if (response.isFail()) {
this.handleFailure(response);
this.formDisabled = false;
return;
}
this.cart = response;
response = await this.spree.checkout.complete({ orderToken });
if (response.isFail()) {
this.handleFailure(response);
this.formDisabled = false;
return;
}
this.cart = response;
const checkoutUrls = await this.spree.sutty.getCheckoutURL({ orderToken });
let redirectUrl = this.data.get("nextUrl");
if (checkoutUrls.data.length > 0) redirectUrl = checkoutUrls.data[0];
window.location = redirectUrl;
}
}