diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-17 17:07:50 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-17 17:07:50 +0700 |
| commit | f99e0aba70efad0deb907d8e27f09fc9f527c8a4 (patch) | |
| tree | f0ac96e4e736a1d385e32553f0e641ee27e11fd3 /src2/components/variants | |
| parent | 90e1edab9b6a8ccc09a49fed3addbec2cbc4e4c3 (diff) | |
Refactor
Diffstat (limited to 'src2/components/variants')
| -rw-r--r-- | src2/components/variants/VariantCard.js | 92 | ||||
| -rw-r--r-- | src2/components/variants/VariantGroupCard.js | 31 |
2 files changed, 123 insertions, 0 deletions
diff --git a/src2/components/variants/VariantCard.js b/src2/components/variants/VariantCard.js new file mode 100644 index 00000000..a821480c --- /dev/null +++ b/src2/components/variants/VariantCard.js @@ -0,0 +1,92 @@ +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, + 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"> + <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.discount_percentage > 0 && ( + <> + <p className="text-caption-2 text-gray_r-11 line-through">{currencyFormat(product.price.price)}</p> + <span className="badge-red">{product.price.discount_percentage}%</span> + </> + )} + <p className="text-caption-2 text-gray_r-12">{currencyFormat(product.price.price_discount)}</p> + </div> + <p className="text-caption-2 text-gray_r-11 mt-1"> + {currencyFormat(product.price.price_discount)} × {product.quantity} Barang + </p> + <p className="text-caption-2 text-gray_r-12 font-bold mt-2"> + {currencyFormat(product.quantity * product.price.price_discount)} + </p> + </div> + </div> + ); + + if (openOnClick) { + return ( + <> + <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> + ) } + </> + ); + } + + return <Card/>; +}
\ No newline at end of file diff --git a/src2/components/variants/VariantGroupCard.js b/src2/components/variants/VariantGroupCard.js new file mode 100644 index 00000000..462c63cf --- /dev/null +++ b/src2/components/variants/VariantGroupCard.js @@ -0,0 +1,31 @@ +import { useState } from "react" +import VariantCard from "./VariantCard" + +export default function VariantGroupCard({ + variants, + ...props +}) { + const [ showAll, setShowAll ] = useState(false) + const variantsToShow = showAll ? variants : variants.slice(0, 2) + + return ( + <> + { variantsToShow?.map((variant, index) => ( + <VariantCard + key={index} + data={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> + ) } + </> + ) +}
\ No newline at end of file |
