Add fake API to allow for testing of requests

This commit is contained in:
Michał 2024-04-27 19:23:06 +01:00
parent 2686fb904c
commit 782c88d082
7 changed files with 86 additions and 46 deletions

View file

@ -48,38 +48,20 @@
<nav class:scrolled={scrolled} class:mobile={mobile}> <nav class:scrolled={scrolled} class:mobile={mobile}>
{#if !mobile} {#if !mobile}
<ul style="justify-content: flex-end"> <ul style="justify-content: flex-end">
<li use:active={links.home}> <li use:active={links.home}><a href="/" use:link>Home</a></li>
<a href="/" use:link>Home</a> <li use:active={links.menu}><a href="/menu" use:link>Menu</a></li>
</li>
<li use:active={links.menu}>
<a href="/menu" use:link>Menu</a>
</li>
</ul> </ul>
<span><img src={Logo} alt="TastyBites"></span> <span><img src={Logo} alt="TastyBites"></span>
<ul style="justify-content: flex-start"> <ul style="justify-content: flex-start">
<li use:active={links.contact}> <li use:active={links.contact}><a href="/contact" use:link>Contact&nbsp;Us</a></li>
<a href="/contact" use:link>Contact&nbsp;Us</a> <li use:active={links.cart}><a href="/cart" use:link>Cart</a></li>
</li>
<li use:active={links.cart}>
<a href="/cart" use:link>Cart</a>
</li>
</ul> </ul>
{:else} {:else}
<ul> <ul>
<li use:active={links.home}> <li use:active={links.home}><a href="/" use:link>Home</a></li>
<a href="/" use:link>Home</a> <li use:active={links.menu}><a href="/menu" use:link>Menu</a></li>
</li> <li use:active={links.contact}><a href="/contact" use:link>Contact&nbsp;Us</a></li>
<li use:active={links.menu}> <li use:active={links.cart}><a href="/cart" use:link>Cart</a></li>
<a href="/menu" use:link>Menu</a>
</li>
<li use:active={links.contact}>
<a href="/contact" use:link>Contact&nbsp;Us</a>
</li>
<li use:active={links.cart}>
<a href="/cart" use:link>Cart</a>
</li>
</ul> </ul>
{/if} {/if}
</nav> </nav>

View file

@ -1,4 +1,4 @@
import Items from '%/lib/testData.js'; import Items from '%/lib/test-data.js';
export async function getPopularToday() { export async function getPopularToday() {
const res = await fetch("/api/items") const res = await fetch("/api/items")

30
front/src/lib/test-api.js Normal file
View file

@ -0,0 +1,30 @@
import Items from '%/lib/test-data.js';
async function fakeDelay(timeout = 1000) {
await new Promise(resolve => setTimeout(resolve, timeout));
}
export async function getPopularToday() {
const data = Items;
await fakeDelay(2000)
return data;
}
export async function getMenuItems() {
const data = [
{
name: "Main Menu",
items: Items,
},
{
name: "Breakfast",
items: [],
},
{
name: "Seasonal",
items: Items,
},
];
await fakeDelay(2000)
return data;
}

View file

@ -1,53 +1,72 @@
// "vegan", "fish", "nut", "spicy", "gluten" // "vegan", "fish", "nut", "spicy", "gluten"
let Items; const Items = [
export default Items = [ // soap: {
{ {
uuid: "soap",
name: "Bar of Soap", name: "Bar of Soap",
price: 69.99, price: 69.99,
labels: ["vegan", "spicy"], labels: ["vegan", "spicy"],
}, },
// sock: {
{ {
uuid: "sock",
name: "Sock", name: "Sock",
price: 21, price: 21,
labels: ["vegan", "fish", "nut", "spicy"], labels: ["vegan", "fish", "nut", "spicy"],
}, },
// brick: {
{ {
uuid: "brick",
name: "Brick", name: "Brick",
price: 0, price: 0,
labels: ["spicy"], labels: ["spicy"],
}, },
// toast: {
{ {
uuid: "toast",
name: "Toast", name: "Toast",
price: 4382749832743, price: 4382749832743,
labels: ["gluten"], labels: ["gluten"],
}, },
// water: {
{ {
uuid: "water",
name: "water", name: "water",
price: 1, price: 1,
labels: ["fish"], labels: ["fish"],
}, },
// mouldy_bread: {
{ {
uuid: "mouldy_bread",
name: "half eaten mouldy bread", name: "half eaten mouldy bread",
price: -99, price: -99,
labels: ["nut"], labels: ["nut"],
}, },
// gwagwa: {
{ {
uuid: "gwagwa",
name: "GwaGwa", name: "GwaGwa",
price: "Priceless", price: "Priceless",
labels: ["nut"], labels: ["nut"],
image: "/dab.jpg", image: "/dab.jpg",
}, },
// hogmelon: {
{ {
uuid: "hogmelon",
name: "Hogermellon", name: "Hogermellon",
price: "1111", price: "1111",
labels: ["fish"], labels: ["fish"],
image: "/wathog.jpg", image: "/wathog.jpg",
}, },
// bluhog: {
{ {
uuid: "bluhog",
name: "Blue HOGGGGG", name: "Blue HOGGGGG",
price: "ARUGH", price: "ARUGH",
labels: ["nut", "gluten", "spicy"], labels: ["nut", "gluten", "spicy"],
image: "/sonichog.jpg", image: "/sonichog.jpg",
}, },
]; ];
export default Items;

View file

@ -5,7 +5,7 @@
import { ArrowUpRight } from "phosphor-svelte"; import { ArrowUpRight } from "phosphor-svelte";
import MenuList from "%/components/MenuList.svelte"; import MenuList from "%/components/MenuList.svelte";
import { getPopularToday } from "%/lib/api.js"; import { getPopularToday } from "%/lib/test-api.js";
import BannerImage from '/BannerExampleImage.jpg'; import BannerImage from '/BannerExampleImage.jpg';
let items = getPopularToday(); let items = getPopularToday();
@ -35,8 +35,8 @@
<div id="contact"> <div id="contact">
<div id="map"></div> <div id="map"></div>
<div class="container"> <div class="container">
<h2>Some Title</h2> <h2>Opening Times</h2>
<p>Example text</p> <p>Please make sure to checkout our full calendar, as we have different times during public holiday</p>
<div id="timetable"> <div id="timetable">
<table> <table>
<tr><th>Day</th><th>Opening</th><th>Closing</th></tr> <tr><th>Day</th><th>Opening</th><th>Closing</th></tr>

View file

@ -1,7 +1,7 @@
<script> <script>
import MenuList from "%/components/MenuList.svelte"; import MenuList from "%/components/MenuList.svelte";
import LoadingImage from "/MenuItemLoading.svg"; import LoadingImage from "/MenuItemLoading.svg";
import Items from '%/lib/testData.js'; import Items from '%/lib/test-data.js';
let items = Items; let items = Items;

View file

@ -1,9 +1,10 @@
<script> <script>
import { ArrowClockwise } from "phosphor-svelte"; import { ArrowClockwise } from "phosphor-svelte";
import LoadingBar from "%/components/LoadingBar.svelte";
import MenuList from "%/components/MenuList.svelte"; import MenuList from "%/components/MenuList.svelte";
import DropDown from "%/components/DropDown.svelte"; import DropDown from "%/components/DropDown.svelte";
import { getMenuItems } from "%/lib/api.js"; import { getMenuItems } from "%/lib/test-api.js";
let items = getMenuItems(); let items = getMenuItems();
</script> </script>
@ -139,15 +140,21 @@
</div> </div>
<div id="menu-list"> <div id="menu-list">
{#each items as section} {#await items}
<h2>{section.name}</h2> <LoadingBar />
{#if section.items.length > 0} {:then items}
<MenuList items={section.items} /> {#each items as section}
{:else} <h2>{section.name}</h2>
<p>No results</p> {#if section.items.length > 0}
{/if} <MenuList items={section.items} />
<div class="spacer" /> {:else}
{/each} <p>No results</p>
{/if}
<div class="spacer" />
{/each}
{:catch error}
<p>{error}</p>
{/await}
</div> </div>
</div> </div>
@ -162,7 +169,9 @@
} }
#menu-list { #menu-list {
padding-left: $spacing-normal; margin-left: $spacing-normal;
width: 100%;
position: relative;
} }
#filter { #filter {
@ -206,8 +215,8 @@
} }
#menu-list { #menu-list {
padding-left: 0; margin-left: 0;
padding-top: $spacing-normal; margin-top: $spacing-normal;
} }
} }
</style> </style>