diff options
| author | trisusilo <tri.susilo@altama.co.id> | 2023-07-24 08:35:11 +0000 |
|---|---|---|
| committer | trisusilo <tri.susilo@altama.co.id> | 2023-07-24 08:35:11 +0000 |
| commit | 5be5be868124e7d4b21ab2137f45006380e3d0a1 (patch) | |
| tree | d7c4151eb2bf83b85cd0532d45dedd02aa06694e /src/core/components/elements/Navbar/NavbarDesktop.jsx | |
| parent | bed971bc1265cd9b0e6f0c01dcb57bf4089c21f9 (diff) | |
| parent | 2de322571bad7490baaf439fa2bb12124c646bb5 (diff) | |
Merged in CR/widget_WA (pull request #17)
CR/widget WA
Diffstat (limited to 'src/core/components/elements/Navbar/NavbarDesktop.jsx')
| -rw-r--r-- | src/core/components/elements/Navbar/NavbarDesktop.jsx | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/src/core/components/elements/Navbar/NavbarDesktop.jsx b/src/core/components/elements/Navbar/NavbarDesktop.jsx index 26edd5a4..ea4b03ae 100644 --- a/src/core/components/elements/Navbar/NavbarDesktop.jsx +++ b/src/core/components/elements/Navbar/NavbarDesktop.jsx @@ -13,9 +13,13 @@ import Category from '@/lib/category/components/Category' import { useEffect, useState } from 'react' import useAuth from '@/core/hooks/useAuth' import NavbarUserDropdown from './NavbarUserDropdown' -import { getCart } from '@/core/utils/cart' +import { getCountCart } from '@/core/utils/cart' import TopBanner from './TopBanner' import whatsappUrl from '@/core/utils/whatsappUrl' +import { useRouter } from 'next/router' +import { getAuth } from '@/core/utils/auth' +import { createSlug, getIdFromSlug } from '@/core/utils/slug' +import productApi from '@/lib/product/api/productApi' const Search = dynamic(() => import('./Search')) @@ -25,13 +29,37 @@ const NavbarDesktop = () => { const [cartCount, setCartCount] = useState(0) + const [templateWA, setTemplateWA] = useState(null) + const [payloadWA, setPayloadWa] = useState(null) + + const router = useRouter() + useEffect(() => { const handleCartChange = () => { - setCartCount(Object.keys(getCart()).length) + const cart = async () => { + const listCart = await getCountCart() + setCartCount(listCart) + } + cart() } handleCartChange() window.addEventListener('localStorageChange', handleCartChange) + if (router.pathname === '/shop/product/[slug]') { + const authToken = getAuth().token + + const { slug } = router.query + const getProduct = async () => { + let product = await productApi({ id: getIdFromSlug(slug), headers: { Token: authToken } }) + setPayloadWa({ + name: product[0]?.name, + manufacture: product[0]?.manufacture.name, + url: createSlug('/shop/product/', product[0]?.name, product[0]?.id, true) + }) + } + getProduct() + setTemplateWA('product') + } return () => { window.removeEventListener('localStorageChange', handleCartChange) @@ -92,7 +120,7 @@ const NavbarDesktop = () => { Wishlist </Link> <a - href={whatsappUrl()} + href={whatsappUrl(templateWA, payloadWA)} target='_blank' rel='noreferrer' className='flex items-center gap-x-1 !text-gray_r-12/80' |
