Fix basket loading and verifying on page load

Update go mod files
This commit is contained in:
Michał Gdula 2024-05-04 17:58:48 +01:00
parent 5bcebe2011
commit 7d8d19a6e9
4 changed files with 33 additions and 26 deletions

View file

@ -3,24 +3,30 @@ import { type Writable, get, writable } from "svelte/store";
import { type CartItem, type Item } from "./types";
import { getItemByUUID, postVerifyCart } from "./test-api";
// Load content from localstorage
let local: Record<string, CartItem> = {};
try {
await postVerifyCart(JSON.parse(localStorage.getItem("basket")))
function getLocal(): Record<string, CartItem> {
let localData: Record<string, CartItem> = {};
try {
localData = JSON.parse(localStorage.getItem("basket"));
} catch (error) {
console.error("Could parse basket JSON:", error);
return localData;
}
postVerifyCart(localData)
.then((data: Record<string, CartItem>) => {
local = data;
console.log(data);
localData = data;
})
.catch((error) => {
throw error; // Hot potato style
console.error("Could not load basket:", error);
});
} catch {
console.error("Failed to load cart");
return localData;
}
// Store function
function createCartStore() {
const cart: Writable<Record<string, CartItem>> = writable(local);
const cart: Writable<Record<string, CartItem>> = writable(getLocal());
async function addToCart(uuid: string, amount: number) {
if (get(cart)[uuid] !== undefined) {