diff --git a/front/src/App.svelte b/front/src/App.svelte index 79f9c18..d490fb4 100644 --- a/front/src/App.svelte +++ b/front/src/App.svelte @@ -17,7 +17,7 @@ let cartItemCount = 0; Cart.subscribe(() => { - cartItemCount = Cart.getLength(); + cartItemCount = Cart.getTotalLength(); }); let scrollY = 0; @@ -72,14 +72,14 @@ {:else} {/if} diff --git a/front/src/components/BasketItem.svelte b/front/src/components/BasketItem.svelte new file mode 100644 index 0000000..d8d3760 --- /dev/null +++ b/front/src/components/BasketItem.svelte @@ -0,0 +1,69 @@ + + +
+ {#if item.data.image} + Item + {:else} + Item + {/if} + + +
+ + \ No newline at end of file diff --git a/front/src/lib/cart.ts b/front/src/lib/cart.ts index 45213a8..d94d590 100644 --- a/front/src/lib/cart.ts +++ b/front/src/lib/cart.ts @@ -1,5 +1,9 @@ import { get, writable } from "svelte/store"; +import type { CartItem } from './types'; +import { getItemByUUID } from "./test-api"; + + // Load content from localstorage let local = []; try { @@ -12,10 +16,10 @@ try { function createCartStore() { const cart = writable(local); - function addToCart(uuid: string, amount: number) { + async function addToCart(uuid: string, amount: number) { let found = false; - get(cart).forEach((item) => { + get(cart).forEach((item: CartItem) => { if (item.uuid === uuid) { item.amount += amount; found = true; @@ -23,24 +27,33 @@ function createCartStore() { }); if (!found) { - cart.update((cart) => [...cart, {uuid:uuid,amount:amount}]); + const newItem: CartItem = { + uuid: uuid, + amount: amount, + data: await getItemByUUID(uuid), + }; + cart.update((cart: CartItem[]) => [...cart, newItem]); } // Remove items that have an amount of 0 or lower cart.update((cart) => cart.filter((item) => item.amount > 0)) } - function getLength() { + function getUniqueLength() { return get(cart).length; } - function getByUUID(uuid: string) { + function getTotalLength() { + let amounts = get(cart).map((item) => item.amount); + return amounts.reduce((a, b) => a + b, 0); + } + + function getTotalPrice() { + let price = 0; get(cart).forEach((item) => { - if (item.uuid === uuid) { - return item; - } + price += item.amount * item.data.price; }) - return {}; + return price; } function removeByUUID(uuid: string) { @@ -50,8 +63,9 @@ function createCartStore() { return { ...cart, addToCart, - getLength, - getByUUID, + getUniqueLength, + getTotalLength, + getTotalPrice, removeByUUID, } } diff --git a/front/src/lib/test-data.ts b/front/src/lib/test-data.ts index a62f50c..7e455a9 100644 --- a/front/src/lib/test-data.ts +++ b/front/src/lib/test-data.ts @@ -49,7 +49,7 @@ const TestData: Item[] = [ name: "GwaGwa", price: 69, labels: [Labels.nut], - image: "/dab.jpg", + // image: "/dab.jpg", }, { uuid: "hogmelon", diff --git a/front/src/lib/types.ts b/front/src/lib/types.ts index a3a3aa7..c2ef630 100644 --- a/front/src/lib/types.ts +++ b/front/src/lib/types.ts @@ -1,3 +1,11 @@ +export enum Labels { + vegan = "VEGAN", + fish = "FISH", + nut = "NUT", + spicy = "SPICY", + gluten = "GLUTEN", +} + export interface Item { uuid: string, name: string, @@ -7,10 +15,10 @@ export interface Item { image?: string, } -export enum Labels { - vegan = "VEGAN", - fish = "FISH", - nut = "NUT", - spicy = "SPICY", - gluten = "GLUTEN", +// UUID is stored in both Item and CartItem, this isn't the best, I don't like it +// But it's the simplest way of doing this shit +export interface CartItem { + uuid: string, + amount: number, + data: Item, } diff --git a/front/src/pages/PageCart.svelte b/front/src/pages/PageCart.svelte index 6b0f8ef..1ad71a4 100644 --- a/front/src/pages/PageCart.svelte +++ b/front/src/pages/PageCart.svelte @@ -1,32 +1,75 @@ - -

Shopping Cart

+

Cart

-{#await items} - -{:then items} - {#if items.length} - - {:else} -

Empty.....

- {/if} + +

Order total: £{totalPrice}

- +{#if items.length > 0} + {#each items as item} +
+ +
+ {/each} +{:else} +

Empty.....

+{/if} + +
+ +

Looking for something more?

+{#await popularToday} +

Loading

+{:then popularToday} + {/await} +
+

Looking past orders? Check out the commonly asked questions

+ + \ No newline at end of file