Improve Carts reliability

Nicer empty cart page
Check on page load if cart is valid
This commit is contained in:
Michał Gdula 2024-05-03 12:31:12 +01:00
parent 67c8794427
commit 764aaa48ea
5 changed files with 117 additions and 31 deletions

View file

@ -1,13 +1,21 @@
import { get, writable } from "svelte/store";
import type { CartItem } from './types';
import { getItemByUUID } from "./test-api";
import type { Item, CartItem } from './types';
import { getItemByUUID, postVerifyCart } from "./test-api";
// Load content from localstorage
let local = [];
let local: CartItem[] = [];
try {
local = JSON.parse(localStorage.getItem("basket")) || []
let verified = await postVerifyCart(
JSON.parse(localStorage.getItem("basket")) || []
);
if (verified instanceof Error) {
throw new Error("Bruh");
}
local = <CartItem[]>verified;
} catch {
console.error("Failed to load cart")
}
@ -27,12 +35,22 @@ function createCartStore() {
});
if (!found) {
const newItem: CartItem = {
uuid: uuid,
amount: amount,
data: await getItemByUUID(uuid),
};
cart.update((cart: CartItem[]) => [...cart, newItem]);
let cartData: Item;
try {
let data: Item | Error = await getItemByUUID(uuid);
if (data instanceof Error) {
return;
}
cartData = <Item>data;
} finally {
const newItem: CartItem = {
uuid: uuid,
amount: amount,
data: cartData,
};
cart.update((cart: CartItem[]) => [...cart, newItem]);
}
}
// Remove items that have an amount of 0 or lower

View file

@ -1,10 +1,10 @@
import type { Item } from './types';
import type {CartItem, Item} from './types';
import TestData from './test-data';
let cache = {
announcement_banner: null,
popular_today: null,
announcement_banner: undefined,
popular_today: undefined,
};
@ -15,28 +15,30 @@ async function fakeDelay(timeout: number = 1000) {
}
export async function getAnnouncements() {
export async function getAnnouncements(): Promise<{image: string}> {
if (cache.announcement_banner) {
return cache.announcement_banner;
}
const data = {
image: "/BannerExampleImage.jpg",
};
cache.announcement_banner = data;
await fakeDelay(200)
return data;
}
export async function getPopularToday() {
export async function getPopularToday(): Promise<Item[]> {
if (cache.popular_today) {
return cache.popular_today;
}
const data = TestData;
const data: Item[] = TestData;
cache.popular_today = data;
await fakeDelay(200)
return data;
}
@ -61,8 +63,8 @@ export async function getMenuItems() {
}
export async function getItemsByUUID(items: string[]) {
let data = [];
export async function getItemsByUUID(items: string[]): Promise<Item[] | Error> {
let data: Item[] = [];
TestData.forEach((itemInDatabase: Item) => {
items.forEach((itemInRequest) => {
@ -82,9 +84,12 @@ export async function getItemsByUUID(items: string[]) {
}
export async function getItemByUUID(uuid: string) {
let data = await getItemsByUUID([uuid]);
export async function getItemByUUID(uuid: string): Promise<Item | Error> {
let data: Item[] | Error = await getItemsByUUID([uuid]);
if (data instanceof Error) {
throw new Error("Resource could not be found");
}
if (data.length != 1) {
throw new Error("Resource could not be found");
}
@ -93,7 +98,7 @@ export async function getItemByUUID(uuid: string) {
}
export async function postContactEmail(name: string, email: string, message: string) {
export async function postContactEmail(name: string, email: string, message: string): Promise<string | Error> {
await fakeDelay(200)
if (!name) {
@ -110,3 +115,36 @@ export async function postContactEmail(name: string, email: string, message: str
return "Check your email to confirm the message!";
}
export async function postVerifyCart(currentCartData: CartItem[]): Promise<CartItem[] | Error> {
if (currentCartData.length <= 0) {
return [];
}
let itemUUIDs: string[] = currentCartData.map((item) => item.uuid);
let verifiedItems: Item[] | Error = await getItemsByUUID(itemUUIDs);
if (verifiedItems instanceof Error) {
return new Error("Could not collect new cart information");
}
let newCartData: CartItem[] = [];
currentCartData.forEach((currentItem) => {
let data: Item;
verifiedItems.forEach((verifiedItem) => {
if (verifiedItem.uuid === currentItem.uuid) {
data = verifiedItem;
}
})
if (data) {
newCartData.push({
uuid: currentItem.uuid,
amount: currentItem.amount,
data: data,
});
}
});
return newCartData;
}

View file

@ -49,7 +49,7 @@ const TestData: Item[] = [
name: "GwaGwa",
price: 69,
labels: [Labels.nut],
// image: "/dab.jpg",
image: "/dab.jpg",
},
{
uuid: "hogmelon",