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

29 lines
676 B
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 = ["counter"];
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
connect() {
2021-06-01 21:33:49 +00:00
if (!this.hasCounterTarget) {
2021-11-22 17:51:50 +00:00
console.error("Missing counter target");
return;
2021-06-01 21:33:49 +00:00
}
2021-11-22 17:51:50 +00:00
window.addEventListener(
"cart:counter",
(event) => (this.counter = event.detail.item_count)
);
window.addEventListener("storage", (event) => {
if (event.key == "cart:counter") this.counter = event.newValue;
});
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
if (!this.cart) return;
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
this.counter = this.cart.data.attributes.item_count;
2021-06-01 21:33:49 +00:00
}
2021-11-22 17:51:50 +00:00
set counter(quantity) {
this.counterTarget.innerText = quantity;
2021-06-01 21:33:49 +00:00
}
}