mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2025-05-25 04:44:55 +00:00
Use array of UUIDs + Amount for cart data
This is very unstable, but works on the power of will and hope
This commit is contained in:
parent
b7bc7da366
commit
04f7d40e52
4 changed files with 73 additions and 27 deletions
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue