-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from datagrove/createCart
Create cart
- Loading branch information
Showing
27 changed files
with
1,415 additions
and
89 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { onMount } from "solid-js"; | ||
import { createStore } from "solid-js/store"; | ||
import type { Component } from "solid-js"; | ||
import { getLangFromUrl, useTranslations } from "@i18n/utils"; | ||
import cart from "@assets/shopping-cart.svg"; | ||
|
||
const lang = getLangFromUrl(new URL(window.location.href)); | ||
const t = useTranslations(lang); | ||
|
||
interface Item { | ||
description: string; | ||
price: number; | ||
price_id: string; | ||
quantity: number; | ||
product_id: string; | ||
} | ||
|
||
interface Props { | ||
description: string; | ||
price: number; | ||
price_id: string; | ||
quantity: number; | ||
product_id: string; | ||
buttonClick: (event: Event) => void; | ||
} | ||
|
||
export const [items, setItems] = createStore<Item[]>([]); | ||
|
||
export const AddToCart: Component<Props> = (props: Props) => { | ||
const storedItems = localStorage.getItem("cartItems"); | ||
|
||
onMount(() => { | ||
if (storedItems) { | ||
setItems(JSON.parse(storedItems)); | ||
} | ||
}); | ||
|
||
function clickHandler(e: Event) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
let itemInCart = false; | ||
|
||
const updatedItems = items.map((item: Item) => { | ||
if (item.product_id === props.product_id) { | ||
itemInCart = true; | ||
return { ...item, quantity: item.quantity + props.quantity }; | ||
} | ||
return item; | ||
}); | ||
|
||
if (!itemInCart) { | ||
const newItem = { | ||
description: props.description, | ||
price: props.price, | ||
price_id: props.price_id, | ||
quantity: props.quantity, | ||
product_id: props.product_id, | ||
}; | ||
setItems([...updatedItems, newItem]); | ||
} else { | ||
setItems(updatedItems); | ||
} | ||
|
||
props.buttonClick(e); | ||
console.log(items); | ||
} | ||
|
||
return ( | ||
<div class="relative z-10"> | ||
<button | ||
onclick={(e) => clickHandler(e)} | ||
class="btn-primary" | ||
aria-label={t("buttons.addToCart")} | ||
> | ||
{t("buttons.addToCart")} | ||
</button> | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.