mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-11 12:29:24 +00:00
Add auto basket invalidation
This commit is contained in:
@ -9,7 +9,7 @@ interface BasketItem {
|
||||
}
|
||||
|
||||
document.addEventListener("alpine:init", () => {
|
||||
Alpine.data("basket", () => ({
|
||||
Alpine.data("basket", (lastPurchaseTime?: number) => ({
|
||||
basket: [] as BasketItem[],
|
||||
|
||||
init() {
|
||||
@ -18,6 +18,16 @@ document.addEventListener("alpine:init", () => {
|
||||
this.saveBasket();
|
||||
});
|
||||
|
||||
// Invalidate basket if a purchase was made
|
||||
if (lastPurchaseTime !== null && localStorage.basketTimestamp !== undefined) {
|
||||
if (
|
||||
new Date(lastPurchaseTime) >=
|
||||
new Date(Number.parseInt(localStorage.basketTimestamp))
|
||||
) {
|
||||
this.basket = [];
|
||||
}
|
||||
}
|
||||
|
||||
// It's quite tricky to manually apply attributes to the management part
|
||||
// of a formset so we dynamically apply it here
|
||||
this.$refs.basketManagementForm
|
||||
@ -38,6 +48,7 @@ document.addEventListener("alpine:init", () => {
|
||||
|
||||
saveBasket() {
|
||||
localStorage.basket = JSON.stringify(this.basket);
|
||||
localStorage.basketTimestamp = Date.now();
|
||||
},
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user