diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-21 12:04:20 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-21 12:04:20 +0700 |
| commit | fdfb47c3a825258b871ac5921605642e5e05fdd8 (patch) | |
| tree | 9d8ee50a5a28bd67fad7a5699aa88c415b6fc60c /src/lib/variant | |
| parent | fa45f8bc91adef3bacd3460ef4e3b1151ee13e71 (diff) | |
fix
Diffstat (limited to 'src/lib/variant')
| -rw-r--r-- | src/lib/variant/components/VariantCard.jsx | 97 | ||||
| -rw-r--r-- | src/lib/variant/components/VariantGroupCard.jsx | 33 |
2 files changed, 130 insertions, 0 deletions
diff --git a/src/lib/variant/components/VariantCard.jsx b/src/lib/variant/components/VariantCard.jsx new file mode 100644 index 00000000..6c7ab22f --- /dev/null +++ b/src/lib/variant/components/VariantCard.jsx @@ -0,0 +1,97 @@ +import { useRouter } from "next/router" +import { toast } from "react-hot-toast" + +import Image from "@/core/components/elements/Image/Image" +import Link from "@/core/components/elements/Link/Link" +import { createSlug } from "@/core/utils/slug" +import currencyFormat from "@/core/utils/currencyFormat" +import { updateItemCart } from "@/core/utils/cart" + +const VariantCard = ({ + product, + openOnClick = true, + buyMore = false +}) => { + const router = useRouter() + + const addItemToCart = () => { + toast.success('Berhasil menambahkan ke keranjang', { duration: 1500 }) + updateItemCart({ + productId: product.id, + quantity: 1 + }) + return + } + + const checkoutItem = () => { + router.push(`/shop/checkout?product_id=${product.id}&qty=${product.quantity}`) + } + + const Card = () => ( + <div className="flex gap-x-3"> + <div className="w-4/12 flex items-center gap-x-2"> + <Image + src={product.parent.image} + alt={product.parent.name} + className="object-contain object-center border border-gray_r-6 h-32 w-full rounded-md" + /> + </div> + <div className="w-8/12 flex flex-col"> + <p className="product-card__title wrap-line-ellipsis-2"> + {product.parent.name} + </p> + <p className="text-caption-2 text-gray_r-11 mt-1"> + {product.code || '-'} + {product.attributes.length > 0 ? ` ・ ${product.attributes.join(', ')}` : ''} + </p> + <div className="flex flex-wrap gap-x-1 items-center mt-auto"> + {product.price.discountPercentage > 0 && ( + <> + <p className="text-caption-2 text-gray_r-11 line-through">{currencyFormat(product.price.price)}</p> + <span className="badge-red">{product.price.discountPercentage}%</span> + </> + )} + <p className="text-caption-2 text-gray_r-12">{currencyFormat(product.price.priceDiscount)}</p> + </div> + <p className="text-caption-2 text-gray_r-11 mt-1"> + {currencyFormat(product.price.priceDiscount)} × {product.quantity} Barang + </p> + <p className="text-caption-2 text-gray_r-12 font-bold mt-2"> + {currencyFormat(product.quantity * product.price.priceDiscount)} + </p> + </div> + </div> + ) + + if (openOnClick) { + return ( + <> + <Link href={createSlug('/shop/product/', 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 text-caption-1" + > + Tambah Keranjang + </button> + <button + type="button" + onClick={checkoutItem} + className="btn-solid-red py-2 px-3 text-caption-1" + > + Beli Lagi + </button> + </div> + ) } + </> + ) + } + + return <Card/> +} + +export default VariantCard
\ No newline at end of file diff --git a/src/lib/variant/components/VariantGroupCard.jsx b/src/lib/variant/components/VariantGroupCard.jsx new file mode 100644 index 00000000..fd4f9b4d --- /dev/null +++ b/src/lib/variant/components/VariantGroupCard.jsx @@ -0,0 +1,33 @@ +import { useState } from "react" +import VariantCard from "./VariantCard" + +const VariantGroupCard = ({ + variants, + ...props +}) => { + const [ showAll, setShowAll ] = useState(false) + const variantsToShow = showAll ? variants : variants.slice(0, 2) + + return ( + <> + { variantsToShow?.map((variant, index) => ( + <VariantCard + key={index} + product={variant} + {...props} + /> + )) } + { variants.length > 2 && ( + <button + type="button" + className="btn-light py-2 w-full" + onClick={() => setShowAll(!showAll)} + > + { !showAll ? `Lihat Semua +${variants.length - variantsToShow.length}` : 'Tutup' } + </button> + ) } + </> + ) +} + +export default VariantGroupCard
\ No newline at end of file |
