From bbc333053b2cb963f8a16cecb4d7f15a0111daf2 Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Thu, 22 Jun 2023 11:02:53 +0700 Subject: page variant --- src/lib/product/api/variantApi.js | 9 + src/lib/product/components/Product/Product.jsx | 39 ++- .../components/Product/ProductDesktopVariant.jsx | 348 +++++++++++++++++++++ .../components/Product/ProductMobileVariant.jsx | 315 +++++++++++++++++++ src/pages/shop/product/variant/[slug].jsx | 75 +++++ 5 files changed, 777 insertions(+), 9 deletions(-) create mode 100644 src/lib/product/api/variantApi.js create mode 100644 src/lib/product/components/Product/ProductDesktopVariant.jsx create mode 100644 src/lib/product/components/Product/ProductMobileVariant.jsx create mode 100644 src/pages/shop/product/variant/[slug].jsx diff --git a/src/lib/product/api/variantApi.js b/src/lib/product/api/variantApi.js new file mode 100644 index 00000000..47273dd7 --- /dev/null +++ b/src/lib/product/api/variantApi.js @@ -0,0 +1,9 @@ +import odooApi from '@/core/api/odooApi' + +const variantApi = async ({ id, headers = {} }) => { + if (!id) return + const dataProduct = await odooApi('GET', `/api/v2/product_variant/${id}`, {}, headers) + return dataProduct +} + +export default variantApi diff --git a/src/lib/product/components/Product/Product.jsx b/src/lib/product/components/Product/Product.jsx index 351c07c1..0547c36e 100644 --- a/src/lib/product/components/Product/Product.jsx +++ b/src/lib/product/components/Product/Product.jsx @@ -7,8 +7,10 @@ import ProductMobile from './ProductMobile' import { useRouter } from 'next/router' import { useEffect } from 'react' import { gtagViewItem } from '@/core/utils/googleTag' +import ProductDesktopVariant from './ProductDesktopVariant' +import ProductMobileVariant from './ProductMobileVariant' -const Product = ({ product }) => { +const Product = ({ product, isVariant = false }) => { const auth = useAuth() const router = useRouter() const { wishlist } = useWishlist({ productId: product?.id }) @@ -29,15 +31,34 @@ const Product = ({ product }) => { } useEffect(() => { - gtagViewItem(product.variants) - }, [product]) + if (isVariant == false) { + gtagViewItem(product.variants) + } + }, [product, isVariant]) - return ( - <> - - - - ) + if (isVariant == true) { + return ( + <> + + + + ) + } else { + return ( + <> + + + + ) + } } export default Product diff --git a/src/lib/product/components/Product/ProductDesktopVariant.jsx b/src/lib/product/components/Product/ProductDesktopVariant.jsx new file mode 100644 index 00000000..a98985c9 --- /dev/null +++ b/src/lib/product/components/Product/ProductDesktopVariant.jsx @@ -0,0 +1,348 @@ +import Image from '@/core/components/elements/Image/Image' +import Link from '@/core/components/elements/Link/Link' +import DesktopView from '@/core/components/views/DesktopView' +import currencyFormat from '@/core/utils/currencyFormat' +import { HeartIcon } from '@heroicons/react/24/outline' +import { useCallback, useEffect, useRef, useState } from 'react' +import LazyLoad from 'react-lazy-load' +import ProductSimilar from '../ProductSimilar' +import { toast } from 'react-hot-toast' +import { updateItemCart } from '@/core/utils/cart' +import { useRouter } from 'next/router' +import { createSlug } from '@/core/utils/slug' +import BottomPopup from '@/core/components/elements/Popup/BottomPopup' +import ProductCard from '../ProductCard' +import productSimilarApi from '../../api/productSimilarApi' +import whatsappUrl from '@/core/utils/whatsappUrl' + +const ProductDesktopVariant = ({ product, wishlist, toggleWishlist, isVariant }) => { + const router = useRouter() + + const [lowestPrice, setLowestPrice] = useState(null) + + const [addCartAlert, setAddCartAlert] = useState(false) + + const getLowestPrice = useCallback(() => { + const lowest = product.price + /* const lowest = prices.reduce((lowest, price) => { + return price.priceDiscount < lowest.priceDiscount ? price : lowest + }, prices[0])*/ + return lowest + }, [product]) + + useEffect(() => { + const lowest = getLowestPrice() + setLowestPrice(lowest) + }, [getLowestPrice]) + + const [informationTab, setInformationTab] = useState(informationTabOptions[0].value) + + const variantQuantityRefs = useRef([]) + + const setVariantQuantityRef = (variantId) => (element) => { + variantQuantityRefs.current[variantId] = element + } + + const validQuantity = (quantity) => { + let isValid = true + if (!quantity || quantity < 1 || isNaN(parseInt(quantity))) { + toast.error('Jumlah barang minimal 1') + isValid = false + } + return isValid + } + + const handleAddToCart = (variant) => { + const quantity = variantQuantityRefs.current[variant].value + if (!validQuantity(quantity)) return + updateItemCart({ + productId: variant, + quantity, + selected: true + }) + setAddCartAlert(true) + } + + const handleBuy = (variant) => { + const quantity = variantQuantityRefs.current[variant].value + if (!validQuantity(quantity)) return + router.push(`/shop/checkout?productId=${variant}&quantity=${quantity}`) + } + + const variantSectionRef = useRef(null) + const goToVariantSection = () => { + if (variantSectionRef.current) { + const position = variantSectionRef.current.getBoundingClientRect() + window.scrollTo({ + top: position.top - 120 + window.pageYOffset, + behavior: 'smooth' + }) + } + } + + const productSimilarQuery = [ + product?.name, + `fq=-product_id_i:${product.id}`, + `fq=-manufacture_id_i:${product.manufacture?.id || 0}` + ].join('&') + + const [productSimilarInBrand, setProductSimilarInBrand] = useState(null) + + useEffect(() => { + const loadProductSimilarInBrand = async () => { + const productSimilarQuery = [product?.name, `fq=-product_id_i:${product.id}`].join('&') + const dataProductSimilar = await productSimilarApi({ query: productSimilarQuery }) + setProductSimilarInBrand(dataProductSimilar.products) + } + if (!productSimilarInBrand) loadProductSimilarInBrand() + }, [product, productSimilarInBrand]) + + return ( + +
+
+
+
+ {product.name} +
+ +
+

{product?.name}

+
+
+
Nomor SKU
+
SKU-{product.id}
+
+
+
Part Number
+
{product.code || '-'}
+
+
+
Manufacture
+
+ {product.manufacture?.name ? ( + + {product.manufacture?.name} + + ) : ( +
-
+ )} +
+
+
+
Berat Barang
+
+ {product?.weight > 0 && {product?.weight} KG} + {product?.weight == 0 && ( + + Tanya Berat + + )} +
+
+
+
+ + {/*
+
+
Informasi Produk
+
+ {informationTabOptions.map((option) => ( + setInformationTab(option.value)} + > + {option.label} + + ))} +
+
+
+ + + + + + Belum ada informasi. + +
+
+
+
*/} +
+ +
+ {lowestPrice?.discountPercentage > 0 && ( +
+
+ {lowestPrice?.discountPercentage}% +
+
+ {currencyFormat(lowestPrice?.price * 1.11)} +
+
+ )} +

+ {currencyFormat(lowestPrice?.priceDiscount)} +

+

+ {lowestPrice?.priceDiscount > 0 ? ( + currencyFormat(lowestPrice?.priceDiscount * 1.11) + ) : ( + + Hubungi kami untuk dapatkan harga terbaik,  + + klik disini + + + )} + {lowestPrice?.priceDiscount > 0 && ( + *include PPN + )} +

+ +
+ + + +
+ +
+ +
+ +
+
+ Produk Serupa +
+
+ {productSimilarInBrand && + productSimilarInBrand?.map((product) => ( +
+ +
+ ))} +
+
+
+
+ +
+
Kamu Mungkin Juga Suka
+ + + +
+ + setAddCartAlert(false)} + > +
+
+ {product.name} +
+
{product.name}
+
+ + Lihat Keranjang + +
+
+ +
+
Kamu Mungkin Juga Suka
+ + + +
+
+
+
+ ) +} + +const informationTabOptions = [ + { value: 'description', label: 'Deskripsi' }, + { value: 'information', label: 'Info Penting' } +] + +const TabButton = ({ children, active, ...props }) => { + const activeClassName = active + ? 'text-danger-500 underline underline-offset-4' + : 'text-gray_r-12/80' + return ( + + ) +} + +const TabContent = ({ children, active, className = '', ...props }) => ( +
+ {children} +
+) + +export default ProductDesktopVariant diff --git a/src/lib/product/components/Product/ProductMobileVariant.jsx b/src/lib/product/components/Product/ProductMobileVariant.jsx new file mode 100644 index 00000000..29d7700d --- /dev/null +++ b/src/lib/product/components/Product/ProductMobileVariant.jsx @@ -0,0 +1,315 @@ +import Divider from '@/core/components/elements/Divider/Divider' +import Image from '@/core/components/elements/Image/Image' +import Link from '@/core/components/elements/Link/Link' +import currencyFormat from '@/core/utils/currencyFormat' +import { useEffect, useState } from 'react' +import Select from 'react-select' +import ProductSimilar from '../ProductSimilar' +import LazyLoad from 'react-lazy-load' +import { updateItemCart } from '@/core/utils/cart' +import { HeartIcon } from '@heroicons/react/24/outline' +import { useRouter } from 'next/router' +import MobileView from '@/core/components/views/MobileView' +import { toast } from 'react-hot-toast' +import { createSlug } from '@/core/utils/slug' +import BottomPopup from '@/core/components/elements/Popup/BottomPopup' +import whatsappUrl from '@/core/utils/whatsappUrl' +import { gtagAddToCart } from '@/core/utils/googleTag' + +const ProductMobileVariant = ({ product, wishlist, toggleWishlist }) => { + const router = useRouter() + + const [quantity, setQuantity] = useState('1') + const [selectedVariant, setSelectedVariant] = useState(product.id) + const [informationTab, setInformationTab] = useState(informationTabOptions[0].value) + const [addCartAlert, setAddCartAlert] = useState(false) + + const getLowestPrice = () => { + const lowest = product.price + return lowest + } + + const [activeVariant, setActiveVariant] = useState({ + id: null, + code: product.code, + name: product.name, + price: getLowestPrice(), + stock: product.stockTotal, + weight: product.weight + }) + + useEffect(() => { + if (selectedVariant) { + setActiveVariant({ + id: product.id, + code: product.code, + name: product.name, + price: product.price, + stock: product.stock, + weight: product.weight + }) + } + }, [selectedVariant, product]) + + const validAction = () => { + let isValid = true + if (!selectedVariant) { + toast.error('Pilih varian terlebih dahulu') + isValid = false + } + if (!quantity || quantity < 1 || isNaN(parseInt(quantity))) { + toast.error('Jumlah barang minimal 1') + isValid = false + } + return isValid + } + + const handleClickCart = () => { + if (!validAction()) return + gtagAddToCart(activeVariant, quantity) + updateItemCart({ + productId: activeVariant.id, + quantity, + selected: true + }) + setAddCartAlert(true) + } + + const handleClickBuy = () => { + if (!validAction()) return + router.push(`/shop/checkout?productId=${activeVariant.id}&quantity=${quantity}`) + } + + const productSimilarQuery = [ + product?.name, + `fq=-product_id_i:${product.id}`, + `fq=-manufacture_id_i:${product.manufacture?.id || 0}` + ].join('&') + + return ( + + {product.name} + +
+
+ {product.manufacture?.name ? ( + + {product.manufacture?.name} + + ) : ( +
-
+ )} + +
+

{activeVariant?.name}

+ + {activeVariant?.price?.discountPercentage > 0 && ( +
+
+ {currencyFormat(activeVariant?.price?.price * 1.11)} +
+
{activeVariant?.price?.discountPercentage}%
+
+ )} +

+ {currencyFormat(activeVariant?.price?.priceDiscount)} +

+

+ {activeVariant?.price?.priceDiscount > 0 ? ( + currencyFormat(activeVariant?.price?.priceDiscount * 1.11) + ) : ( + + Hubungi kami untuk dapatkan harga terbaik,  + + klik disini + + + )} + + {activeVariant?.price?.priceDiscount > 0 && ( + *include PPN + )} +

+
+ + + +
+
Jumlah
+
+
+ setQuantity(e.target.value)} + /> +
+ + +
+
+ + + +
+

Informasi Produk

+
+ {informationTabOptions.map((option) => ( + setInformationTab(option.value)} + > + {option.label} + + ))} +
+ + + + SKU-{product?.id} + + + {activeVariant?.code || '-'} + + + {activeVariant?.stock > 0 && ( + +
Ready Stock
+
{activeVariant?.stock > 5 ? '> 5' : '< 5'}
+
+ )} + {activeVariant?.stock == 0 && ( + + Tanya Stok + + )} +
+ + {activeVariant?.weight > 0 && {activeVariant?.weight} KG} + {activeVariant?.weight == 0 && ( + + Tanya Berat + + )} + +
+ + +
+ + + +
+

