diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/product/api/variantApi.js | 9 | ||||
| -rw-r--r-- | src/lib/product/api/variantSearchApi.js | 11 | ||||
| -rw-r--r-- | src/lib/product/components/Product/Product.jsx | 39 | ||||
| -rw-r--r-- | src/lib/product/components/Product/ProductDesktopVariant.jsx | 349 | ||||
| -rw-r--r-- | src/lib/product/components/Product/ProductMobileVariant.jsx | 324 | ||||
| -rw-r--r-- | src/pages/api/shop/variant.js | 157 | ||||
| -rw-r--r-- | src/pages/google_merchant/products/[page].js | 3 | ||||
| -rw-r--r-- | src/pages/google_merchant/products/index.js | 3 | ||||
| -rw-r--r-- | src/pages/shop/product/variant/[slug].jsx | 75 |
9 files changed, 959 insertions, 11 deletions
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/api/variantSearchApi.js b/src/lib/product/api/variantSearchApi.js new file mode 100644 index 00000000..d7b05423 --- /dev/null +++ b/src/lib/product/api/variantSearchApi.js @@ -0,0 +1,11 @@ +import _ from 'lodash-contrib' +import axios from 'axios' + +const variantSearchApi = async ({ query, operation = 'AND' }) => { + const dataProductSearch = await axios( + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/variant?${query}&operation=${operation}` + ) + return dataProductSearch.data +} + +export default variantSearchApi 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 ( - <> - <ProductMobile product={product} wishlist={wishlist} toggleWishlist={toggleWishlist} /> - <ProductDesktop product={product} wishlist={wishlist} toggleWishlist={toggleWishlist} /> - </> - ) + if (isVariant == true) { + return ( + <> + <ProductDesktopVariant + product={product} + wishlist={wishlist} + toggleWishlist={toggleWishlist} + /> + <ProductMobileVariant + product={product} + wishlist={wishlist} + toggleWishlist={toggleWishlist} + /> + </> + ) + } else { + return ( + <> + <ProductMobile product={product} wishlist={wishlist} toggleWishlist={toggleWishlist} /> + <ProductDesktop product={product} wishlist={wishlist} toggleWishlist={toggleWishlist} /> + </> + ) + } } 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..e0573357 --- /dev/null +++ b/src/lib/product/components/Product/ProductDesktopVariant.jsx @@ -0,0 +1,349 @@ +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 ( + <DesktopView> + <div className='container mx-auto pt-10'> + <div className='flex'> + <div className='w-full flex flex-wrap'> + <div className='w-5/12'> + <Image + src={product.parent.image} + alt={product.name} + className='h-[430px] object-contain object-center w-full border border-gray_r-4' + /> + </div> + + <div className='w-7/12 px-4'> + <h1 className='text-title-md leading-10 font-medium'>{product?.name}</h1> + <div className='mt-10'> + <div className='flex p-3'> + <div className='w-1/4 text-gray_r-12/70'>Nomor SKU</div> + <div className='w-3/4'>SKU-{product.id}</div> + </div> + <div className='flex p-3 bg-gray_r-4'> + <div className='w-1/4 text-gray_r-12/70'>Part Number</div> + <div className='w-3/4'>{product.code || '-'}</div> + </div> + <div className='flex p-3'> + <div className='w-1/4 text-gray_r-12/70'>Manufacture</div> + <div className='w-3/4'> + {product.manufacture?.name ? ( + <Link + href={createSlug( + '/shop/brands/', + product.manufacture?.name, + product.manufacture?.id + )} + > + {product.manufacture?.name} + </Link> + ) : ( + <div>-</div> + )} + </div> + </div> + <div className='flex p-3 bg-gray_r-4'> + <div className='w-1/4 text-gray_r-12/70'>Berat Barang</div> + <div className='w-3/4'> + {product?.weight > 0 && <span>{product?.weight} KG</span>} + {product?.weight == 0 && ( + <a + href={whatsappUrl('productWeight', { + name: product.name, + url: createSlug('/shop/product/', product.name, product.id, true) + })} + className='text-danger-500 font-medium' + > + Tanya Berat + </a> + )} + </div> + </div> + </div> + </div> + + {/* <div className='w-full'> + <div className='mt-12'> + <div className='text-h-lg font-semibold'>Informasi Produk</div> + <div className='flex gap-x-4 mt-6 mb-4'> + {informationTabOptions.map((option) => ( + <TabButton + value={option.value} + key={option.value} + active={informationTab == option.value} + onClick={() => setInformationTab(option.value)} + > + {option.label} + </TabButton> + ))} + </div> + <div className='flex'> + <div className='w-3/4 leading-7 product__description'> + <TabContent active={informationTab == 'description'}> + <span + dangerouslySetInnerHTML={{ + __html: + product.description != '' + ? product.description + : 'Belum ada deskripsi produk.' + }} + /> + </TabContent> + + <TabContent active={informationTab == 'information'}> + Belum ada informasi. + </TabContent> + </div> + </div> + </div> + </div> */} + </div> + <div className='w-[25%]'> + {lowestPrice?.priceDiscount > 0 ? ( + <> + <div className='flex gap-x-2 mb-3 items-center'> + <div className='flex'> + <span className='text-gray-400 text-md'>Harga Sebelum PPN : </span> + </div> + <div className='flex'> + <span className=' text-body-1 '> + {currencyFormat(lowestPrice?.priceDiscount)} + </span> + </div> + </div> + <span className='font-semibold'>Termasuk PPN :</span> + <div className='flex gap-x-1 items-center mt-2 '> + <div className='badge-solid-red text-caption-1'> + {lowestPrice?.discountPercentage}% + </div> + <div className='text-gray_r-11 line-through text-caption-1'> + {currencyFormat(lowestPrice?.price * 1.11)} + </div> + <h3 className='text-danger-500 font-semibold text-title-sm'> + {currencyFormat(lowestPrice?.priceDiscount * 1.11)} + </h3> + </div> + </> + ) : ( + <span className='text-gray_r-12/90 font-normal text-h-sm'> + Hubungi kami untuk dapatkan harga terbaik, + <a + href={whatsappUrl('product', { + name: product.name, + url: createSlug('/shop/product/', product.name, product.id, true) + })} + className='text-danger-500 underline' + > + klik disini + </a> + </span> + )} + <div className='flex gap-x-3 mt-4'> + <input + type='number' + className='form-input w-16 py-2 text-center bg-gray_r-1' + ref={setVariantQuantityRef(product.id)} + defaultValue={1} + /> + <button + type='button' + onClick={() => handleAddToCart(product.id)} + className='flex-1 py-2 btn-yellow' + > + Keranjang + </button> + <button + type='button' + onClick={() => handleBuy(product.id)} + className='flex-1 py-2 btn-solid-red' + > + Beli + </button> + </div> + <div className='flex mt-4'> + <button className='flex items-center gap-x-1' onClick={toggleWishlist}> + {wishlist.data?.productTotal > 0 ? ( + <HeartIcon className='w-6 fill-danger-500 text-danger-500' /> + ) : ( + <HeartIcon className='w-6' /> + )} + Wishlist + </button> + </div> + <div className='border border-gray_r-6 overflow-auto mt-4'> + <div className='font-medium text-center p-4 bg-gray_r-1 border-b border-gray_r-6 sticky top-0 z-10'> + Produk Serupa + </div> + <div className='h-full divide-y divide-gray_r-6 max-h-96'> + {productSimilarInBrand && + productSimilarInBrand?.map((product) => ( + <div className='py-2' key={product.id}> + <ProductCard product={product} variant='horizontal' /> + </div> + ))} + </div> + </div> + </div> + </div> + + <div className='my-12'> + <div className='text-h-lg font-semibold mb-6'>Kamu Mungkin Juga Suka</div> + <LazyLoad> + <ProductSimilar query={productSimilarQuery} /> + </LazyLoad> + </div> + + <BottomPopup + className='!container' + title='Berhasil Ditambahkan' + active={addCartAlert} + close={() => setAddCartAlert(false)} + > + <div className='flex mt-4'> + <div className='w-[10%]'> + <Image + src={product.image} + alt={product.name} + className='h-32 object-contain object-center w-full border border-gray_r-4' + /> + </div> + <div className='ml-3 flex flex-1 items-center font-normal'>{product.name}</div> + <div className='ml-3 flex items-center font-normal'> + <Link href='/shop/cart' className='flex-1 py-2 text-gray_r-12 btn-yellow'> + Lihat Keranjang + </Link> + </div> + </div> + + <div className='mt-8 mb-4'> + <div className='text-h-sm font-semibold mb-6'>Kamu Mungkin Juga Suka</div> + <LazyLoad> + <ProductSimilar query={productSimilarQuery} /> + </LazyLoad> + </div> + </BottomPopup> + </div> + </DesktopView> + ) +} + +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 ( + <button {...props} type='button' className={`font-medium ${activeClassName}`}> + {children} + </button> + ) +} + +const TabContent = ({ children, active, className = '', ...props }) => ( + <div {...props} className={`${active ? 'block' : 'hidden'} ${className}`}> + {children} + </div> +) + +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..26c3fea3 --- /dev/null +++ b/src/lib/product/components/Product/ProductMobileVariant.jsx @@ -0,0 +1,324 @@ +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 ( + <MobileView> + <Image + src={product.parent.image} + alt={product.name} + className='h-72 object-contain object-center w-full border-b border-gray_r-4' + /> + + <div className='p-4'> + <div className='flex items-end mb-2'> + {product.manufacture?.name ? ( + <Link + href={createSlug('/shop/brands/', product.manufacture?.name, product.manufacture?.id)} + > + {product.manufacture?.name} + </Link> + ) : ( + <div>-</div> + )} + <button type='button' className='ml-auto' onClick={toggleWishlist}> + {wishlist.data?.productTotal > 0 ? ( + <HeartIcon className='w-6 fill-danger-500 text-danger-500' /> + ) : ( + <HeartIcon className='w-6' /> + )} + </button> + </div> + <h1 className='leading-6 font-medium mb-3'>{activeVariant?.name}</h1> + + {activeVariant?.price?.priceDiscount > 0 ? ( + <> + <div className='flex gap-x-1 items-center'> + <div className='text-gray_r-11 line-through text-caption-1'>Harga Sebelum PPN :</div> + <div className='text-gray_r-12 line-through text-caption-1'> + {' '} + {currencyFormat(activeVariant?.price?.priceDiscount)} + </div> + </div> + <div className='mt-2'> + <span className='font-semibold '>Termasuk PPN :</span> + <div className='flex gap-x-2 items-center mt-2'> + {activeVariant?.price?.discountPercentage > 0 && ( + <> + <div className='badge-solid-red'> + {activeVariant?.price?.discountPercentage}% + </div> + <div className='text-gray_r-11 line-through text-caption-1'> + {currencyFormat(activeVariant?.price?.price * 1.11)} + </div> + </> + )} + <h3 className='text-danger-500 font-semibold'> + {currencyFormat(activeVariant?.price?.priceDiscount * 1.11)} + </h3> + </div> + </div> + </> + ) : ( + <span className='text-gray_r-11 leading-6 font-normal'> + Hubungi kami untuk dapatkan harga terbaik, + <a + href={whatsappUrl('product', { + name: product.name, + url: createSlug('/shop/product/', product.name, product.id, true) + })} + className='text-danger-500 underline' + > + klik disini + </a> + </span> + )} + </div> + + <Divider /> + + <div className='p-4'> + <div className='mt-4 mb-2'>Jumlah</div> + <div className='flex gap-x-3'> + <div className='w-2/12'> + <input + name='quantity' + type='number' + className='form-input' + value={quantity} + onChange={(e) => setQuantity(e.target.value)} + /> + </div> + <button type='button' className='btn-yellow flex-1' onClick={handleClickCart}> + Keranjang + </button> + <button type='button' className='btn-solid-red flex-1' onClick={handleClickBuy}> + Beli + </button> + </div> + </div> + + <Divider /> + + <div className='p-4'> + <h2 className='font-semibold'>Informasi Produk</h2> + <div className='flex gap-x-4 mt-4 mb-3'> + {informationTabOptions.map((option) => ( + <TabButton + value={option.value} + key={option.value} + active={informationTab == option.value} + onClick={() => setInformationTab(option.value)} + > + {option.label} + </TabButton> + ))} + </div> + + <TabContent + active={informationTab == 'specification'} + className='rounded border border-gray_r-6 divide-y divide-gray_r-6' + > + <SpecificationContent label='Nomor SKU'> + <span>SKU-{product?.id}</span> + </SpecificationContent> + <SpecificationContent label='Part Number'> + <span>{activeVariant?.code || '-'}</span> + </SpecificationContent> + <SpecificationContent label='Stok'> + {activeVariant?.stock > 0 && ( + <span className='flex gap-x-1.5'> + <div className='badge-solid-red'>Ready Stock</div> + <div className='badge-gray'>{activeVariant?.stock > 5 ? '> 5' : '< 5'}</div> + </span> + )} + {activeVariant?.stock == 0 && ( + <a + href={whatsappUrl('product', { + name: product.name, + url: createSlug('/shop/product/', product.name, product.id, true) + })} + className='text-danger-500 font-medium' + > + Tanya Stok + </a> + )} + </SpecificationContent> + <SpecificationContent label='Berat Barang'> + {activeVariant?.weight > 0 && <span>{activeVariant?.weight} KG</span>} + {activeVariant?.weight == 0 && ( + <a + href={whatsappUrl('productWeight', { + name: product.name, + url: createSlug('/shop/product/', product.name, product.id, true) + })} + className='text-danger-500 font-medium' + > + Tanya Berat + </a> + )} + </SpecificationContent> + </TabContent> + + <TabContent + active={informationTab == 'description'} + className='leading-6 text-gray_r-11' + dangerouslySetInnerHTML={{ + __html: product.description != '' ? product.description : 'Belum ada deskripsi produk.' + }} + /> + </div> + + <Divider /> + + <div className='p-4'> + <h2 className='font-semibold mb-4'>Kamu Mungkin Juga Suka</h2> + <LazyLoad> + <ProductSimilar query={productSimilarQuery} /> + </LazyLoad> + </div> + + <BottomPopup + title='Berhasil Ditambahkan' + active={addCartAlert} + close={() => setAddCartAlert(false)} + > + <div className='flex mt-4'> + <div className='w-[15%]'> + <Image + src={product.image} + alt={product.name} + className='h-20 object-contain object-center w-full border border-gray_r-4' + /> + </div> + <div className='ml-3 flex flex-1 items-center text-sm font-normal'>{product.name}</div> + <div className='ml-3 flex items-center text-sm font-normal'> + <Link href='/shop/cart' className='flex-1 py-2 text-gray_r-12 btn-yellow'> + Lihat Keranjang + </Link> + </div> + </div> + <div className='mt-8 mb-4'> + <div className='text-h-sm font-semibold mb-6'>Kamu Mungkin Juga Suka</div> + <LazyLoad> + <ProductSimilar query={productSimilarQuery} /> + </LazyLoad> + </div> + </BottomPopup> + </MobileView> + ) +} + +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 ( + <button {...props} type='button' className={`font-medium pb-1 ${activeClassName}`}> + {children} + </button> + ) +} + +const TabContent = ({ children, active, className, ...props }) => ( + <div {...props} className={`${active ? 'block' : 'hidden'} ${className}`}> + {children} + </div> +) + +const SpecificationContent = ({ children, label }) => ( + <div className='flex justify-between p-3'> + <span className='text-gray_r-11'>{label}</span> + {children} + </div> +) + +export default ProductMobileVariant diff --git a/src/pages/api/shop/variant.js b/src/pages/api/shop/variant.js new file mode 100644 index 00000000..30213cc3 --- /dev/null +++ b/src/pages/api/shop/variant.js @@ -0,0 +1,157 @@ +import axios from 'axios' +import camelcaseObjectDeep from 'camelcase-object-deep' + +export default async function handler(req, res) { + const { + q = '*', + page = 1, + brand = '', + category = '', + priceFrom = 0, + priceTo = 0, + orderBy = '', + operation = 'AND', + fq = '', + limit = 30 + } = req.query + + let paramOrderBy = '' + switch (orderBy) { + case 'price-asc': + paramOrderBy += 'price_discount_f ASC' + break + case 'price-desc': + paramOrderBy += 'price_discount_f DESC' + break + case 'popular': + paramOrderBy += 'search_rank_i DESC' + break + case 'popular-weekly': + paramOrderBy += 'search_rank_weekly_i DESC' + break + case 'stock': + paramOrderBy += 'stock_total_f DESC' + break + default: + paramOrderBy += 'product_rating_f DESC, price_discount_f DESC' + break + } + + let offset = (page - 1) * limit + let parameter = [ + 'facet.field=manufacture_name', + 'facet.field=category_name', + 'facet=true', + 'indent=true', + `facet.query=${escapeSolrQuery(q)}`, + `q.op=${operation}`, + `q=${escapeSolrQuery(q)}`, + 'qf=name_s', + `start=${offset}`, + `rows=${limit}`, + `sort=${paramOrderBy}` + ] + + if (priceFrom > 0 || priceTo > 0) { + parameter.push( + `fq=price_discount_f:[${priceFrom == '' ? '*' : priceFrom} TO ${ + priceTo == '' ? '*' : priceTo + }]` + ) + } + + if (brand) parameter.push(`fq=manufacture_name:${brand}`) + if (category) parameter.push(`fq=category_name:${category}`) + + // Single fq in url params + if (typeof fq === 'string') parameter.push(`fq=${fq}`) + // Multi fq in url params + if (Array.isArray(fq)) parameter = parameter.concat(fq.map((val) => `fq=${val}`)) + + let result = await axios(process.env.SOLR_HOST + '/solr/variants/select?' + parameter.join('&')) + try { + let { auth } = req.cookies + if (auth) auth = JSON.parse(auth) + result.data.response.products = productResponseMap( + result.data.response.docs, + auth?.pricelist || false + ) + result.data.responseHeader.params.start = parseInt(result.data.responseHeader.params.start) + result.data.responseHeader.params.rows = parseInt(result.data.responseHeader.params.rows) + delete result.data.response.docs + result.data = camelcaseObjectDeep(result.data) + res.status(200).json(result.data) + } catch (error) { + res.status(400).json({ error: error.message }) + } +} + +const escapeSolrQuery = (query) => { + if (query == '*') return query + + const specialChars = /([\+\-\!\(\)\{\}\[\]\^"~\*\?:\\\/])/g + const words = query.split(/\s+/) + const escapedWords = words.map((word) => { + if (specialChars.test(word)) { + return `"${word.replace(specialChars, '\\$1')}"` + } + return word + }) + + return escapedWords.join(' ') +} + +const productResponseMap = (products, pricelist) => { + return products.map((product) => { + let price = product.price_f || 0 + let priceDiscount = product.price_discount_f || 0 + let discountPercentage = product.discount_f || 0 + + if (pricelist) { + const pricelistDiscount = product?.[`price_${pricelist}_f`] || false + const pricelistDiscountPerc = product?.[`discount_${pricelist}_f`] || false + + if (pricelistDiscount && pricelistDiscount > 0) priceDiscount = pricelistDiscount + if (pricelistDiscountPerc && pricelistDiscountPerc > 0) + discountPercentage = pricelistDiscountPerc + } + + if (product?.flashsale_id_i > 0) { + price = product?.flashsale_base_price_f || 0 + priceDiscount = product?.flashsale_price_f || 0 + discountPercentage = product?.flashsale_discount_f || 0 + } + + let productMapped = { + id: product.product_id_i || '', + image: product.image_s || '', + code: product.default_code_s || '', + name: product.name_s || '', + lowestPrice: { price, priceDiscount, discountPercentage }, + variantTotal: product.variant_total_i || 0, + stockTotal: product.stock_total_f || 0, + weight: product.weight_f || 0, + manufacture: {}, + categories: [], + flashSale: { + id: product?.flashsale_id_i, + name: product?.product?.flashsale_name_s + } + } + + if (product.manufacture_id_i && product.manufacture_name_s) { + productMapped.manufacture = { + id: product.manufacture_id_i || '', + name: product.manufacture_name_s || '' + } + } + + productMapped.categories = [ + { + id: product.category_id_i || '', + name: product.category_name_s || '' + } + ] + return productMapped + }) +} diff --git a/src/pages/google_merchant/products/[page].js b/src/pages/google_merchant/products/[page].js index 4a7d95c6..f9eac45d 100644 --- a/src/pages/google_merchant/products/[page].js +++ b/src/pages/google_merchant/products/[page].js @@ -1,6 +1,7 @@ import { createSlug } from '@/core/utils/slug' import toTitleCase from '@/core/utils/toTitleCase' import productSearchApi from '@/lib/product/api/productSearchApi' +import variantSearchApi from '@/lib/product/api/variantSearchApi' import _ from 'lodash-contrib' import { create } from 'xmlbuilder' @@ -18,7 +19,7 @@ export async function getServerSideProps({ res, query }) { orderBy: 'popular', fq: 'image_s:["" TO *]' } - const products = await productSearchApi({ query: _.toQuery(queries) }) + const products = await variantSearchApi({ query: _.toQuery(queries) }) const productItems = [] products.response.products.forEach((product) => { diff --git a/src/pages/google_merchant/products/index.js b/src/pages/google_merchant/products/index.js index a1f59d39..d3cdc514 100644 --- a/src/pages/google_merchant/products/index.js +++ b/src/pages/google_merchant/products/index.js @@ -1,4 +1,5 @@ import productSearchApi from '@/lib/product/api/productSearchApi' +import variantSearchApi from '@/lib/product/api/variantSearchApi' import _ from 'lodash-contrib' const limit = 5000 @@ -9,7 +10,7 @@ export async function getServerSideProps() { priceFrom: 1, fq: 'image_s:["" TO *]' } - const products = await productSearchApi({ query: _.toQuery(queries) }) + const products = await variantSearchApi({ query: _.toQuery(queries) }) const { numFound } = products.response const pageTotal = Math.ceil(numFound / limit) 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 <PageNotFound /> + + return ( + <BasicLayout> + <Seo + title={product?.name || '' + ' - Indoteknik.com' || ''} + description='Temukan pilihan produk B2B Industri & Alat Teknik untuk Perusahaan, UMKM & Pemerintah dengan lengkap, mudah dan transparan.' + openGraph={{ + url: process.env.NEXT_PUBLIC_SELF_HOST + router.asPath, + images: [ + { + url: product?.image, + width: 800, + height: 800, + alt: product?.name + } + ], + type: 'product' + }} + additionalMetaTags={[ + { + name: 'keywords', + content: `${product?.name}, Harga ${product?.name}, Beli ${product?.name}, Spesifikasi ${product?.name}` + } + ]} + /> + {!product && ( + <div className='container mx-auto flex justify-center pt-10'> + <LogoSpinner width={36} height={36} /> + </div> + )} + {product && <Product product={product} isVariant={true} />} + </BasicLayout> + ) +} |
