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

48 lines
1.1 KiB
JavaScript
Raw Permalink 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 = ["order"];
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
async connect() {
if (this.clear) {
2021-11-22 17:51:50 +00:00
this.storage.clear();
window.dispatchEvent(
new CustomEvent("cart:counter", { detail: { item_count: 0 } })
);
}
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
if (!this.template) return;
2021-06-01 21:33:49 +00:00
if (this.storage.cart) {
2021-11-22 17:51:50 +00:00
const order = this.cart.data.attributes;
const products = this.products;
const site = window.site;
const shipping_address = JSON.parse(
this.storage.getItem("shipping_address")
);
2021-11-22 17:51:50 +00:00
const data = { order, products, site, shipping_address };
2021-11-22 17:51:50 +00:00
this.storage.setItem("confirmation", JSON.stringify(data));
} else {
2021-11-22 17:51:50 +00:00
data = JSON.parse(this.storage.getItem("confirmation"));
2021-06-01 21:33:49 +00:00
}
2021-11-22 17:51:50 +00:00
this.render(data);
2021-06-01 21:33:49 +00:00
}
2021-11-22 17:51:50 +00:00
render(data = {}) {
this.engine
.parseAndRender(this.template, data)
.then((html) => (this.orderTarget.innerHTML = html));
2021-06-01 21:33:49 +00:00
}
2021-11-22 17:51:50 +00:00
get clear() {
return this.element.dataset.clear;
2021-06-01 21:33:49 +00:00
}
2021-10-28 13:00:00 +00:00
2021-11-22 17:51:50 +00:00
get template() {
return window.templates[this.element.dataset.template];
2021-10-28 13:00:00 +00:00
}
2021-06-01 21:33:49 +00:00
}