Kamu Mungkin Juga Suka

+ + + +
+ + setAddCartAlert(false)} + > +
+
+ {product.name} +
+
{product.name}
+
+ + Lihat Keranjang + +
+
+
+
Kamu Mungkin Juga Suka
+ + + +
+
+
+ ) +} + +const informationTabOptions = [ + { value: 'specification', label: 'Spesifikasi' } + // { value: 'description', label: 'Deskripsi' }, + // { value: 'information', label: 'Info Penting' } +] + +const TabButton = ({ children, active, ...props }) => { + const activeClassName = active ? 'text-danger-500 underline underline-offset-4' : 'text-gray_r-11' + return ( + + ) +} + +const TabContent = ({ children, active, className, ...props }) => ( +
+ {children} +
+) + +const SpecificationContent = ({ children, label }) => ( +
+ {label} + {children} +
+) + +export default ProductMobileVariant diff --git a/src/pages/shop/product/variant/[slug].jsx b/src/pages/shop/product/variant/[slug].jsx new file mode 100644 index 00000000..ba2a79d5 --- /dev/null +++ b/src/pages/shop/product/variant/[slug].jsx @@ -0,0 +1,75 @@ +import Seo from '@/core/components/Seo' +import LogoSpinner from '@/core/components/elements/Spinner/LogoSpinner' +import { getIdFromSlug } from '@/core/utils/slug' +import PageNotFound from '@/pages/404' +import dynamic from 'next/dynamic' +import { useRouter } from 'next/router' +import cookie from 'cookie' +import variantApi from '@/lib/product/api/variantApi' + +const BasicLayout = dynamic(() => import('@/core/components/layouts/BasicLayout')) +const Product = dynamic(() => import('@/lib/product/components/Product/Product')) + +export async function getServerSideProps(context) { + const { slug } = context.query + const cookies = context.req.headers.cookie + const cookieObj = cookies ? cookie.parse(cookies) : {} + const auth = cookieObj.auth ? JSON.parse(cookieObj.auth) : {} + const authToken = auth?.token || '' + + let product = await variantApi({ id: getIdFromSlug(slug), headers: { Token: authToken } }) + if (product?.length == 1) { + product = product[0] + /* const regexHtmlTags = /(<([^>]+)>)/gi + const regexHtmlTagsExceptP = /<\/?(?!p\b)[^>]*>/g + product.description = product.description + .replace(regexHtmlTagsExceptP, ' ') + .replace(regexHtmlTags, ' ') + .trim()*/ + } else { + product = null + } + + return { + props: { product } + } +} + +export default function ProductDetail({ product }) { + const router = useRouter() + + if (!product) return + + return ( + + + {!product && ( +
+ +
+ )} + {product && } +
+ ) +} -- cgit v1.2.3