diff options
| author | HATEC\SPVDEV001 <tri.susilo@altama.co.id> | 2023-07-17 16:58:42 +0700 |
|---|---|---|
| committer | HATEC\SPVDEV001 <tri.susilo@altama.co.id> | 2023-07-17 16:58:42 +0700 |
| commit | e76d537689d08fb4c15d482ffa996b8012dbc941 (patch) | |
| tree | 17267b16ba50c89d55c29ea8c962312c05bdcb70 | |
| parent | 581334e8d077916c3a9ab87fd5b6e6b5126aba12 (diff) | |
| parent | e39d3b0082e83ad08044918f0b6d8e977223100d (diff) | |
Merge branch 'Feature/voucher' into Feature/promotion_programvaoucher
# Conflicts:
# src/lib/cart/components/Cart.jsx
# src/lib/checkout/components/Checkout.jsx
# src/lib/product/components/Product/Product.jsx
# src/lib/product/components/Product/ProductDesktop.jsx
# src/lib/product/components/Product/ProductMobile.jsx
27 files changed, 1642 insertions, 85 deletions
diff --git a/public/images/DISKONICON.svg b/public/images/DISKONICON.svg new file mode 100644 index 00000000..95d4eb47 --- /dev/null +++ b/public/images/DISKONICON.svg @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Generator: Adobe Illustrator 24.1.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> +<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" + viewBox="0 0 150 150" style="enable-background:new 0 0 150 150;" xml:space="preserve"> +<style type="text/css"> + .st0{fill:#E20613;} +</style> +<g> + <path class="st0" d="M73.3,149c-5.1-1-8.9-4-12.3-7.6c-1.7-1.8-3.5-3.5-5.1-5.3c-2.9-3.2-6.5-4.5-10.7-4.4c-3.7,0-7.3,0.1-11-0.1 + c-1.6-0.1-3.3-0.3-4.8-0.7c-6.3-1.6-9.8-5.9-10.6-12.2c-0.5-3.7-0.6-7.4-0.3-11.1c0.4-6.3-1.6-11.3-6.5-15.4 + c-2.7-2.3-5.2-4.9-7.3-7.7c-4.8-6.2-4.7-12.6,0-18.8C6,63.7,7.7,62,9.3,60.3c1.1-1.2,2.2-2.3,3.4-3.3c4.3-3.5,5.9-8,5.7-13.5 + c-0.2-3.8-0.2-7.6,0.3-11.4c1.1-8.8,6.3-13.4,15.2-13.7c4.5-0.2,8.9-0.1,13.4-0.2c3-0.1,5.5-1.3,7.6-3.4c3-2.9,5.8-5.9,8.8-8.7 + c2.4-2.3,5.2-4,8.5-4.7c4.5-1,8.6,0.2,12,3c2.9,2.3,5.8,4.9,8.2,7.7c4,4.7,8.9,6.6,15,6.3c4.1-0.2,8.3-0.1,12.4,0.6 + c6.7,1.1,10.8,5.6,11.5,12.4c0.4,3.6,0.6,7.3,0.3,10.9c-0.4,6.3,1.4,11.4,6.5,15.4c1.1,0.8,1.9,1.9,2.9,2.8c3.7,3.6,7,7.5,8,12.7 + c0,1.2,0,2.3,0,3.5c-1,5.3-4.3,9.1-8,12.7c-1,1-2,2.1-3.1,3c-4.8,3.8-6.6,8.8-6.3,14.9c0.2,3.7,0.1,7.5-0.3,11.2 + c-0.7,5.9-3.7,10.2-9.5,12c-1.9,0.6-3.8,0.9-5.8,1c-3.6,0.2-7.3,0.1-11,0.1c-4.3-0.1-8,1.3-10.9,4.6c-1.6,1.7-3.3,3.4-4.9,5.1 + c-3.5,3.6-7.3,6.7-12.4,7.7C75.6,149,74.4,149,73.3,149z M26.9,109.4c0.1,0,0.1,0,0.2,0c0,1.8,0,3.7,0,5.5c0.2,6,2.1,7.9,8,8.1 + c3.2,0.1,6.4,0.1,9.5,0c7.1-0.2,13.1,2.2,17.9,7.5c2,2.2,4.2,4.4,6.4,6.4c4.7,4.4,7.4,4.4,12.1,0c2.7-2.5,5.3-5.2,7.9-7.8 + c3.7-3.7,8.1-5.8,13.4-6c4.2-0.1,8.4-0.1,12.6-0.2c6-0.2,7.9-2.1,8.1-8.1c0.1-3.2,0.1-6.5,0-9.7c-0.2-6.9,2.2-12.8,7.3-17.5 + c2.3-2.1,4.4-4.3,6.5-6.5c4.5-4.8,4.5-7.5,0.1-12.3c-2.6-2.7-5.3-5.3-7.9-8c-3.6-3.6-5.7-8-5.8-13.1c-0.1-4.2-0.1-8.4-0.2-12.6 + c-0.2-6.1-2-8-8.1-8.2c-3.2-0.1-6.4-0.1-9.5,0c-7,0.2-12.9-2.2-17.7-7.4c-1.3-1.4-2.6-2.7-3.9-4c-1.6-1.5-3.2-3.1-5-4.4 + c-2.7-2-4.8-2-7.5,0c-1.3,1-2.5,2.1-3.7,3.2c-2.3,2.2-4.5,4.5-6.8,6.7c-3.6,3.5-7.9,5.6-13,5.7c-4.2,0.1-8.5,0.1-12.7,0.2 + c-5.9,0.2-7.8,2.1-8,8c-0.1,3.3-0.1,6.6,0,9.8c0.1,6.9-2.2,12.7-7.3,17.4c-2.3,2.1-4.4,4.3-6.5,6.5c-4.6,4.9-4.6,7.5,0,12.5 + c2.4,2.6,5,5.1,7.5,7.6c3.7,3.8,6,8.2,6.1,13.6C26.9,104.8,26.9,107.1,26.9,109.4z"/> + <path class="st0" d="M101.1,53.5c0,1.3-0.7,2.4-1.8,3.4C90,66.2,80.7,75.5,71.5,84.7c-4.9,4.9-9.8,9.8-14.7,14.7 + c-1.4,1.4-2.9,2-4.8,1.4c-2.8-0.9-3.9-4.3-2.1-6.7c0.3-0.3,0.6-0.7,0.9-1C64.8,79,79,64.9,93.1,50.7c1-1,2.1-1.8,3.6-1.8 + C99.2,49,101.1,50.8,101.1,53.5z"/> + <path class="st0" d="M57.7,44.6c7.2,0,13,5.9,13,13.1c0,7.2-6,13-13.1,13c-7.2,0-13-5.9-13-13.1C44.6,50.3,50.5,44.5,57.7,44.6z + M53.3,57.5c0,2.4,1.9,4.4,4.2,4.4c2.4,0.1,4.5-2,4.5-4.4c0-2.4-2-4.3-4.3-4.3C55.2,53.3,53.3,55.1,53.3,57.5z"/> + <path class="st0" d="M92.4,105.4c-7.2,0-13-5.9-13-13.1c0-7.2,5.9-13,13.1-13c7.2,0,13,5.9,13,13.1 + C105.4,99.7,99.6,105.5,92.4,105.4z M96.7,92.4c0-2.4-1.9-4.3-4.3-4.4c-2.3,0-4.4,2-4.4,4.3c0,2.4,1.9,4.4,4.4,4.4 + C94.9,96.7,96.7,94.8,96.7,92.4z"/> +</g> +</svg> diff --git a/public/robots.txt b/public/robots.txt index a4ee103d..418e72f1 100644 --- a/public/robots.txt +++ b/public/robots.txt @@ -1,13 +1,23 @@ # This says to apply these settings to ALL search engine spiders/crawlers # User-agent: * +# Version 1.0 User-agent: AhrefsBot -User-agent: Adsbot-Google User-agent: Yandex User-agent: Googlebot +User-agent: Googlebot-mobile Disallow: /shop/product/pdf/* +Disallow: /shop/product/wishlist/* Disallow: /shop/product/https://* Disallow: /v1/* +Disallow: /shop/search?product_name=* +Disallow: /shop/search?q=* +Disallow: /shop/cart/ +Disallow: /shop/checkout/ +Disallow: /my/* +Disallow: /shop/variant/* +Disallow: /shop/search/* +Disallow: /promo/* User-agent: * Disallow: sentral.indoteknik.com/* diff --git a/src/components/ui/HeroBanner.jsx b/src/components/ui/HeroBanner.jsx index d7d3d854..6cf7902c 100644 --- a/src/components/ui/HeroBanner.jsx +++ b/src/components/ui/HeroBanner.jsx @@ -21,7 +21,7 @@ const swiperBanner = { }, modules: [Pagination, Autoplay], loop: true, - className: 'border border-gray_r-6', + className: 'border border-gray_r-6 min-h-full', slidesPerView: 1 } diff --git a/src/components/ui/PopularProduct.jsx b/src/components/ui/PopularProduct.jsx index 211291c8..bbbd18bc 100644 --- a/src/components/ui/PopularProduct.jsx +++ b/src/components/ui/PopularProduct.jsx @@ -16,14 +16,14 @@ const PopularProduct = () => { <> <MobileView> <div className='px-4'> - <div className='font-medium mb-4'>Produk Banyak Dilihat</div> + <div className='font-semibold mb-4'>Produk Banyak Dilihat</div> <ProductSlider products={popularProduct.data} simpleTitle /> </div> </MobileView> <DesktopView> <div className='border border-gray_r-6 h-full overflow-auto'> - <div className='font-medium text-center p-4 bg-gray_r-1 border-b border-gray_r-6 sticky top-0 z-10'> + <div className='font-semibold text-center p-4 bg-gray_r-1 border-b border-gray_r-6 sticky top-0 z-10'> Produk Banyak Dilihat </div> <div className='h-full divide-y divide-gray_r-6'> diff --git a/src/core/components/layouts/BasicLayout.jsx b/src/core/components/layouts/BasicLayout.jsx index 04b189f9..70482e12 100644 --- a/src/core/components/layouts/BasicLayout.jsx +++ b/src/core/components/layouts/BasicLayout.jsx @@ -2,11 +2,26 @@ import dynamic from 'next/dynamic' import BasicFooter from '../elements/Footer/BasicFooter' import Image from 'next/image' import whatsappUrl from '@/core/utils/whatsappUrl' +import { useEffect, useState } from 'react' +import axios from 'axios' +import odooApi from '@/core/api/odooApi' const Navbar = dynamic(() => import('../elements/Navbar/Navbar')) const AnimationLayout = dynamic(() => import('./AnimationLayout')) const BasicLayout = ({ children }) => { + useEffect(() => { + const getIP = async () => { + const ip = await odooApi('GET', '/api/ip-address') + const data = { + page_title: document.title, + url: window.location.href, + ip: ip + } + axios.get(`/api/user-activity?page_title=${data.page_title}&url=${data.url}&ip=${data.ip}`) + } + getIP() + }, []) return ( <> <Navbar /> diff --git a/src/core/utils/googleTag.js b/src/core/utils/googleTag.js new file mode 100644 index 00000000..6d7476bd --- /dev/null +++ b/src/core/utils/googleTag.js @@ -0,0 +1,79 @@ +const mapVariants = (variants) => { + return variants.map((variant) => { + const res = { + item_id: variant.id, + item_name: variant.parent.name, + discount: variant.price.price - variant.price.priceDiscount, + item_brand: variant.manufacture?.name, + item_variant: variant.code || variant.id, + price: variant.price.price + } + if (variant?.quantity > 0) { + res.quantity = variant.quantity + } + return res + }) +} + +const sumTotal = (variants) => { + let totalPurchase = variants.reduce((total, x) => total + x.price.price, 0) + let totalDiscount = variants.reduce( + (total, x) => total + (x.price.price - x.price.priceDiscount), + 0 + ) + let subtotal = totalPurchase - totalDiscount + let tax = Math.round(subtotal * 0.11) + let grandTotal = subtotal + tax + return { + totalPurchase: totalPurchase, + totalDiscount: totalDiscount, + subtotal: subtotal, + tax: tax, + grandTotal: Math.round(grandTotal) + } +} + +export const gtagAddToCart = (variant, quantity) => { + const param = { + currency: 'IDR', + value: variant.price.priceDiscount * quantity, + items: [ + { + item_id: variant.id, + item_name: variant.name, + discount: variant.price.price - variant.price.priceDiscount, + item_brand: variant.manufacture?.name, + item_variant: variant.code || variant.id, + price: variant.price.price, + quantity + } + ] + } + gtag('event', 'add_to_cart', param) +} + +export const gtagViewItem = (variants) => { + const items = mapVariants(variants) + const param = { currency: 'IDR', value: variants[0].price.priceDiscount, items } + gtag('event', 'view_item', param) +} + +export const gtagBeginCheckout = (variants) => { + const items = mapVariants(variants) + const { subtotal } = sumTotal(variants) + const param = { currency: 'IDR', value: subtotal, items } + gtag('event', 'begin_checkout', param) +} + +export const gtagPurchase = (variants, shipping, transactionId) => { + const items = mapVariants(variants) + const { grandTotal } = sumTotal(variants) + const param = { + currency: 'IDR', + value: grandTotal, + transactionId, + shipping, + items + } + gtag('event', 'purchase', param) +} diff --git a/src/lib/cart/components/Cart.jsx b/src/lib/cart/components/Cart.jsx index 41efffe9..efbcf76b 100644 --- a/src/lib/cart/components/Cart.jsx +++ b/src/lib/cart/components/Cart.jsx @@ -21,6 +21,7 @@ import useAuth from '@/core/hooks/useAuth' import LogoSpinner from '@/core/components/elements/Spinner/LogoSpinner' import { getPromotionProgram } from '@/lib/promotinProgram/api/homepageApi' import PromotionType from '@/lib/promotinProgram/components/PromotionType' +import { gtagBeginCheckout } from '@/core/utils/googleTag' const Cart = () => { const router = useRouter() @@ -205,6 +206,15 @@ const Cart = () => { setPromotionActiveId(promoId) } + const handleCheckout = () => { + gtagBeginCheckout(products) + router.push('/shop/checkout') + } + + const totalOrder = totalPriceBeforeTax - totalDiscountAmount + totalTaxAmount + const tax = totalOrder * 0.11 + const totalPrice = totalOrder + tax + return ( <> <BottomPopup @@ -473,15 +483,19 @@ const Cart = () => { ))} <div className='sticky bottom-0 left-0 w-full p-4 mt-auto border-t border-gray_r-6 bg-white'> - <div className='flex justify-between mb-4'> - <div className='text-gray_r-11'> - Total: - <span className='text-danger-500 font-semibold'> - - {selectedProduct().length > 0 - ? currencyFormat(totalPriceBeforeTax - totalDiscountAmount + totalTaxAmount) - : '-'} - </span> + <div className='flex flex-col gap-y-3 mb-4'> + <div className='text-gray_r-11 flex justify-between'> + <span>Total Pesanan</span> + <span className='text-gray_r-12'>{currencyFormat(totalOrder)}</span> + </div> + <div className='text-gray_r-11 flex justify-between'> + PPN 11% + <span className='text-gray_r-12'>{currencyFormat(tax)}</span> + </div> + <hr /> + <div className='text-gray_r-12 flex font-semibold justify-between'> + Total Harga: + <span className='text-danger-500'>{currencyFormat(totalPrice)}</span> </div> </div> <div className='flex gap-x-3'> @@ -783,15 +797,19 @@ const Cart = () => { <div className='col-span-3 pl-4'> <div className='sticky top-48 w-full p-4 rounded border border-gray_r-6 bg-white'> <h1 className='text-title-sm font-semibold mb-6'>Ringkasan Belanja</h1> - <div className='flex justify-between mb-4'> - <div className='text-gray_r-11'> - Total: - <span className='text-danger-500 font-semibold'> - - {selectedProduct().length > 0 - ? currencyFormat(totalPriceBeforeTax - totalDiscountAmount + totalTaxAmount) - : '-'} - </span> + <div className='flex flex-col gap-y-3 mb-4'> + <div className='text-gray_r-11 flex justify-between'> + <span>Total Pesanan</span> + <span className='text-gray_r-12'>{currencyFormat(totalOrder)}</span> + </div> + <div className='text-gray_r-11 flex justify-between'> + PPN 11% + <span className='text-gray_r-12'>{currencyFormat(tax)}</span> + </div> + <hr /> + <div className='text-gray_r-12 flex font-semibold justify-between'> + Total Harga: + <span className='text-danger-500'>{currencyFormat(totalPrice)}</span> </div> </div> <div className='flex gap-x-3'> @@ -807,7 +825,7 @@ const Cart = () => { type='button' className='btn-solid-red flex-1' disabled={selectedProduct().length == 0} - onClick={() => router.push('/shop/checkout')} + onClick={handleCheckout} > Checkout </button> diff --git a/src/lib/checkout/api/getVoucher.js b/src/lib/checkout/api/getVoucher.js new file mode 100644 index 00000000..03ac3d6d --- /dev/null +++ b/src/lib/checkout/api/getVoucher.js @@ -0,0 +1,11 @@ +import odooApi from '@/core/api/odooApi' + +export const getVoucher = async () => { + const dataVoucher = await odooApi('GET', `/api/v1/voucher`) + return dataVoucher +} + +export const findVoucher = async (code) => { + const dataVoucher = await odooApi('GET', `/api/v1/voucher?code=${code}`) + return dataVoucher +} diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx index f5b27cfb..9c026372 100644 --- a/src/lib/checkout/components/Checkout.jsx +++ b/src/lib/checkout/components/Checkout.jsx @@ -16,14 +16,16 @@ import { useRouter } from 'next/router' import VariantGroupCard from '@/lib/variant/components/VariantGroupCard' import axios from 'axios' import Image from '@/core/components/elements/Image/Image' +import imageNext from 'next/image' import MobileView from '@/core/components/views/MobileView' import DesktopView from '@/core/components/views/DesktopView' import ExpedisiList from '../api/ExpedisiList' import whatsappUrl from '@/core/utils/whatsappUrl' import { createSlug } from '@/core/utils/slug' -import { Button, Modal } from 'flowbite-react' import BottomPopup from '@/core/components/elements/Popup/BottomPopup' import { useQuery } from 'react-query' +import { gtagPurchase } from '@/core/utils/googleTag' +import { findVoucher, getVoucher } from '../api/getVoucher' const SELF_PICKUP_ID = 32 @@ -84,7 +86,45 @@ const Checkout = () => { const [selectedExpedisiService, setselectedExpedisiService] = useState(null) const [etd, setEtd] = useState(null) const [etdFix, setEtdFix] = useState(null) + const [bottomPopup, SetBottomPopup] = useState(null) + const [listVouchers, SetListVoucher] = useState(null) + const [activeVoucher, SetActiveVoucher] = useState(null) + const [discountVoucher, SetDiscountVoucher] = useState(0) + const [codeVoucher, SetCodeVoucher] = useState(null) + const [findCodeVoucher, SetFindVoucher] = useState(null) + const [selisihHargaCode, SetSelisihHargaCode] = useState(null) + const [buttonTerapkan, SetButtonTerapkan] = useState(false) + const [checkoutValidation, setCheckoutValidation] = useState(false) + + const expedisiValidation = useRef(null) + + const voucher = async () => { + let dataVoucher = await getVoucher() + SetListVoucher(dataVoucher) + } + const VoucherCode = async (code) => { + let dataVoucher = await findVoucher(code) + if (dataVoucher.length <= 0) { + SetFindVoucher(1) + return + } + let addNewLine = dataVoucher[0] + let checkList = listVouchers.findIndex((voucher) => voucher.code == addNewLine.code) + if (checkList >= 0) return + if (totalAmount - totalDiscountAmount < addNewLine.minPurchaseAmount) { + SetSelisihHargaCode( + currencyFormat(addNewLine.minPurchaseAmount - (totalAmount - totalDiscountAmount)) + ) + SetFindVoucher(2) + return + } else { + SetFindVoucher(3) + SetButtonTerapkan(true) + } + SetListVoucher((prevList) => [addNewLine, ...prevList]) + SetActiveVoucher(addNewLine.code) + } useEffect(() => { const loadExpedisi = async () => { let dataExpedisi = await ExpedisiList() @@ -96,8 +136,39 @@ const Checkout = () => { setExpedisi(dataExpedisi) } loadExpedisi() + voucher() }, []) + const hitungDiscountVoucher = (code) => { + let dataVoucherIndex = listVouchers.findIndex((voucher) => voucher.code == code) + let dataActiveVoucher = listVouchers[dataVoucherIndex] + + let countDiscount = 0 + + if (dataActiveVoucher.discountType === 'percentage') { + countDiscount = (totalAmount - totalDiscountAmount) * (dataActiveVoucher.discountAmount / 100) + if ( + dataActiveVoucher.maxDiscountAmount > 0 && + countDiscount > dataActiveVoucher.maxDiscountAmount + ) { + countDiscount = dataActiveVoucher.maxDiscountAmount + } + } else { + countDiscount = dataActiveVoucher.discountAmount + } + + return countDiscount + } + + useEffect(() => { + if (!listVouchers) return + if (!activeVoucher) return + + const countDiscount = hitungDiscountVoucher(activeVoucher) + + SetDiscountVoucher(countDiscount) + }, [activeVoucher, listVouchers]) + useEffect(() => { setProducts(cartCheckout?.products) setCheckWeight(cartCheckout?.hasProductWithoutWeight) @@ -105,6 +176,7 @@ const Checkout = () => { }, [cartCheckout]) useEffect(() => { + setCheckoutValidation(false) const loadServiceRajaOngkir = async () => { const body = { origin: 2127, @@ -154,6 +226,9 @@ const Checkout = () => { useEffect(() => { if (selectedExpedisi) { let serviceType = selectedExpedisi.split(',') + if (serviceType[0] === 0) { + setSelectedExpedisi(0) + } setselectedCarrier(serviceType[0]) setselectedCarrierId(serviceType[1]) setListServiceExpedisi([]) @@ -171,6 +246,21 @@ const Checkout = () => { toast.error('Maksimal ukuran file adalah 5MB', { position: 'bottom-center' }) return } + if (selectedExpedisi === 0) { + setCheckoutValidation(true) + if (expedisiValidation.current) { + const position = expedisiValidation.current.getBoundingClientRect() + window.scrollTo({ + top: position.top - 300 + window.pageYOffset, + behavior: 'smooth' + }) + } + return + } + if (selectedCarrier != 1 && biayaKirim == 0) { + toast.error('Maaf, layanan tidak tersedia. Mohon pilih expedisi lain.') + return + } setIsLoading(true) const productOrder = products.map((product) => ({ product_id: product.id, @@ -184,6 +274,7 @@ const Checkout = () => { delivery_amount: biayaKirim, carrier_id: selectedCarrierId, delivery_service_type: selectedExpedisiService, + voucher: activeVoucher, type: 'sale_order' } if (poNumber.current.value) data.po_number = poNumber.current.value @@ -195,16 +286,219 @@ const Checkout = () => { return } - for (const product of products) deleteItemCart({ productId: product.id }) - const payment = await axios.post( - `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/midtrans-payment?transactionId=${isCheckouted.id}` - ) - setIsLoading(false) - window.location.href = payment.data.redirectUrl + gtagPurchase(products, biayaKirim, isCheckouted.name) + + const midtrans = async () => { + for (const product of products) deleteItemCart({ productId: product.id }) + const payment = await axios.post( + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/midtrans-payment?transactionId=${isCheckouted.id}` + ) + setIsLoading(false) + window.location.href = payment.data.redirectUrl + } + + gtag('event', 'conversion', { + send_to: 'AW-954540379/nDymCL3BhaQYENvClMcD', + value: + totalAmount - + totalDiscountAmount + + taxTotal + + Math.round(parseInt(biayaKirim * 1.1) / 1000) * 1000, + currency: 'IDR', + transaction_id: isCheckouted.id, + event_callback: midtrans + }) + } + + const handlingActivateCode = async () => { + VoucherCode(codeVoucher) } + const handleUseVoucher = async (code, isCheck) => { + if (isCheck) { + if (code === activeVoucher) { + SetActiveVoucher(null) + SetDiscountVoucher(0) + } else { + SetActiveVoucher(code) + SetFindVoucher(null) + document.getElementById('uniqCode').value = '' + SetButtonTerapkan(false) + } + } else { + SetActiveVoucher(code) + SetFindVoucher(null) + document.getElementById('uniqCode').value = '' + SetButtonTerapkan(false) + } + } + + const onChangeCodeVoucher = async (e) => { + SetCodeVoucher(e.target.value) + SetButtonTerapkan(false) + } + + const [isChecked, setIsChecked] = useState(false) + + const ToggleSwitch = (code) => { + setIsChecked(!isChecked) + handleUseVoucher(code, !isChecked) + } + + const taxTotal = (totalAmount - totalDiscountAmount - discountVoucher) * 0.11 + return ( <> + <BottomPopup + className='w-full md:!w-[40%] !min-h-[350px]' + active={bottomPopup} + close={() => SetBottomPopup(false)} + title='Gunakan Promo' + > + <div className='row'> + <div className='flex justify-between items-center'> + <div className='flex md:w-[70%]'> + <input + type='text' + id='uniqCode' + name='uniqCode' + className='form-input w-full rounded-md' + placeholder='Kode Voucher' + autoCapitalize='true' + onChange={(e) => onChangeCodeVoucher(e)} + /> + </div> + <div className='flex'> + <button + className='btn-solid-red flex-1 md:flex-none rounded-md' + type='button' + onClick={() => handlingActivateCode()} + disabled={buttonTerapkan} + > + Terapkan + </button> + </div> + </div> + {findCodeVoucher === 3 && activeVoucher === codeVoucher && ( + <div className='mt-2'> + <span className='text-caption-1 mt-2 text-green-600'> + Voucher berhasil ditambahkan{' '} + </span> + </div> + )} + {findCodeVoucher === 1 && ( + <div className='mt-2'> + <span className='text-caption-1 mt-2 text-red-600'> + Kode voucher salah / sudah tidak berlaku lagi. Coba voucher lainnya, ya. + </span> + </div> + )} + {findCodeVoucher === 2 && ( + <div className='mt-2'> + <span className='text-caption-1 mt-2 text-red-600'> + Tambah <span className='text-red-600'>{selisihHargaCode}</span> untuk pakai promo + ini + </span> + </div> + )} + + <hr className='mt-10 my-4 border-gray_r-10' /> + <div className=''> + {!listVouchers ? ( + <div className='flex items-center justify-center mt-4 mb-4'> + <div className='text-center'> + <h1 className='font-bold mb-4'>Tidak ada voucher tersedia</h1> + <p className='text-gray-500'>Maaf, saat ini tidak ada voucher yang tersedia.</p> + </div> + </div> + ) : ( + <h3 className='font-semibold mb-4'>Promo Khusus Untuk {auth.name}</h3> + )} + {listVouchers?.map((item) => ( + <div key={item.id} className='relative'> + {totalAmount - totalDiscountAmount < item.minPurchaseAmount && ( + <div className='absolute w-full h-full bg-gray_r-3/40 top-0 left-0 z-50' /> + )} + + <div className={`border border-solid mb-5 w-full hover:cursor-pointer p-4 `}> + <div className={`flex gap-x-3`}> + <div className='hidden md:w-[250px] md:block'> + <Image src={item.image} alt={item.name} className={`object-cover`} /> + </div> + <div className='w-full'> + <div className='flex justify-between gap-x-2 mb-1 items-center'> + <div className=''> + <h3 className='font-semibold'>{item.name}</h3> + <div className='mt-1'> + <span className='text-sm line-clamp-3'>{item.description} </span> + </div> + </div> + <div className='flex justify-end'> + <label class='relative inline-flex items-center cursor-pointer'> + <input + type='checkbox' + value='' + class='sr-only peer' + checked={activeVoucher === item.code ? true : false} + onChange={() => ToggleSwitch(item.code)} + /> + <div class="w-11 h-6 bg-gray-200 rounded-full peer dark:bg-gray-700 peer-focus:ring-4 peer-focus:ring-green-300 dark:peer-focus:ring-green-800 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-0.5 after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-green-600"></div> + </label> + </div> + </div> + <hr className='mt-3 my-4 border-gray_r-8' /> + <div className='flex justify-between items-center'> + <p className='text-justify text-sm md:text-xs'> + Kode Voucher :{' '} + <span className='text-red-500 font-semibold'>{item.code}</span> + </p> + <p className='text-sm md:text-xs'> + {activeVoucher === item.code && ( + <span className=' text-green-600'>Voucher digunakan </span> + )} + </p> + </div> + </div> + </div> + <div className='mt-3'> + <p className='text-justify text-sm '> + {totalAmount - totalDiscountAmount < item.minPurchaseAmount + ? 'Tambah ' + + currencyFormat( + item.minPurchaseAmount - (totalAmount - totalDiscountAmount) + ) + + ' untuk pakai promo ini' + : 'Potensi potongan sebesar ' + + currencyFormat(hitungDiscountVoucher(item.code))} + </p> + <hr className='mt-2 my-4 border-gray_r-8' /> + <div className='flex items-center'> + <svg + aria-hidden='true' + fill='none' + stroke='currentColor' + stroke-width='1.5' + viewBox='0 0 24 24' + className='w-5 text-black' + > + <path + d='M12 6v6h4.5m4.5 0a9 9 0 11-18 0 9 9 0 0118 0z' + stroke-linecap='round' + stroke-linejoin='round' + ></path> + </svg> + <span className='text-left ml-3 text-sm '> + Berakhir dalam <span className='text-red-600'>{item.remainingTime}</span>{' '} + lagi{' '} + </span> + </div> + </div> + </div> + </div> + ))} + </div> + </div> + </BottomPopup> <MobileView> <div className='p-4'> <Alert type='info' className='text-caption-2 flex gap-x-3'> @@ -243,6 +537,8 @@ const Checkout = () => { listExpedisi={listExpedisi} setSelectedExpedisi={setSelectedExpedisi} checkWeigth={checkWeigth} + checkoutValidation={checkoutValidation} + expedisiValidation={expedisiValidation} /> <Divider /> <SectionListService @@ -268,13 +564,15 @@ const Checkout = () => { <div>{currencyFormat(cartCheckout?.totalPurchase)}</div> </div> <div className='flex gap-x-2 justify-between'> - <div className='text-gray_r-11'>Total Diskon</div> - <div className='text-danger-500'>- {currencyFormat(cartCheckout?.totalDiscount)}</div> - </div> - <div className='flex gap-x-2 justify-between'> - <div className='text-gray_r-11'>Total Hemat</div> - <div className='text-danger-500'>- {currencyFormat(cartCheckout?.totalSavings)}</div> + <div className='text-gray_r-11'>Diskon Produk</div> + <div className='text-danger-500'>- {currencyFormat(totalDiscountAmount)}</div> </div> + {activeVoucher && ( + <div className='flex gap-x-2 justify-between'> + <div className='text-gray_r-11'>Diskon Voucher</div> + <div className='text-danger-500'>- {currencyFormat(discountVoucher)}</div> + </div> + )} <div className='flex gap-x-2 justify-between'> <div className='text-gray_r-11'>Subtotal</div> <div>{currencyFormat(cartCheckout?.subtotal)}</div> @@ -299,6 +597,42 @@ const Checkout = () => { )} </div> </div> + + <hr className='my-4 border-gray_r-6' /> + + <div className='mt-4 mb-4'> + <button + type='button' + onClick={() => SetBottomPopup(true)} + className='text-gray-900 p-4 flex items-center justify-between rounded-lg bg-white border font-medium border-gray-300 hover:bg-gray-100 py-2.5 h-[50px] w-[100%]' + > + <div className='flex items-center justify-between gap-x-3'> + <span className='text-left text-gray_r-9'> + <Image + src={'/images/DISKONICON.svg'} + alt={''} + className='object-contain object-center h-6 rounded-md' + /> + </span> + {activeVoucher ? ( + <div className=''> + <div className='text-left text-sm text-black font-semibold'> + Potongan Senilai {currencyFormat(discountVoucher)} + </div> + <div className='text-left mt-1 text-green-600 text-xs'> + Voucher berhasil digunakan + </div> + </div> + ) : ( + <span className='text-left text-sm text-gray_r-9'> + Hemat belanja dengan promo + </span> + )} + </div> + + <span className='text-left ml-1 text-gray_r-9'>{'>'}</span> + </button> + </div> {/* <p className='text-caption-2 text-gray_r-10 mb-2'>*) Belum termasuk biaya pengiriman</p> */} <p className='text-caption-2 text-gray_r-10 leading-5'> Dengan melakukan pembelian melalui website Indoteknik, saya menyetujui{' '} @@ -338,9 +672,7 @@ const Checkout = () => { <button className='flex-1 btn-yellow' onClick={checkout} - disabled={ - isLoading || !products || products?.length == 0 || priceCheck || selectedExpedisi == 0 - } + disabled={isLoading || !products || products?.length == 0 || priceCheck} > {isLoading ? 'Loading...' : 'Lanjut Pembayaran'} </button> @@ -383,6 +715,8 @@ const Checkout = () => { listExpedisi={listExpedisi} setSelectedExpedisi={setSelectedExpedisi} checkWeigth={checkWeigth} + checkoutValidation={checkoutValidation} + expedisiValidation={expedisiValidation} /> <Divider /> <SectionListService @@ -549,17 +883,15 @@ const Checkout = () => { <div>{currencyFormat(cartCheckout?.totalPurchase)}</div> </div> <div className='flex gap-x-2 justify-between'> - <div className='text-gray_r-11'>Total Diskon</div> - <div className='text-danger-500'> - - {currencyFormat(cartCheckout?.totalDiscount)} - </div> + <div className='text-gray_r-11'>Diskon Produk</div> + <div className='text-danger-500'>- {currencyFormat(cartCheckout?.totalDiscount)}</div> </div> - <div className='flex gap-x-2 justify-between'> - <div className='text-gray_r-11'>Total Hemat</div> - <div className='text-danger-500'> - - {currencyFormat(cartCheckout?.totalSavings)} + {activeVoucher && ( + <div className='flex gap-x-2 justify-between'> + <div className='text-gray_r-11'>Diskon Voucher</div> + <div className='text-danger-500'>- {currencyFormat(discountVoucher)}</div> </div> - </div> + )} <div className='flex gap-x-2 justify-between'> <div className='text-gray_r-11'>Subtotal</div> <div>{currencyFormat(cartCheckout?.subtotal)}</div> @@ -587,6 +919,42 @@ const Checkout = () => { )} </div> </div> + + <hr className='my-4 border-gray_r-6' /> + + <div className='mt-4 mb-4'> + <button + type='button' + onClick={() => SetBottomPopup(true)} + className='text-gray-900 p-3 flex items-center justify-between rounded-lg bg-white border font-medium border-gray-300 hover:bg-gray-100 py-2.5 h-[50px] w-[100%]' + > + <div className='flex items-center justify-between gap-x-3'> + <span className='text-left text-gray_r-9'> + <Image + src={'/images/DISKONICON.svg'} + alt={''} + className='object-contain object-center h-6 w-full rounded-md' + /> + </span> + {activeVoucher ? ( + <div className=''> + <div className='text-left text-sm text-black font-semibold'> + Hemat {currencyFormat(discountVoucher)} + </div> + <div className='text-left mt-1 text-green-600 text-xs'> + Voucher berhasil digunakan + </div> + </div> + ) : ( + <span className='text-left text-sm text-gray_r-9'> + Hemat belanja dengan promo + </span> + )} + </div> + <span className='text-left ml-1 text-gray_r-9'>{'>'}</span> + </button> + </div> + <p className='text-caption-2 text-gray_r-11 leading-5'> Dengan melakukan pembelian melalui website Indoteknik, saya menyetujui{' '} <Link href='/syarat-ketentuan' className='inline font-normal'> @@ -623,14 +991,7 @@ const Checkout = () => { <button className='w-full btn-yellow mt-4' onClick={checkout} - disabled={ - isLoading || - !products || - products?.length == 0 || - priceCheck || - selectedCarrier == 0 || - (selectedCarrier != 1 && biayaKirim == 0) - } + disabled={isLoading || !products || products?.length == 0 || priceCheck} > {isLoading ? 'Loading...' : 'Lanjut Pembayaran'} </button> @@ -693,13 +1054,24 @@ const SectionValidation = ({ address }) => </BottomPopup> ) -const SectionExpedisi = ({ address, listExpedisi, setSelectedExpedisi, checkWeigth }) => +const SectionExpedisi = ({ + address, + listExpedisi, + setSelectedExpedisi, + checkWeigth, + checkoutValidation, + expedisiValidation +}) => address?.rajaongkirCityId > 0 && ( - <div className='p-4'> + <div className='p-4' ref={expedisiValidation}> <div className='flex justify-between items-center'> <div className='font-medium'>Pilih Expedisi : </div> - <div> - <select className='form-input' onChange={(e) => setSelectedExpedisi(e.target.value)}> + <div className='w-[250px]'> + <select + className={`form-input ${checkoutValidation ? 'border-red-500 shake' : ''}`} + onChange={(e) => setSelectedExpedisi(e.target.value)} + required + > <option value='0,0'>Pengiriman</option> <option value='1,32'>SELF PICKUP</option> {checkWeigth != true && @@ -714,7 +1086,15 @@ const SectionExpedisi = ({ address, listExpedisi, setSelectedExpedisi, checkWeig </option> ))} </select> + {checkoutValidation && ( + <span className='text-sm text-red-500'>* Silahkan pilih expedisi</span> + )} </div> + <style jsx>{` + .shake { + animation: shake 0.4s ease-in-out; + } + `}</style> </div> {checkWeigth == true && ( <p className='mt-4 text-gray_r-11 leading-6'> diff --git a/src/lib/home/components/CategoryHomeId.jsx b/src/lib/home/components/CategoryHomeId.jsx index 3707063c..71428e27 100644 --- a/src/lib/home/components/CategoryHomeId.jsx +++ b/src/lib/home/components/CategoryHomeId.jsx @@ -7,7 +7,7 @@ const CategoryHomeId = () => { return ( <div> - <div className='font-medium sm:text-h-lg mb-6 px-4 sm:px-0'>Kategori Pilihan</div> + <div className='font-semibold sm:text-h-lg mb-6 px-4 sm:px-0'>Kategori Pilihan</div> <div className='flex flex-col gap-y-10'> {categoryHomeIds.data?.map((id) => ( <LazyLoadComponent key={id}> diff --git a/src/lib/home/components/PreferredBrand.jsx b/src/lib/home/components/PreferredBrand.jsx index 34c50220..55abe0b7 100644 --- a/src/lib/home/components/PreferredBrand.jsx +++ b/src/lib/home/components/PreferredBrand.jsx @@ -12,9 +12,9 @@ const PreferredBrand = () => { return ( <div className='px-4 sm:px-0'> <div className='flex justify-between items-center mb-4'> - <div className='font-medium sm:text-h-lg'>Brand Pilihan</div> + <div className='font-semibold sm:text-h-lg'>Brand Pilihan</div> {isDesktop && ( - <Link href='/shop/brands' className='btn-yellow !text-gray_r-12'> + <Link href='/shop/brands' className='!text-red-500 font-semibold'> Lihat Semua </Link> )} 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 9649fd21..0547c36e 100644 --- a/src/lib/product/components/Product/Product.jsx +++ b/src/lib/product/components/Product/Product.jsx @@ -5,8 +5,12 @@ import ProductDesktop from './ProductDesktop' import useAuth from '@/core/hooks/useAuth' 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 }) @@ -26,12 +30,35 @@ const Product = ({ product }) => { wishlist.refetch() } - return ( - <> - <ProductMobile product={product} wishlist={wishlist} toggleWishlist={toggleWishlist} /> - <ProductDesktop products={product} wishlist={wishlist} toggleWishlist={toggleWishlist} /> - </> - ) + useEffect(() => { + if (isVariant == false) { + gtagViewItem(product.variants) + } + }, [product, isVariant]) + + 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/ProductDesktop.jsx b/src/lib/product/components/Product/ProductDesktop.jsx index e15ea252..4e746877 100644 --- a/src/lib/product/components/Product/ProductDesktop.jsx +++ b/src/lib/product/components/Product/ProductDesktop.jsx @@ -15,9 +15,13 @@ import BottomPopup from '@/core/components/elements/Popup/BottomPopup' import ProductCard from '../ProductCard' import productSimilarApi from '../../api/productSimilarApi' import whatsappUrl from '@/core/utils/whatsappUrl' +<<<<<<< HEAD import PromotionType from '@/lib/promotinProgram/components/PromotionType' import useAuth from '@/core/hooks/useAuth' import odooApi from '@/core/api/odooApi' +======= +import { gtagAddToCart } from '@/core/utils/googleTag' +>>>>>>> Feature/voucher const ProductDesktop = ({ products, wishlist, toggleWishlist }) => { const router = useRouter() @@ -96,10 +100,10 @@ const ProductDesktop = ({ products, wishlist, toggleWishlist }) => { setAddCartAlert(true) } - const handleBuy = (variantId) => { - const quantity = variantQuantityRefs.current[variantId].value + const handleBuy = (variant) => { + const quantity = variantQuantityRefs.current[variant].value if (!validQuantity(quantity)) return - router.push(`/shop/checkout?productId=${variantId}&quantity=${quantity}`) + router.push(`/shop/checkout?productId=${variant}&quantity=${quantity}`) } const variantSectionRef = useRef(null) @@ -299,7 +303,7 @@ const ProductDesktop = ({ products, wishlist, toggleWishlist }) => { /> <button type='button' - onClick={() => handleAddToCart(product.variants[0].id)} + onClick={() => handleAddToCart(product.variants[0])} className='flex-1 py-2 btn-yellow' > Keranjang @@ -428,7 +432,7 @@ const ProductDesktop = ({ products, wishlist, toggleWishlist }) => { )} <button type='button' - onClick={() => handleAddToCart(variant.id)} + onClick={() => handleAddToCart(variant)} className='flex-1 py-2 btn-yellow' > Keranjang 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/ProductMobile.jsx b/src/lib/product/components/Product/ProductMobile.jsx index f3afa938..fa1e2521 100644 --- a/src/lib/product/components/Product/ProductMobile.jsx +++ b/src/lib/product/components/Product/ProductMobile.jsx @@ -15,6 +15,7 @@ import { createSlug } from '@/core/utils/slug' import BottomPopup from '@/core/components/elements/Popup/BottomPopup' import whatsappUrl from '@/core/utils/whatsappUrl' import PromotionType from '@/lib/promotinProgram/components/PromotionType' +import { gtagAddToCart } from '@/core/utils/googleTag' const ProductMobile = ({ product, wishlist, toggleWishlist }) => { const router = useRouter() @@ -91,6 +92,7 @@ const ProductMobile = ({ product, wishlist, toggleWishlist }) => { const handleClickCart = () => { if (!validAction()) return + gtagAddToCart(activeVariant, quantity) updateItemCart({ productId: activeVariant.id, quantity, diff --git a/src/lib/product/components/Product/ProductMobileVariant.jsx b/src/lib/product/components/Product/ProductMobileVariant.jsx new file mode 100644 index 00000000..958b00cc --- /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 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/lib/review/components/CustomerReviews.jsx b/src/lib/review/components/CustomerReviews.jsx index 5cc179e9..7cad52fb 100644 --- a/src/lib/review/components/CustomerReviews.jsx +++ b/src/lib/review/components/CustomerReviews.jsx @@ -12,7 +12,7 @@ const CustomerReviews = () => { return ( <div className='px-4 sm:px-0'> - <div className='font-medium sm:text-h-lg mb-4'>Ulasan Konsumen Kami</div> + <div className='font-semibold sm:text-h-lg mb-4'>Ulasan Konsumen Kami</div> <DesktopView> <Swiper slidesPerView={3.2} spaceBetween={16} {...swiperProps}> diff --git a/src/pages/_document.jsx b/src/pages/_document.jsx index 2130c097..5f3d2367 100644 --- a/src/pages/_document.jsx +++ b/src/pages/_document.jsx @@ -72,6 +72,25 @@ export default function MyDocument() { }} /> + <Script + async + strategy='beforeInteractive' + src='https://www.googletagmanager.com/gtag/js?id=AW-954540379' + /> + + <Script + id='google-ads' + strategy='afterInteractive' + dangerouslySetInnerHTML={{ + __html: ` + window.dataLayer = window.dataLayer || []; + function gtag(){dataLayer.push(arguments);} + gtag('js', new Date()); + + gtag('config', 'AW-954540379');` + }} + /> + {/* <Script id='tawk-script-tag' strategy='afterInteractive' 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/api/user-activity.js b/src/pages/api/user-activity.js new file mode 100644 index 00000000..04daa9a6 --- /dev/null +++ b/src/pages/api/user-activity.js @@ -0,0 +1,16 @@ +import odooApi from "@/core/api/odooApi"; + +export default async function handler(req, res) { + res.setHeader('Cache-Control', 'no-cache'); + const data = { + 'page_title' : req.query.page_title, + 'url' : req.query.url, + 'ip_address' : req.query.ip, + } + + const sendData = await odooApi('POST', '/api/v1/activity', data) + + // Kirim respons ke klien + res.status(200).json({ message: 'OK' }); + } +
\ No newline at end of file diff --git a/src/pages/google_merchant/products/[page].js b/src/pages/google_merchant/products/[page].js index aaa4dde4..52b87389 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,11 +19,11 @@ 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) => { - const productUrl = createSlug('/shop/product/', product.name, product.id, true) + const productUrl = createSlug('/shop/product/variant/', product.name, product.id, true) const productId = product.code != '' ? product.code : product.id const regexHtmlTags = /(<([^>]+)>)/gi product.description = product.description?.replace(regexHtmlTags, ' ').trim() @@ -33,6 +34,8 @@ export async function getServerSideProps({ res, query }) { product.description = defaultProductDescription } + const availability = 'in_stock' + const item = { 'g:id': { '#text': productId }, 'g:title': { '#text': toTitleCase(product.name) }, @@ -40,12 +43,14 @@ export async function getServerSideProps({ res, query }) { 'g:link': { '#text': productUrl }, 'g:image_link': { '#text': product.image }, 'g:condition': { '#text': 'new' }, - 'g:availability': { '#text': 'in_stock' }, + 'g:availability': { '#text': availability }, 'g:brand': { '#text': product.manufacture?.name || '' }, - 'g:price': { '#text': `${product.lowestPrice.price} IDR` } + 'g:price': { '#text': `${Math.floor(product.lowestPrice.price * 1.11)} IDR` } } if (product.lowestPrice.discountPercentage > 0) { - item['g:sale_price'] = { '#text': `${product.lowestPrice.priceDiscount} IDR` } + item['g:sale_price'] = { + '#text': `${Math.floor(product.lowestPrice.priceDiscount * 1.11)} IDR` + } } productItems.push(item) }) 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/index.jsx b/src/pages/index.jsx index cd421987..12d2ab46 100644 --- a/src/pages/index.jsx +++ b/src/pages/index.jsx @@ -32,7 +32,7 @@ export default function Home() { const handleOnLoad = () => { wrapperRef.current.style.height = - bannerRef.current.querySelector(':first-child').clientHeight + 'px' + bannerRef.current?.querySelector(':first-child')?.clientHeight + 'px' } return ( 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> + ) +} diff --git a/src/styles/globals.css b/src/styles/globals.css index 7d571e3f..af9f9bea 100644 --- a/src/styles/globals.css +++ b/src/styles/globals.css @@ -461,7 +461,9 @@ button { } .table-data { - @apply w-full; + @apply w-full + table-auto + border-collapse;; } .table-data thead tr { @@ -480,7 +482,7 @@ button { } .table-data tbody td { - @apply text-gray_r-12/90 first:whitespace-nowrap; + @apply text-gray_r-12/90; } .table-data tbody tr { @@ -628,3 +630,12 @@ button { animation-timing-function: ease-in-out; animation-iteration-count: infinite; } + +@keyframes shake { + 0% { transform: translateX(0); } + 10%, 90% { transform: translateX(-10px); } + 20%, 80% { transform: translateX(10px); } + 30%, 50%, 70% { transform: translateX(-10px); } + 40%, 60% { transform: translateX(10px); } + 100% { transform: translateX(0); } + } |
