mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2025-06-19 08:30:36 +00:00
Basic Cart functionality
Add test-api function for contact page
This commit is contained in:
parent
e2e68ab1fb
commit
b7bc7da366
8 changed files with 142 additions and 24 deletions
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';
|
||||
|
||||
|
||||
let cache = {};
|
||||
|
||||
|
||||
|
@ -7,6 +8,7 @@ async function fakeDelay(timeout = 1000) {
|
|||
await new Promise(resolve => setTimeout(resolve, timeout));
|
||||
}
|
||||
|
||||
|
||||
export async function getAnnouncements() {
|
||||
if (cache.announcement_banner !== undefined) {
|
||||
return cache.announcement_banner;
|
||||
|
@ -20,6 +22,7 @@ export async function getAnnouncements() {
|
|||
return data;
|
||||
}
|
||||
|
||||
|
||||
export async function getPopularToday() {
|
||||
if (cache.popular_today !== undefined) {
|
||||
return cache.popular_today;
|
||||
|
@ -31,6 +34,7 @@ export async function getPopularToday() {
|
|||
return data;
|
||||
}
|
||||
|
||||
|
||||
export async function getMenuItems() {
|
||||
const data = [
|
||||
{
|
||||
|
@ -50,6 +54,7 @@ export async function getMenuItems() {
|
|||
return data;
|
||||
}
|
||||
|
||||
|
||||
export async function getItemByUUID(uuid) {
|
||||
let data;
|
||||
|
||||
|
@ -66,4 +71,23 @@ export async function getItemByUUID(uuid) {
|
|||
}
|
||||
|
||||
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",
|
||||
},
|
||||
// gwagwa: {
|
||||
{
|
||||
uuid: "gwagwa",
|
||||
name: "GwaGwa",
|
||||
price: "Priceless",
|
||||
labels: ["nut"],
|
||||
image: "/dab.jpg",
|
||||
},
|
||||
// {
|
||||
// uuid: "gwagwa",
|
||||
// name: "GwaGwa",
|
||||
// price: "Priceless",
|
||||
// labels: ["nut"],
|
||||
// image: "/dab.jpg",
|
||||
// },
|
||||
// hogmelon: {
|
||||
{
|
||||
uuid: "hogmelon",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue