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/pages/shop/product/[slug].js | 55 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 5 deletions(-) (limited to 'src/pages') 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