From d194dcc23c19a4cf31863b32770f8df77e1f675a Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 30 Jan 2023 09:36:40 +0700 Subject: Add or remove wishlist --- src/components/layouts/Header.js | 6 ++++- src/pages/shop/product/[slug].js | 55 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/components/layouts/Header.js b/src/components/layouts/Header.js index 5cd0a1bc..4741905f 100644 --- a/src/components/layouts/Header.js +++ b/src/components/layouts/Header.js @@ -8,7 +8,8 @@ import { Bars3Icon, ShoppingCartIcon, ChevronRightIcon, - Cog6ToothIcon + Cog6ToothIcon, + HeartIcon } from "@heroicons/react/24/outline"; // Helpers @@ -117,6 +118,9 @@ export default function Header({ title }) { Logo Indoteknik
+ + + diff --git a/src/pages/shop/product/[slug].js b/src/pages/shop/product/[slug].js index fe467439..dd097bbe 100644 --- a/src/pages/shop/product/[slug].js +++ b/src/pages/shop/product/[slug].js @@ -12,6 +12,9 @@ import toast from "react-hot-toast"; import Footer from "@/components/layouts/Footer"; import Image from "@/components/elements/Image"; import LineDivider from "@/components/elements/LineDivider"; +import { HeartIcon as HeartIconSolid } from "@heroicons/react/24/solid"; +import { useAuth } from "@/core/utils/auth"; +import { HeartIcon } from "@heroicons/react/24/outline"; export async function getServerSideProps( context ) { const { slug } = context.query; @@ -29,6 +32,7 @@ export async function getServerSideProps( context ) { } export default function ProductDetail({ product }) { + const [ auth ] = useAuth(); const router = useRouter(); const { slug } = router.query; const [selectedVariant, setSelectedVariant] = useState(""); @@ -42,12 +46,41 @@ export default function ProductDetail({ product }) { weight: product.weight, attributes: '', }); + + const [ isAddedToWishlist, setAddedToWishlist ] = useState(false); + + const addOrDeleteWishlist = async () => { + if (auth) { + await apiOdoo('POST', `/api/v1/user/${auth.id}/wishlist/create-or-delete`, { + product_id: product.id + }); + if (isAddedToWishlist) { + toast.success('Berhasil menghapus dari wishlist'); + } else { + toast.success('Berhasil menambahkan ke wishlist'); + } + setAddedToWishlist(!isAddedToWishlist); + } else { + toast.error('Login terlebih dahulu untuk melanjutkan'); + router.push('/login'); + } + }; + + useEffect(() => { + if (auth) { + const checkWishlist = async () => { + const wishlist = await apiOdoo('GET', `/api/v1/user/${auth.id}/wishlist?product_id=${product.id}`); + setAddedToWishlist(wishlist.product_total > 0 ? true : false); + } + checkWishlist(); + } + }, [ auth, product ]); useEffect(() => { if (product.variants.length == 1) { setSelectedVariant(product.variants[0].id); } - }, [product]); + }, [ product ]); useEffect(() => { setSimilarProducts(null); @@ -116,10 +149,22 @@ export default function ProductDetail({ product }) { />
- - {product.manufacture.name ?? '-'} - -

{product.name}{activeVariant.attributes ? ' - ' + activeVariant.attributes : ''}

+
+
+ + {product.manufacture.name ?? '-'} + +

{product.name}{activeVariant.attributes ? ' - ' + activeVariant.attributes : ''}

+
+ +
{product.variant_total > 1 && !selectedVariant && product.lowest_price.price > 0 ? (

Harga mulai dari:

-- cgit v1.2.3