diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-03 17:03:45 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-03 17:03:45 +0700 |
| commit | cd01ba82733062db99075ad7690bdb52fb85745a (patch) | |
| tree | df86ed690452945463abc77263ac058d5b7f9823 /src/components | |
| parent | 87af032177192ed1d5d7c68cab911ed102e647bc (diff) | |
no message
Diffstat (limited to 'src/components')
| -rw-r--r-- | src/components/products/ProductCategories.js | 56 | ||||
| -rw-r--r-- | src/components/products/ProductSimilar.js | 25 | ||||
| -rw-r--r-- | src/components/variants/VariantCard.js | 43 |
3 files changed, 120 insertions, 4 deletions
diff --git a/src/components/products/ProductCategories.js b/src/components/products/ProductCategories.js new file mode 100644 index 00000000..dc1325b7 --- /dev/null +++ b/src/components/products/ProductCategories.js @@ -0,0 +1,56 @@ +import { useEffect, useState } from "react"; +import ProductSlider from "./ProductSlider"; +import apiOdoo from "@/core/utils/apiOdoo"; +import { LazyLoadComponent } from "react-lazy-load-image-component"; +import { SkeletonProduct } from "../elements/Skeleton"; + +const ProductCategory = ({ id }) => { + const [ content, setContent ] = useState(null); + + useEffect(() => { + const loadContent = async () => { + if (!content) { + const dataContent = await apiOdoo('GET', `/api/v1/categories_homepage?id=${id}`); + setContent(dataContent[0]); + } + } + loadContent(); + }, [id, content]); + + return ( + <div className="my-6 px-4 pt-4 relative"> + { content ? ( + <ProductSlider + products={{ + products: content.products, + banner: { + image: content.image, + name: content.name, + url: `/shop/search?category=${content.name}` + } + }} + simpleProductTitleLine + bannerMode + /> + ) : <SkeletonProduct /> } + </div> + ); +} + +export default function ProductCategories() { + const [ contentIds, setContentIds ] = useState([]); + + useEffect(() => { + const getContentIds = async () => { + const dataContentIds = await apiOdoo('GET', '/api/v1/categories_homepage/ids'); + setContentIds(dataContentIds); + } + getContentIds(); + }, []); + + return contentIds.map((contentId) => ( + <LazyLoadComponent key={contentId}> + <ProductCategory id={contentId} /> + </LazyLoadComponent> + )); +}
\ No newline at end of file diff --git a/src/components/products/ProductSimilar.js b/src/components/products/ProductSimilar.js new file mode 100644 index 00000000..9e2292cb --- /dev/null +++ b/src/components/products/ProductSimilar.js @@ -0,0 +1,25 @@ +import apiOdoo from '@/core/utils/apiOdoo'; +import { useEffect, useState } from 'react'; +import ProductSlider from './ProductSlider'; + +export default function ProductSimilar({ productId }) { + const [similarProducts, setSimilarProducts] = useState(null); + + useEffect(() => { + const getSimilarProducts = async () => { + if (productId && !similarProducts) { + const dataSimilarProducts = await apiOdoo('GET', `/api/v1/product/${productId}/similar?limit=20`); + setSimilarProducts(dataSimilarProducts); + } + } + getSimilarProducts(); + }, [productId, similarProducts]); + + + return ( + <div className="p-4"> + <h2 className="font-bold mb-4">Kamu Mungkin Juga Suka</h2> + <ProductSlider products={similarProducts}/> + </div> + ) +}
\ No newline at end of file diff --git a/src/components/variants/VariantCard.js b/src/components/variants/VariantCard.js index cb4d8272..2d27371b 100644 --- a/src/components/variants/VariantCard.js +++ b/src/components/variants/VariantCard.js @@ -2,12 +2,27 @@ import { createSlug } from "@/core/utils/slug"; import Image from "../elements/Image"; import Link from "../elements/Link"; import currencyFormat from "@/core/utils/currencyFormat"; +import { useRouter } from "next/router"; +import { toast } from "react-hot-toast"; +import { createOrUpdateItemCart } from "@/core/utils/cart"; export default function VariantCard({ data, - openOnClick = true + openOnClick = true, + buyMore = false }) { let product = data; + const router = useRouter(); + + const addItemToCart = () => { + toast.success('Berhasil menambahkan ke keranjang', { duration: 1500 }); + createOrUpdateItemCart(product.id, 1); + return; + }; + + const checkoutItem = () => { + router.push(`/shop/checkout?product_id=${product.id}&qty=${product.quantity}`); + } const Card = () => ( <div className="flex gap-x-3"> @@ -38,9 +53,29 @@ export default function VariantCard({ if (openOnClick) { return ( - <Link href={'/shop/product/' + createSlug(product.parent.name, product.parent.id)}> - <Card /> - </Link> + <> + <Link href={'/shop/product/' + createSlug(product.parent.name, product.parent.id)}> + <Card /> + </Link> + { buyMore && ( + <div className="flex justify-end gap-x-2 mb-2"> + <button + type="button" + onClick={addItemToCart} + className="btn-yellow text-gray_r-12 py-2 px-3" + > + Tambah Keranjang + </button> + <button + type="button" + onClick={checkoutItem} + className="btn-solid-red py-2 px-3" + > + Beli Lagi + </button> + </div> + ) } + </> ); } |
