mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2025-01-15 11:05:21 +00:00
Basic Cart functionality
Add test-api function for contact page
This commit is contained in:
parent
e2e68ab1fb
commit
b7bc7da366
|
@ -1,9 +1,11 @@
|
||||||
<script>
|
<script>
|
||||||
|
import { get } from 'svelte/store';
|
||||||
import Router from 'svelte-spa-router';
|
import Router from 'svelte-spa-router';
|
||||||
import { replace, link } from 'svelte-spa-router';
|
import { replace, link } from 'svelte-spa-router';
|
||||||
import active from 'svelte-spa-router/active'
|
import active from 'svelte-spa-router/active'
|
||||||
import { TwitterLogo, FacebookLogo, InstagramLogo, TiktokLogo } from 'phosphor-svelte';
|
import { TwitterLogo, FacebookLogo, InstagramLogo, TiktokLogo } from 'phosphor-svelte';
|
||||||
|
|
||||||
|
import Cart from '%/lib/cart.ts';
|
||||||
import routes from '%/routes.js';
|
import routes from '%/routes.js';
|
||||||
import Logo from '/LogoAlt.svg';
|
import Logo from '/LogoAlt.svg';
|
||||||
|
|
||||||
|
@ -14,6 +16,11 @@
|
||||||
cart: {path: '/cart', className: 'active'},
|
cart: {path: '/cart', className: 'active'},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let cartLen = 0;
|
||||||
|
Cart.subscribe((value) => {
|
||||||
|
cartLen = Cart.getLength();
|
||||||
|
});
|
||||||
|
|
||||||
let scrollY = 0;
|
let scrollY = 0;
|
||||||
let width = 0;
|
let width = 0;
|
||||||
|
|
||||||
|
@ -56,14 +63,18 @@
|
||||||
<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}><a href="/contact" use:link>Contact Us</a></li>
|
<li use:active={links.contact}><a href="/contact" use:link>Contact Us</a></li>
|
||||||
<li use:active={links.cart}><a href="/cart" use:link>Cart</a></li>
|
<li use:active={links.cart}><a href="/cart" use:link>
|
||||||
|
Cart {#if cartLen}({cartLen}){/if}
|
||||||
|
</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
{:else}
|
{:else}
|
||||||
<ul>
|
<ul>
|
||||||
<li use:active={links.home}><a href="/" use:link>Home</a></li>
|
<li use:active={links.home}><a href="/" use:link>Home</a></li>
|
||||||
<li use:active={links.menu}><a href="/menu" use:link>Menu</a></li>
|
<li use:active={links.menu}><a href="/menu" use:link>Menu</a></li>
|
||||||
<li use:active={links.contact}><a href="/contact" use:link>Contact Us</a></li>
|
<li use:active={links.contact}><a href="/contact" use:link>Contact Us</a></li>
|
||||||
<li use:active={links.cart}><a href="/cart" use:link>Cart</a></li>
|
<li use:active={links.cart}><a href="/cart" use:link>
|
||||||
|
Cart {#if cartLen}({cartLen}){/if}
|
||||||
|
</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
{/if}
|
{/if}
|
||||||
</nav>
|
</nav>
|
||||||
|
|
43
front/src/lib/cart.ts
Normal file
43
front/src/lib/cart.ts
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
import { get, writable } from "svelte/store";
|
||||||
|
|
||||||
|
// Load content from localstorage
|
||||||
|
let local = [];
|
||||||
|
try {
|
||||||
|
local = JSON.parse(localStorage.getItem("basket")) || []
|
||||||
|
} catch {
|
||||||
|
console.error("Failed to load cart")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store function
|
||||||
|
function createCartStore() {
|
||||||
|
const cart = writable(local);
|
||||||
|
|
||||||
|
function addToCart(item: any) {
|
||||||
|
cart.update((cart) => [...cart, item]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLength() {
|
||||||
|
return get(cart).length;
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeByUUID(uuid: string) {
|
||||||
|
cart.update((cart) => cart.filter((item) => item.uuid !== uuid))
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...cart,
|
||||||
|
addToCart,
|
||||||
|
getLength,
|
||||||
|
removeByUUID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create store
|
||||||
|
const Cart = createCartStore();
|
||||||
|
|
||||||
|
// Make sure to update localstorage on any changes
|
||||||
|
Cart.subscribe((value) => {
|
||||||
|
localStorage.setItem("basket", JSON.stringify(value));
|
||||||
|
});
|
||||||
|
|
||||||
|
export default Cart;
|
|
@ -1,5 +1,6 @@
|
||||||
import Items from '%/lib/test-data.js';
|
import Items from '%/lib/test-data.js';
|
||||||
|
|
||||||
|
|
||||||
let cache = {};
|
let cache = {};
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,6 +8,7 @@ async function fakeDelay(timeout = 1000) {
|
||||||
await new Promise(resolve => setTimeout(resolve, timeout));
|
await new Promise(resolve => setTimeout(resolve, timeout));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function getAnnouncements() {
|
export async function getAnnouncements() {
|
||||||
if (cache.announcement_banner !== undefined) {
|
if (cache.announcement_banner !== undefined) {
|
||||||
return cache.announcement_banner;
|
return cache.announcement_banner;
|
||||||
|
@ -20,6 +22,7 @@ export async function getAnnouncements() {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function getPopularToday() {
|
export async function getPopularToday() {
|
||||||
if (cache.popular_today !== undefined) {
|
if (cache.popular_today !== undefined) {
|
||||||
return cache.popular_today;
|
return cache.popular_today;
|
||||||
|
@ -31,6 +34,7 @@ export async function getPopularToday() {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function getMenuItems() {
|
export async function getMenuItems() {
|
||||||
const data = [
|
const data = [
|
||||||
{
|
{
|
||||||
|
@ -50,6 +54,7 @@ export async function getMenuItems() {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function getItemByUUID(uuid) {
|
export async function getItemByUUID(uuid) {
|
||||||
let data;
|
let data;
|
||||||
|
|
||||||
|
@ -67,3 +72,22 @@ export async function getItemByUUID(uuid) {
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export async function postContactEmail(name, email, message) {
|
||||||
|
await fakeDelay(1000)
|
||||||
|
|
||||||
|
if (!name) {
|
||||||
|
throw new Error("Namey missing");
|
||||||
|
}
|
||||||
|
if (!email) {
|
||||||
|
throw new Error("Emaily missing");
|
||||||
|
}
|
||||||
|
if (!message) {
|
||||||
|
throw new Error("Message missing");
|
||||||
|
} else if (message.length < 150) {
|
||||||
|
throw new Error("Message FUCKED");
|
||||||
|
}
|
||||||
|
|
||||||
|
return "Check your email to confirm the message!";
|
||||||
|
}
|
||||||
|
|
|
@ -50,13 +50,13 @@ const Items = [
|
||||||
detail: "Example",
|
detail: "Example",
|
||||||
},
|
},
|
||||||
// gwagwa: {
|
// gwagwa: {
|
||||||
{
|
// {
|
||||||
uuid: "gwagwa",
|
// uuid: "gwagwa",
|
||||||
name: "GwaGwa",
|
// name: "GwaGwa",
|
||||||
price: "Priceless",
|
// price: "Priceless",
|
||||||
labels: ["nut"],
|
// labels: ["nut"],
|
||||||
image: "/dab.jpg",
|
// image: "/dab.jpg",
|
||||||
},
|
// },
|
||||||
// hogmelon: {
|
// hogmelon: {
|
||||||
{
|
{
|
||||||
uuid: "hogmelon",
|
uuid: "hogmelon",
|
||||||
|
|
|
@ -1,15 +1,22 @@
|
||||||
<script>
|
<script>
|
||||||
import { link } from 'svelte-spa-router';
|
import { link } from 'svelte-spa-router';
|
||||||
|
|
||||||
|
import Cart from '%/lib/cart.ts';
|
||||||
|
import MenuList from "%/components/MenuList.svelte";
|
||||||
|
|
||||||
|
console.log($Cart);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>Shopping Cart</h1>
|
<h1>Shopping Cart</h1>
|
||||||
|
|
||||||
<div class="container">
|
<MenuList items={$Cart} />
|
||||||
<div class="section">
|
|
||||||
<p>Empty....</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="spacer"></div>
|
<ul>
|
||||||
|
{#each $Cart as item}
|
||||||
|
<li>
|
||||||
|
<button on:click={() => {Cart.removeByUUID(item.uuid)}}>Yeet {item.name}</button>
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
||||||
|
|
||||||
<p>Looking past orders? Check out the <a href="/contact" use:link>commonly asked questions</a></p>
|
<p>Looking past orders? Check out the <a href="/contact" use:link>commonly asked questions</a></p>
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
<script>
|
<script>
|
||||||
import { PaperPlaneRight } from "phosphor-svelte";
|
import { PaperPlaneRight, SealWarning, SealCheck } from "phosphor-svelte";
|
||||||
|
|
||||||
import DropDown from "%/components/DropDown.svelte";
|
import DropDown from "%/components/DropDown.svelte";
|
||||||
|
import { postContactEmail } from "%/lib/test-api.js";
|
||||||
import { expandOnTyping } from "%/lib/utils.js";
|
import { expandOnTyping } from "%/lib/utils.js";
|
||||||
|
|
||||||
const minMessageLength = 150;
|
const minMessageLength = 150;
|
||||||
|
|
||||||
|
let formMessage;
|
||||||
|
|
||||||
let name = "";
|
let name = "";
|
||||||
let email = "";
|
let email = "";
|
||||||
let message = "";
|
let message = "";
|
||||||
|
@ -25,17 +28,14 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function onSubmit(event) {
|
function onSubmit(event) {
|
||||||
console.log(name, email, message);
|
formMessage = postContactEmail(name, email, message);
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>Contact us</h1>
|
<h1>Contact us</h1>
|
||||||
|
|
||||||
|
<h2>Commonly Asked Questions</h2>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<!-- <div class="header">-->
|
|
||||||
<!-- <h2>Commonly Asked Questions</h2>-->
|
|
||||||
<!-- </div>-->
|
|
||||||
<hr>
|
|
||||||
<DropDown name="Can I refund my order?">
|
<DropDown name="Can I refund my order?">
|
||||||
<p>If you ordered online, if we haven't started making your food yet, a refund is possible.</p>
|
<p>If you ordered online, if we haven't started making your food yet, a refund is possible.</p>
|
||||||
<p>If you reserved a table, you can refund upto 1 hour before your time.</p>
|
<p>If you reserved a table, you can refund upto 1 hour before your time.</p>
|
||||||
|
@ -54,6 +54,14 @@
|
||||||
|
|
||||||
<h2>Contact From</h2>
|
<h2>Contact From</h2>
|
||||||
<form on:submit|preventDefault={onSubmit}>
|
<form on:submit|preventDefault={onSubmit}>
|
||||||
|
{#await formMessage then formMessage}
|
||||||
|
{#if formMessage}
|
||||||
|
<p class="form-message success"><SealCheck weight="fill" /> {formMessage}</p>
|
||||||
|
{/if}
|
||||||
|
{:catch error}
|
||||||
|
<p class="form-message error"><SealWarning weight="fill" /> {error.message}</p>
|
||||||
|
{/await}
|
||||||
|
|
||||||
<div class="form-element">
|
<div class="form-element">
|
||||||
<label class="form-label" for="name">Name</label>
|
<label class="form-label" for="name">Name</label>
|
||||||
<input
|
<input
|
||||||
|
@ -86,6 +94,8 @@
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- ToDo: Add dropdown for issue type, such as Technical, Order, Account, or other -->
|
||||||
|
|
||||||
<div class="form-element">
|
<div class="form-element">
|
||||||
<label class="form-label" for="message">Message</label>
|
<label class="form-label" for="message">Message</label>
|
||||||
<textarea
|
<textarea
|
||||||
|
@ -103,7 +113,7 @@
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<button type="submit">Submit <PaperPlaneRight /></button>
|
<button type="submit">Submit <PaperPlaneRight weight="fill" /></button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<style lang="scss">
|
<style lang="scss">
|
||||||
|
|
|
@ -2,9 +2,10 @@
|
||||||
import { replace } from "svelte-spa-router";
|
import { replace } from "svelte-spa-router";
|
||||||
import { SmileySad } from "phosphor-svelte";
|
import { SmileySad } from "phosphor-svelte";
|
||||||
|
|
||||||
|
import Cart from "%/lib/cart.ts";
|
||||||
|
import { getPopularToday, getItemByUUID } from "%/lib/test-api.js";
|
||||||
import MenuList from "%/components/MenuList.svelte";
|
import MenuList from "%/components/MenuList.svelte";
|
||||||
import LoadingBar from "%/components/LoadingBar.svelte";
|
import LoadingBar from "%/components/LoadingBar.svelte";
|
||||||
import { getPopularToday, getItemByUUID } from "%/lib/test-api.js";
|
|
||||||
import LoadingImage from "/MenuItemLoading.svg";
|
import LoadingImage from "/MenuItemLoading.svg";
|
||||||
|
|
||||||
export let params;
|
export let params;
|
||||||
|
@ -64,6 +65,8 @@
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<p>{item.detail}</p>
|
<p>{item.detail}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<button on:click={() => { Cart.addToCart(item) }}>Add to Cart</button>
|
||||||
</div>
|
</div>
|
||||||
{:catch error}
|
{:catch error}
|
||||||
<div id="error">
|
<div id="error">
|
||||||
|
|
|
@ -47,3 +47,23 @@
|
||||||
color: $color-error;
|
color: $color-error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.form-message {
|
||||||
|
margin-bottom: $spacing-small;
|
||||||
|
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
justify-content: flex-start;
|
||||||
|
align-items: center;
|
||||||
|
|
||||||
|
text-shadow: 0 1px 0.5px rgba(#000, 0.2);
|
||||||
|
border-radius: $border-radius-normal;
|
||||||
|
color: $color-on-background;
|
||||||
|
|
||||||
|
&.success {
|
||||||
|
color: $color-primary;
|
||||||
|
}
|
||||||
|
&.error {
|
||||||
|
color: $color-error;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue