mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2025-06-02 08:23:14 +00:00
Fix basket loading and verifying on page load
Update go mod files
This commit is contained in:
parent
5bcebe2011
commit
7d8d19a6e9
4 changed files with 33 additions and 26 deletions
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue