sutty-base-jekyll-theme/_packs/controllers/cart_shipping_controller.js

128 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-11-22 17:51:50 +00:00
import { CartBaseController } from "./cart_base_controller";
2021-06-01 21:33:49 +00:00
export default class extends CartBaseController {
2021-11-22 17:51:50 +00:00
static targets = ["methods", "rates", "form"];
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
connect() {
this.formTarget.addEventListener("formdata", (event) =>
this.processShippingAddress(event.formData)
);
2021-06-01 21:33:49 +00:00
}
2021-11-22 17:51:50 +00:00
async rates(event) {
event.preventDefault();
event.stopPropagation();
2021-06-01 21:33:49 +00:00
if (!this.formTarget.checkValidity()) {
2021-11-22 17:51:50 +00:00
this.adressTarget.classList.add("was-validated");
return;
2021-06-01 21:33:49 +00:00
}
2021-11-22 17:51:50 +00:00
this.formTarget.classList.remove("was-validated");
2021-06-01 21:33:49 +00:00
// FormDataEvent es muy reciente
if (window.FormDataEvent) {
// Esto lanza el evento formdata en el formulario
2021-11-22 17:51:50 +00:00
new FormData(event.target);
2021-06-01 21:33:49 +00:00
} else {
// Fallback
2021-11-22 17:51:50 +00:00
this.processShippingAddress(new FormData(event.target));
2021-06-01 21:33:49 +00:00
}
}
2021-11-22 17:51:50 +00:00
payment(event) {
event.preventDefault();
event.stopPropagation();
2021-06-01 21:33:49 +00:00
// FormDataEvent es muy reciente
if (window.FormDataEvent) {
// Esto lanza el evento formdata en el formulario
2021-11-22 17:51:50 +00:00
new FormData(event.target);
2021-06-01 21:33:49 +00:00
} else {
2021-11-22 17:51:50 +00:00
this.processShippingRate(new FormData(event.target));
2021-06-01 21:33:49 +00:00
}
}
2021-11-22 17:51:50 +00:00
async processShippingAddress(formData) {
this.formDisabled = true;
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
const email = this.email;
const orderToken = this.token;
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
const ship_address_attributes = this.formDataToObject(formData);
const bill_address_attributes = ship_address_attributes;
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
const response = await this.spree.checkout.orderUpdate(
{ orderToken },
{ order: { email, ship_address_attributes, bill_address_attributes } }
);
2021-06-01 21:33:49 +00:00
if (response.isFail()) {
2021-11-22 17:51:50 +00:00
this.handleFailure(response);
this.formDisabled = false;
return;
2021-06-01 21:33:49 +00:00
}
2021-11-22 17:51:50 +00:00
const shippingMethods = await this.shippingMethods(orderToken);
2021-06-01 21:33:49 +00:00
if (!shippingMethods) {
2021-11-22 17:51:50 +00:00
this.formDisabled = false;
return;
2021-06-01 21:33:49 +00:00
}
2021-11-22 17:51:50 +00:00
const shipping_rates = shippingMethods.included.filter(
(x) => x.type == "shipping_rate"
);
2021-06-01 21:33:49 +00:00
// XXX: No hay varios paquetes
2021-11-22 17:51:50 +00:00
const shipping_method = shippingMethods.data[0];
const site = window.site;
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
await this.render({ shipping_method, shipping_rates, site });
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
const nextStep = document.querySelector(
`#${this.element.dataset.scrollTo}`
);
if (nextStep) nextStep.scrollIntoView();
2021-06-01 21:33:49 +00:00
}
2021-11-22 17:51:50 +00:00
async processShippingRate(formData) {
const rate = this.formDataToObject(formData);
const orderToken = this.token;
2021-06-01 21:33:49 +00:00
// XXX: Deshabilitar el formulario después del evento FormData, de
// lo contrario el objeto queda vacío.
2021-11-22 17:51:50 +00:00
this.ratesTarget.elements.forEach((x) => (x.disabled = true));
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
const response = await window.spree.checkout.orderUpdate(
{ orderToken },
{ order: { shipments_attributes: [{ ...rate }] } }
);
2021-06-01 21:33:49 +00:00
if (response.isFail()) {
2021-11-22 17:51:50 +00:00
this.handleFailure(response);
return;
2021-06-01 21:33:49 +00:00
}
2021-11-22 17:51:50 +00:00
this.cart = response;
2021-06-01 21:33:49 +00:00
// Continue to next step
try {
2021-11-22 17:51:50 +00:00
Turbolinks.visit(this.data.get("next"));
2021-06-01 21:33:49 +00:00
} catch {
2021-11-22 17:51:50 +00:00
window.location = this.data.get("next");
2021-06-01 21:33:49 +00:00
}
}
2021-11-22 17:51:50 +00:00
async render(data = {}) {
const template = window.templates[this.data.get("template")];
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
this.methodsTarget.innerHTML = await this.engine.parseAndRender(
template,
data
);
this.ratesTarget.addEventListener("formdata", (event) =>
this.processShippingRate(event.formData)
);
2021-06-01 21:33:49 +00:00
}
}