sutty-base-jekyll-theme/_packs/controllers/cart_coupon_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

81 lines
2 KiB
JavaScript

import { CartBaseController } from "./cart_base_controller";
/*
* Retrieves shipping methods
*/
export default class extends CartBaseController {
static targets = ["couponCodeInvalid", "preDiscount", "total"];
connect() {
this.input_event = this._input_event.bind(this);
this.couponCode.addEventListener("input", this.input_event);
}
disconnect() {
this.couponCode.removeEventListener("input", this.input_event);
}
_input_event(event) {
this.couponCode.parentElement.classList.remove("was-validated");
this.couponCode.setCustomValidity("");
}
get couponCode() {
if (!this._couponCode) this._couponCode = this.element.elements.coupon_code;
return this._couponCode;
}
get couponCodeInvalid() {
return this.hasCouponCodeInvalidTarget
? this.couponCodeInvalidTarget
: document.querySelector("#coupon-code-invalid");
}
get preDiscount() {
return this.hasPreDiscountTarget
? this.preDiscountTarget
: document.querySelector("#pre-discount");
}
get total() {
return this.hasTotalTarget
? this.totalTarget
: document.querySelector("#total");
}
set total(total) {
this.total.innerHTML = total;
}
async apply(event = undefined) {
event?.preventDefault();
event?.stopPropagation();
const orderToken = this.token;
const coupon_code = this.couponCode.value;
const include = "line_items";
const response = await window.spree.cart.applyCouponCode(
{ orderToken },
{ coupon_code, include }
);
this.element.elements.forEach((x) => (x.disabled = true));
if (response.isFail()) {
this.couponCodeInvalid.innerHTML = response.fail().summary;
this.couponCode.setCustomValidity(response.fail().summary);
this.couponCode.parentElement.classList.add("was-validated");
this.element.elements.forEach((x) => (x.disabled = false));
return;
}
this.cart = response;
this.total = response.success().data.attributes.total;
this.preDiscount.classList.remove("d-none");
}
}