sutty-base-jekyll-theme/_packs/controllers/cart_confirmation_controller.js
2021-11-22 14:51:50 -03:00

47 lines
1.1 KiB
JavaScript

import { CartBaseController } from "./cart_base_controller";
export default class extends CartBaseController {
static targets = ["order"];
async connect() {
if (this.clear) {
this.storage.clear();
window.dispatchEvent(
new CustomEvent("cart:counter", { detail: { item_count: 0 } })
);
}
if (!this.template) return;
if (this.storage.cart) {
const order = this.cart.data.attributes;
const products = this.products;
const site = window.site;
const shipping_address = JSON.parse(
this.storage.getItem("shipping_address")
);
const data = { order, products, site, shipping_address };
this.storage.setItem("confirmation", JSON.stringify(data));
} else {
data = JSON.parse(this.storage.getItem("confirmation"));
}
this.render(data);
}
render(data = {}) {
this.engine
.parseAndRender(this.template, data)
.then((html) => (this.orderTarget.innerHTML = html));
}
get clear() {
return this.element.dataset.clear;
}
get template() {
return window.templates[this.element.dataset.template];
}
}