From 39a2e8012ba38d6663820ae27080a2b843c08c5f Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Wed, 14 Dec 2022 12:01:13 +0700 Subject: Add to cart to localstorage --- src/helpers/cart.js | 54 ++++++++++++++++++++++++++++++++++++++++ src/icons/chevron-left.svg | 3 +++ src/pages/404.js | 4 +-- src/pages/shop/cart.js | 51 +++++++++++++++++++++++++++++++++++++ src/pages/shop/product/[slug].js | 8 ++++-- 5 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 src/helpers/cart.js create mode 100644 src/icons/chevron-left.svg create mode 100644 src/pages/shop/cart.js (limited to 'src') diff --git a/src/helpers/cart.js b/src/helpers/cart.js new file mode 100644 index 00000000..07e47324 --- /dev/null +++ b/src/helpers/cart.js @@ -0,0 +1,54 @@ +const getCart = () => { + const cart = localStorage.getItem('cart'); + if (cart) return JSON.parse(cart); + return []; +} + +const setCart = (cart) => { + localStorage.setItem('cart', JSON.stringify(cart)); + return true; +} + +const getItemIndex = (product_id) => { + const cart = getCart(); + return cart.findIndex((item) => item.product_id == product_id); +} + +const addToCart = (product_id, quantity) => { + let cart = getCart(); + let itemIndexByProductId = getItemIndex(product_id); + if (itemIndexByProductId > -1) { + updateItemCart(product_id, quantity); + } else { + cart.push({ product_id, quantity }); + } + setCart(cart); + return true; +} + +const deleteItemCart = (product_id) => { + let cart = getCart(); + let itemIndexByProductId = getItemIndex(product_id); + if (itemIndexByProductId > -1) { + cart.splice(itemIndexByProductId, 1) + } + setCart(cart); + return true; +} + +const updateItemCart = (product_id, quantity) => { + let cart = getCart(); + let itemIndexByProductId = getItemIndex(product_id); + if (itemIndexByProductId > -1) { + cart[itemIndexByProductId].quantity += quantity; + } + setCart(cart); + return true; +} + +export { + getCart, + addToCart, + deleteItemCart, + updateItemCart +} \ No newline at end of file diff --git a/src/icons/chevron-left.svg b/src/icons/chevron-left.svg new file mode 100644 index 00000000..a22ce386 --- /dev/null +++ b/src/icons/chevron-left.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/src/pages/404.js b/src/pages/404.js index 3eafbb08..b7f8b8ef 100644 --- a/src/pages/404.js +++ b/src/pages/404.js @@ -13,10 +13,10 @@ export default function PageNotFound() { Halaman Tidak Ditemukan - Indoteknik

Halaman tidak ditemukan

- + Kembali ke beranda - + Tanya admin
diff --git a/src/pages/shop/cart.js b/src/pages/shop/cart.js new file mode 100644 index 00000000..2de1a7ac --- /dev/null +++ b/src/pages/shop/cart.js @@ -0,0 +1,51 @@ +import { useEffect } from "react"; +import Header from "../../components/Header"; +import Layout from "../../components/Layout"; +import Link from "../../components/Link"; +import { getCart } from "../../helpers/cart"; +import ChevronLeftIcon from "../../icons/chevron-left.svg"; + +export default function Cart() { + useEffect(() => { + console.log(getCart()); + }) + return ( + <> +
+ +
+ {/* Progress Bar */} +
+
+
1
+

Keranjang

+
+
+
+
+
+
2
+

Pembayaran

+
+
+
+
+
+
3
+

Selesai

+
+
+ {/* [End] Progress Bar */} + {/* Title */} +
+ + + +

Keranjang Saya

+
+ {/* [End] Title */} +
+
+ + ); +} \ No newline at end of file diff --git a/src/pages/shop/product/[slug].js b/src/pages/shop/product/[slug].js index 5fce1c85..e44cfeca 100644 --- a/src/pages/shop/product/[slug].js +++ b/src/pages/shop/product/[slug].js @@ -9,6 +9,7 @@ import { LazyLoadImage } from "react-lazy-load-image-component"; import "react-lazy-load-image-component/src/effects/blur.css"; import ProductSlider from "../../../components/product/ProductSlider"; import Layout from "../../../components/Layout"; +import { addToCart } from "../../../helpers/cart"; export async function getServerSideProps( context ) { const { slug } = context.query; @@ -80,7 +81,10 @@ export default function ProductDetail({ product }) { setQuantity("1"); } - let addToCart = () => { + let addItemToCart = () => { + if (quantity > 0) { + addToCart(activeVariant.id, parseInt(quantity)); + } return true; } @@ -136,7 +140,7 @@ export default function ProductDetail({ product }) {
- +
-- cgit v1.2.3