diff --git a/front/src/lib/api.ts b/front/src/lib/api.ts index dc3735e..6117cdc 100644 --- a/front/src/lib/api.ts +++ b/front/src/lib/api.ts @@ -1,36 +1,16 @@ import { type Item, type JSONResponse } from "./types"; -import TestData from "./test-data"; const API_URL = "http://127.0.0.1:8080"; export async function getPopularToday(): Promise { const response = await fetch(`${API_URL}/api/items`); - const { data, error }: JSONResponse = await response.json(); + const {data, error}: JSONResponse = await response.json(); if (response.ok) { if (data?.item) { return data?.item; - } else { - return Promise.reject(new Error("Failed to fetch popular today")); } - } else { - return Promise.reject(error); + throw new Error("Failed to fetch popular today"); } -} - -export function getMenuItems() { - return [ - { - name: "Main Menu", - items: TestData, - }, - { - name: "Breakfast", - items: [], - }, - { - name: "Seasonal", - items: TestData, - }, - ]; + throw new Error(error); } diff --git a/front/src/lib/test-api.ts b/front/src/lib/test-api.ts index d112473..3162d07 100644 --- a/front/src/lib/test-api.ts +++ b/front/src/lib/test-api.ts @@ -8,34 +8,24 @@ async function fakeDelay(timeout = 1000) { } export async function getAnnouncements(): Promise<{ image: string }> { - if (cache["announcement_banner"] !== undefined) { - return cache["announcement_banner"]; + if (cache["announcement_banner"] === undefined) { + cache["announcement_banner"] = { + image: "/banner_images/BannerExampleImage.jpg", + }; + await fakeDelay(200); } - await fakeDelay(200); - - const data = { - image: "/banner_images/BannerExampleImage.jpg", - }; - cache["announcement_banner"] = data; - - return data; + return cache["announcement_banner"]; } export async function getPopularToday(): Promise { - if (cache["popular_today"] !== undefined) { - return cache["popular_today"]; + if (cache["popular_today"] === undefined) { + cache["popular_today"] = TestData; + await fakeDelay(200); } - await fakeDelay(200); - - const data: Item[] = TestData; - cache["popular_today"] = data; - - return data; + return cache["popular_today"]; } -export async function getMenuItems(): Promise< - { name: string; items: Item[] }[] -> { +export async function getMenuItems(): Promise<{ name: string; items: Item[] }[]> { await fakeDelay(20); return [ { @@ -54,8 +44,6 @@ export async function getMenuItems(): Promise< } export async function getItemsByUUID(items: string[]): Promise { - await fakeDelay(200); - const data: Item[] = []; TestData.forEach((itemInDatabase: Item) => { @@ -66,59 +54,41 @@ export async function getItemsByUUID(items: string[]): Promise { }); }); - if (data.length === 0) { - throw new Error("Resource could not be found"); - } + if (data.length === 0) throw new Error("Resource could not be found"); + await fakeDelay(200); return data; } export async function getItemByUUID(uuid: string): Promise { const data = await getItemsByUUID([uuid]); - if (data.length !== 1) { - throw new Error("Resource could not be found"); - } + if (data.length !== 1) throw new Error("Resource could not be found"); return data[0]; } -export async function postContactEmail( - name: string, - email: string, - message: string -): Promise { +export async function postContactEmail(name: string, email: string, message: string): Promise { await fakeDelay(200); - if (!name) { - throw new Error("Name missing"); - } - if (!email) { - throw new Error("Email missing"); - } - if (!message) { - throw new Error("Message missing"); - } else if (message.length < 150) { - throw new Error("Message FUCKED"); - } + if (!name) throw new Error("Name missing"); + if (!email) throw new Error("Email missing"); + if (!message) throw new Error("Message missing"); + if (message.length < 150) throw new Error("Message FUCKED"); return "Check your email to confirm the message!"; } -export async function postVerifyCart( - currentCartData: Record -): Promise> { - const verifiedItems: Item[] = []; +export async function postVerifyCart(cartData: Record): Promise> { + let verifiedItems: Item[] = []; - await getItemsByUUID(Object.keys(currentCartData)) - .then((data) => { - verifiedItems.push(...data); - }) - .catch(() => { - return new Error("Could not collect new cart information"); - }); + try { + verifiedItems = await getItemsByUUID(Object.keys(cartData)); + } catch (error) { + throw new Error(error); + } const newCartData: Record = {}; - Object.entries(currentCartData).forEach(([uuid, currentData]) => { + Object.entries(cartData).forEach(([uuid, currentData]) => { verifiedItems.forEach((verifiedItem: Item) => { if (verifiedItem.uuid === uuid) { newCartData[uuid] = { diff --git a/front/src/lib/types.ts b/front/src/lib/types.ts index 72d349a..cdf71b5 100644 --- a/front/src/lib/types.ts +++ b/front/src/lib/types.ts @@ -22,6 +22,8 @@ export type CartItem = { }; export type JSONResponse = { - data?: any; // Todo make this _not_ an any + data?: { + item: Item[], // Todo Make this not just item type + }; error?: string; };