diff --git a/front/src/lib/cart.ts b/front/src/lib/cart.ts index d771e30..45213a8 100644 --- a/front/src/lib/cart.ts +++ b/front/src/lib/cart.ts @@ -12,14 +12,37 @@ try { function createCartStore() { const cart = writable(local); - function addToCart(item: any) { - cart.update((cart) => [...cart, item]); + function addToCart(uuid: string, amount: number) { + let found = false; + + get(cart).forEach((item) => { + if (item.uuid === uuid) { + item.amount += amount; + found = true; + } + }); + + if (!found) { + cart.update((cart) => [...cart, {uuid:uuid,amount:amount}]); + } + + // Remove items that have an amount of 0 or lower + cart.update((cart) => cart.filter((item) => item.amount > 0)) } function getLength() { return get(cart).length; } + function getByUUID(uuid: string) { + get(cart).forEach((item) => { + if (item.uuid === uuid) { + return item; + } + }) + return {}; + } + function removeByUUID(uuid: string) { cart.update((cart) => cart.filter((item) => item.uuid !== uuid)) } @@ -28,6 +51,7 @@ function createCartStore() { ...cart, addToCart, getLength, + getByUUID, removeByUUID, } } diff --git a/front/src/lib/test-api.js b/front/src/lib/test-api.js index 98505d3..16a61db 100644 --- a/front/src/lib/test-api.js +++ b/front/src/lib/test-api.js @@ -18,7 +18,7 @@ export async function getAnnouncements() { image: "/BannerExampleImage.jpg", }; cache.announcement_banner = data; - await fakeDelay(5000) + await fakeDelay(200) return data; } @@ -30,7 +30,7 @@ export async function getPopularToday() { const data = Items; cache.popular_today = data; - await fakeDelay(2000) + await fakeDelay(200) return data; } @@ -62,9 +62,9 @@ export async function getItemByUUID(uuid) { if (item.uuid === uuid) { data = item; } - }) + }); - await fakeDelay(1000) + await fakeDelay(200) if (!data) { throw new Error("Resource could not be found"); @@ -74,8 +74,29 @@ export async function getItemByUUID(uuid) { } +export async function getItemsByUUID(items) { + let data = []; + + Items.forEach((itemInDatabase) => { + items.forEach((itemInRequest) => { + if (itemInDatabase.uuid === itemInRequest) { + data.push(itemInDatabase); + } + }); + }); + + await fakeDelay(200) + + if (data.length < 0) { + throw new Error("Resource could not be found"); + } + + return data; +} + + export async function postContactEmail(name, email, message) { - await fakeDelay(1000) + await fakeDelay(200) if (!name) { throw new Error("Namey missing"); diff --git a/front/src/pages/PageCart.svelte b/front/src/pages/PageCart.svelte index 463bb1c..b3001e1 100644 --- a/front/src/pages/PageCart.svelte +++ b/front/src/pages/PageCart.svelte @@ -1,22 +1,32 @@

Shopping Cart

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

Empty.....

+ {/if} -
    - {#each $Cart as item} -
  • - -
  • - {/each} -
+
    + {#each items as item} +
  • + +
  • + {/each} +
+{/await}

Looking past orders? Check out the commonly asked questions

diff --git a/front/src/pages/PageItem.svelte b/front/src/pages/PageItem.svelte index d23505c..759a6ef 100644 --- a/front/src/pages/PageItem.svelte +++ b/front/src/pages/PageItem.svelte @@ -1,5 +1,4 @@ @@ -66,7 +57,7 @@

{item.detail}

- + {:catch error}