summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/contexts/ProductCartContext.js21
-rw-r--r--src/contexts/ProductContext.js15
-rw-r--r--src/core/components/elements/Navbar/NavbarDesktop.jsx49
-rw-r--r--src/core/components/layouts/BasicLayout.jsx34
-rw-r--r--src/core/utils/whatsappUrl.js5
-rw-r--r--src/lib/cart/components/Cart.jsx4
-rw-r--r--src/lib/cart/components/Cartheader.jsx278
-rw-r--r--src/lib/home/api/categoryHomeApi.js7
-rw-r--r--src/lib/home/components/CategoryHome.jsx10
-rw-r--r--src/lib/product/api/productSimilarApi.js12
-rw-r--r--src/lib/product/components/Product/ProductDesktop.jsx4
-rw-r--r--src/pages/_app.jsx78
-rw-r--r--src/pages/api/shop/product-detail.js2
-rw-r--r--src/pages/api/shop/product-homepage.js43
-rw-r--r--src/pages/shop/product/[slug].jsx13
15 files changed, 480 insertions, 95 deletions
diff --git a/src/contexts/ProductCartContext.js b/src/contexts/ProductCartContext.js
new file mode 100644
index 00000000..06e97563
--- /dev/null
+++ b/src/contexts/ProductCartContext.js
@@ -0,0 +1,21 @@
+import { createContext, useCallback, useContext, useEffect, useState } from 'react'
+
+const ProductCartContext = createContext()
+
+export const ProductCartProvider = ({ children }) => {
+ const [productCart, setProductCart] = useState(null)
+ const [refreshCart, setRefreshCart] = useState(false)
+ const [isLoading, setIsloading] = useState(false)
+
+ return (
+ <ProductCartContext.Provider
+ value={{ productCart, setProductCart, refreshCart, setRefreshCart, isLoading, setIsloading }}
+ >
+ {children}
+ </ProductCartContext.Provider>
+ )
+}
+
+export const useProductCartContext = () => {
+ return useContext(ProductCartContext)
+}
diff --git a/src/contexts/ProductContext.js b/src/contexts/ProductContext.js
new file mode 100644
index 00000000..7bf65989
--- /dev/null
+++ b/src/contexts/ProductContext.js
@@ -0,0 +1,15 @@
+import { createContext, useContext, useState } from 'react'
+
+const ProductContext = createContext()
+
+export const ProductProvider = ({ children }) => {
+ const [product, setProduct] = useState(null)
+
+ return (
+ <ProductContext.Provider value={{ product, setProduct }}>{children}</ProductContext.Provider>
+ )
+}
+
+export const useProductContext = () => {
+ return useContext(ProductContext)
+}
diff --git a/src/core/components/elements/Navbar/NavbarDesktop.jsx b/src/core/components/elements/Navbar/NavbarDesktop.jsx
index 655c4732..3aba55c9 100644
--- a/src/core/components/elements/Navbar/NavbarDesktop.jsx
+++ b/src/core/components/elements/Navbar/NavbarDesktop.jsx
@@ -10,18 +10,17 @@ import DesktopView from '../../views/DesktopView'
import dynamic from 'next/dynamic'
import IndoteknikLogo from '@/images/logo.png'
import Category from '@/lib/category/components/Category'
-import { useContext, useEffect, useState } from 'react'
+import { useCallback, useContext, useEffect, useState } from 'react'
import useAuth from '@/core/hooks/useAuth'
import NavbarUserDropdown from './NavbarUserDropdown'
-import { getCountCart } from '@/core/utils/cart'
+import { getCartApi, getCountCart } from '@/core/utils/cart'
import whatsappUrl from '@/core/utils/whatsappUrl'
import { useRouter } from 'next/router'
import { getAuth, setAuth } from '@/core/utils/auth'
import { createSlug, getIdFromSlug } from '@/core/utils/slug'
-import productApi from '@/lib/product/api/productApi'
-import { useSession } from 'next-auth/react'
-import { AuthContext } from '@/pages/_app'
import { TopBannerSkeleton } from '../Skeleton/TopBannerSkeleton'
+import { useProductContext } from '@/contexts/ProductContext'
+import Cardheader from '@/lib/cart/components/Cartheader'
const Search = dynamic(() => import('./Search'))
const TopBanner = dynamic(() => import('./TopBanner'), {
@@ -30,7 +29,6 @@ const TopBanner = dynamic(() => import('./TopBanner'), {
const NavbarDesktop = () => {
const [isOpenCategory, setIsOpenCategory] = useState(false)
- const {authenticated} = useContext(AuthContext)
const auth = useAuth()
const [cartCount, setCartCount] = useState(0)
@@ -40,8 +38,23 @@ const NavbarDesktop = () => {
const [urlPath, setUrlPath] = useState(null)
const router = useRouter()
+
+ const { product } = useProductContext()
useEffect(() => {
+ if (router.pathname === '/shop/product/[slug]') {
+ setPayloadWa({
+ name: product?.name,
+ manufacture: product?.manufacture.name,
+ url: createSlug('/shop/product/', product?.name, product?.id, true)
+ })
+ setTemplateWA('product')
+
+ setUrlPath(router.asPath)
+ }
+ }, [product, router])
+
+ useEffect(() => {
const handleCartChange = () => {
const cart = async () => {
const listCart = await getCountCart()
@@ -52,23 +65,6 @@ const NavbarDesktop = () => {
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')
-
- setUrlPath(router.asPath)
- }
return () => {
window.removeEventListener('localStorageChange', handleCartChange)
@@ -102,7 +98,7 @@ const NavbarDesktop = () => {
<div className='flex-1 flex items-center'>
<Search />
</div>
- <div className='flex gap-x-4'>
+ <div className='flex gap-x-4 items-center'>
<Link
href='/my/transactions'
target='_blank'
@@ -114,7 +110,8 @@ const NavbarDesktop = () => {
<br />
Quotation
</Link>
- <Link
+ <Cardheader cartCount={cartCount}/>
+ {/* <Link
href='/shop/cart'
target='_blank'
rel='noreferrer'
@@ -133,7 +130,7 @@ const NavbarDesktop = () => {
<br />
Belanja
</span>
- </Link>
+ </Link> */}
<Link
target='_blank'
rel='noreferrer'
diff --git a/src/core/components/layouts/BasicLayout.jsx b/src/core/components/layouts/BasicLayout.jsx
index 266223d8..e6bd4de0 100644
--- a/src/core/components/layouts/BasicLayout.jsx
+++ b/src/core/components/layouts/BasicLayout.jsx
@@ -11,6 +11,7 @@ import { getAuth, setAuth } from '@/core/utils/auth'
import { createSlug, getIdFromSlug } from '@/core/utils/slug'
import { useSession } from 'next-auth/react'
import { setCookie } from 'cookies-next'
+import { useProductContext } from '@/contexts/ProductContext'
const Navbar = dynamic(() => import('../elements/Navbar/Navbar'))
const AnimationLayout = dynamic(() => import('./AnimationLayout'))
@@ -22,6 +23,22 @@ const BasicLayout = ({ children }) => {
const router = useRouter()
+ const { product } = useProductContext()
+
+ useEffect(() => {
+ if (router.pathname === '/shop/product/[slug]') {
+ setPayloadWa({
+ name: product?.name,
+ manufacture: product?.manufacture.name,
+ url: createSlug('/shop/product/', product?.name, product?.id, true)
+ })
+ setTemplateWA('product')
+
+ setUrlPath(router.asPath)
+ }
+ }, [product, router])
+
+
useEffect(() => {
const getIP = async () => {
const ip = await odooApi('GET', '/api/ip-address')
@@ -33,23 +50,6 @@ const BasicLayout = ({ children }) => {
axios.get(`/api/user-activity?page_title=${data.page_title}&url=${data.url}&ip=${data.ip}`)
}
getIP()
- 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')
-
- setUrlPath(router.asPath)
- }
}, [])
return (
<>
diff --git a/src/core/utils/whatsappUrl.js b/src/core/utils/whatsappUrl.js
index 6c354924..9a92f424 100644
--- a/src/core/utils/whatsappUrl.js
+++ b/src/core/utils/whatsappUrl.js
@@ -1,9 +1,10 @@
import { getAuth } from "./auth"
-const whatsappUrl = (template = 'default', payload, urlPath = '') => {
+const whatsappUrl = (template = 'default', payload, urlPath = null) => {
let user = getAuth()
if(!user){
- return `/login?next=${urlPath}`
+ if(urlPath) return `/login?next=${urlPath}`
+ if(!urlPath) return '/login'
}
let parentName = user.parentName || '-'
let url = 'https://wa.me/628128080622'
diff --git a/src/lib/cart/components/Cart.jsx b/src/lib/cart/components/Cart.jsx
index efbcf76b..b5976a1b 100644
--- a/src/lib/cart/components/Cart.jsx
+++ b/src/lib/cart/components/Cart.jsx
@@ -22,6 +22,7 @@ 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'
+import { useProductCartContext } from '@/contexts/ProductCartContext'
const Cart = () => {
const router = useRouter()
@@ -31,6 +32,8 @@ const Cart = () => {
const [cart, setCart] = useState(null)
+ const {setRefreshCart} = useProductCartContext()
+
useEffect(() => {
if (!auth) return
}, [auth])
@@ -196,6 +199,7 @@ const Cart = () => {
deleteItemCart({ productId })
setDeleteConfirmation(null)
setProducts([...productsToUpdate])
+ setRefreshCart(true)
toast.success('Berhasil menghapus barang dari keranjang')
}
diff --git a/src/lib/cart/components/Cartheader.jsx b/src/lib/cart/components/Cartheader.jsx
new file mode 100644
index 00000000..dd6c276e
--- /dev/null
+++ b/src/lib/cart/components/Cartheader.jsx
@@ -0,0 +1,278 @@
+import { useCallback, useEffect, useMemo, useState } from 'react'
+import { getCartApi } from '../api/CartApi'
+import currencyFormat from '@/core/utils/currencyFormat'
+import Image from '@/core/components/elements/Image/Image'
+import { createSlug } from '@/core/utils/slug'
+import useAuth from '@/core/hooks/useAuth'
+import { useRouter } from 'next/router'
+import odooApi from '@/core/api/odooApi'
+import { useProductCartContext } from '@/contexts/ProductCartContext'
+import whatsappUrl from '@/core/utils/whatsappUrl'
+
+const { ShoppingCartIcon, PhotoIcon } = require('@heroicons/react/24/outline')
+const { default: Link } = require('next/link')
+
+const Cardheader = (cartCount) => {
+ const router = useRouter()
+ const [subTotal, setSubTotal] = useState(null)
+ const [buttonLoading, SetButtonTerapkan] = useState(false)
+ const itemLoading = [1, 2, 3]
+ const auth = useAuth()
+ const [countCart, setCountCart] = useState(null)
+ const { productCart, setRefreshCart, setProductCart, refreshCart, isLoading, setIsloading } =
+ useProductCartContext()
+
+ const [isHovered, setIsHovered] = useState(false)
+
+ const products = useMemo(() => {
+ return productCart?.products || []
+ }, [productCart])
+
+ const handleMouseEnter = () => {
+ setIsHovered(true)
+ getCart()
+ }
+
+ const handleMouseLeave = () => {
+ setIsHovered(false)
+ }
+
+ const getCart = () => {
+ if (!productCart && auth) {
+ refreshCartf()
+ }
+ }
+ const refreshCartf = useCallback(async () => {
+ setIsloading(true)
+ let cart = await getCartApi()
+ setProductCart(cart)
+ setCountCart(cart.productTotal)
+ setIsloading(false)
+ }, [setProductCart])
+
+ useEffect(() => {
+ if (!products) return
+
+ let calculateTotalPriceBeforeTax = 0
+ let calculateTotalTaxAmount = 0
+ let calculateTotalDiscountAmount = 0
+ for (const product of products) {
+ if (product.quantity == '') continue
+
+ let priceBeforeTax = product.price.price / 1.11
+ calculateTotalPriceBeforeTax += priceBeforeTax * product.quantity
+ calculateTotalTaxAmount += (product.price.price - priceBeforeTax) * product.quantity
+ calculateTotalDiscountAmount +=
+ (product.price.price - product.price.priceDiscount) * product.quantity
+ }
+ let subTotal =
+ calculateTotalPriceBeforeTax - calculateTotalDiscountAmount + calculateTotalTaxAmount
+ setSubTotal(subTotal)
+ }, [products])
+
+ useEffect(() => {
+ if (refreshCart) {
+ refreshCartf()
+ }
+ setRefreshCart(false)
+ }, [refreshCart, refreshCartf, setRefreshCart])
+
+ useEffect(() => {
+ setCountCart(cartCount.cartCount)
+ }, [cartCount])
+
+ const handleCheckout = async () => {
+ SetButtonTerapkan(true)
+ let checkoutAll = await odooApi('POST', `/api/v1/user/${auth.id}/cart/select-all`)
+ router.push('/shop/checkout')
+ }
+
+ return (
+ <div className='relative group'>
+ <div>
+ <Link
+ href='/shop/cart'
+ target='_blank'
+ rel='noreferrer'
+ className='flex items-center gap-x-2 !text-gray_r-12/80'
+ onMouseEnter={handleMouseEnter}
+ onMouseLeave={handleMouseLeave}
+ >
+ <div className={`relative ${countCart > 0 && 'mr-2'}`}>
+ <ShoppingCartIcon className='w-7' />
+ {countCart > 0 && (
+ <span className='absolute -top-2 -right-2 badge-solid-red rounded-full w-5 h-5 flex items-center justify-center'>
+ {countCart}
+ </span>
+ )}
+ </div>
+ <span>
+ Keranjang
+ <br />
+ Belanja
+ </span>
+ </Link>
+ </div>
+ <div
+ className={` ${
+ isHovered ? 'block' : 'hidden'
+ } fixed top-[155px] left-0 w-full h-full bg-black opacity-50 z-10`}
+ ></div>
+ <div
+ className='hidden group-hover:block absolute z-10 left-0 w-96'
+ onMouseEnter={handleMouseEnter}
+ onMouseLeave={handleMouseLeave}
+ >
+ <div className='w-full max-w-md p-2 bg-white border border-gray-200 rounded-lg shadow'>
+ <div className='p-2 flex justify-between items-center'>
+ <h5 class='text-base font-semibold leading-none'>Keranjang Belanja</h5>
+ <Link href='/shop/cart' class='text-sm font-medium text-red-600 underline'>
+ Lihat Semua
+ </Link>
+ </div>
+ <hr className='mt-3 mb-3 border border-gray-100' />
+ <div className='flow-root max-h-[250px] overflow-y-auto'>
+ {!auth && (
+ <div className='justify-center p-4'>
+ <p className='text-gray-500 text-center '>
+ Silahkan{' '}
+ <Link href='/login' className='text-red-600 underline leading-6'>
+ Login
+ </Link>{' '}
+ Untuk Melihat Daftar Keranjang Belanja Anda
+ </p>
+ </div>
+ )}
+ {isLoading &&
+ itemLoading.map((item) => (
+ <div key={item} role='status' class='max-w-sm animate-pulse'>
+ <div class='flex items-center space-x-4 mb- 2'>
+ <div class='flex-shrink-0'>
+ <PhotoIcon class='h-16 w-16 text-gray-500' />
+ </div>
+ <div class='flex-1 min-w-0'>
+ <div class='h-2.5 bg-gray-200 rounded-full dark:bg-gray-700 w-48 mb-4'></div>
+ <div class='h-2 bg-gray-200 rounded-full dark:bg-gray-700 max-w-[360px] mb-2.5'></div>
+ <div class='h-2 bg-gray-200 rounded-full dark:bg-gray-700 mb-2.5'></div>
+ </div>
+ </div>
+ </div>
+ ))}
+ {products.length === 0 && !isLoading && (
+ <div className='justify-center p-4'>
+ <p className='text-gray-500 text-center '>
+ Tidak Ada Produk di Keranjang Belanja Anda
+ </p>
+ </div>
+ )}
+ {products.length > 0 && !isLoading && (
+ <>
+ <ul role='list' class='divide-y divide-gray-200 dark:divide-gray-700'>
+ {products &&
+ products?.map((product, index) => (
+ <>
+ <li class='py-1 sm:py-2'>
+ <div class='flex items-center space-x-4'>
+ <div class='flex-shrink-0'>
+ <Link
+ href={createSlug(
+ '/shop/product/',
+ product?.parent.name,
+ product?.parent.id
+ )}
+ className='line-clamp-2 leading-6 !text-gray_r-12 font-normal'
+ >
+ <Image
+ src={product?.parent?.image}
+ alt={product?.name}
+ className='object-contain object-center border border-gray_r-6 h-16 w-16 rounded-md'
+ />
+ </Link>
+ </div>
+ <div class='flex-1 min-w-0'>
+ <Link
+ href={createSlug(
+ '/shop/product/',
+ product?.parent.name,
+ product?.parent.id
+ )}
+ className='line-clamp-2 leading-6 !text-gray_r-12 font-normal'
+ >
+ {' '}
+ <p class='text-caption-2 font-medium text-gray-900 truncate dark:text-white'>
+ {product.parent.name}
+ </p>
+ </Link>
+
+ {product?.price?.discountPercentage > 0 && (
+ <div className='flex gap-x-1 items-center mb-2 mt-1'>
+ <div className='badge-solid-red'>
+ {product?.price?.discountPercentage}%
+ </div>
+ <div className='text-gray_r-11 line-through text-caption-2'>
+ {currencyFormat(product?.price?.price)}
+ </div>
+ </div>
+ )}
+ <div className='flex justify-between items-center'>
+ <div className='font-semibold text-sm text-red-600'>
+ {product?.price?.priceDiscount > 0 ? (
+ currencyFormat(product?.price?.priceDiscount)
+ ) : (
+ <span className='text-gray_r-12/90 font-normal text-caption-1'>
+ <a
+ href={whatsappUrl('product', {
+ name: product.name,
+ manufacture: product.manufacture?.name,
+ url: createSlug(
+ '/shop/product/',
+ product.name,
+ product.id,
+ true
+ )
+ })}
+ className='text-danger-500 underline'
+ rel='noopener noreferrer'
+ target='_blank'
+ >
+ Call For Price
+ </a>
+ </span>
+ )}
+ </div>
+ </div>
+ </div>
+ </div>
+ </li>
+ </>
+ ))}
+ </ul>
+ <hr />
+ </>
+ )}
+ </div>
+ {products.length > 0 && !isLoading && (
+ <>
+ <div className='mt-3'>
+ <span className='text-gray-400 text-caption-2'>Sub Total Sebelum PPN : </span>
+ <span className='font-semibold text-red-600'>Rp. {currencyFormat(subTotal)}</span>
+ </div>
+ <div className='mt-5 mb-2'>
+ <button
+ type='button'
+ className='btn-solid-red rounded-lg w-full'
+ onClick={handleCheckout}
+ disabled={buttonLoading}
+ >
+ {buttonLoading ? 'Loading...' : 'Lanjutkan Ke Pembayaran'}
+ </button>
+ </div>
+ </>
+ )}
+ </div>
+ </div>
+ </div>
+ )
+}
+
+export default Cardheader
diff --git a/src/lib/home/api/categoryHomeApi.js b/src/lib/home/api/categoryHomeApi.js
index 44b77daa..9e7d1402 100644
--- a/src/lib/home/api/categoryHomeApi.js
+++ b/src/lib/home/api/categoryHomeApi.js
@@ -1,8 +1,11 @@
import odooApi from '@/core/api/odooApi'
+import axios from 'axios'
const categoryHomeIdApi = async ({ id }) => {
- const dataCategoryHomeId = await odooApi('GET', `/api/v1/product/category-homepage?id=${id}`)
- return dataCategoryHomeId
+ // const dataCategoryHomeIdO = await odooApi('GET', `/api/v1/product/category-homepage?id=${id}`)
+ // console.log('ini adalah odoo', dataCategoryHomeIdO)
+ const dataCategoryHomeId = await axios(`${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/product-homepage?id=` + id)
+ return dataCategoryHomeId.data
}
export default categoryHomeIdApi
diff --git a/src/lib/home/components/CategoryHome.jsx b/src/lib/home/components/CategoryHome.jsx
index 34c28728..9e992640 100644
--- a/src/lib/home/components/CategoryHome.jsx
+++ b/src/lib/home/components/CategoryHome.jsx
@@ -7,14 +7,14 @@ const CategoryHome = ({ id }) => {
return (
<div className='p-4 sm:py-8 relative bg-gray-100 border border-gray-200 min-h-[240px]'>
- {categoryHome?.data?.length > 0 ? (
+ {categoryHome.data ? (
<ProductSlider
products={{
- products: categoryHome.data?.[0].products,
+ products: categoryHome?.data?.products,
banner: {
- image: categoryHome.data?.[0].image,
- name: categoryHome.data?.[0].name,
- url: categoryHome.data?.[0].url
+ image: categoryHome?.data?.image,
+ name: categoryHome?.data?.name,
+ url: categoryHome?.data?.url
}
}}
simpleTitle
diff --git a/src/lib/product/api/productSimilarApi.js b/src/lib/product/api/productSimilarApi.js
index c1bccd59..a008ce5d 100644
--- a/src/lib/product/api/productSimilarApi.js
+++ b/src/lib/product/api/productSimilarApi.js
@@ -21,10 +21,14 @@ const productSimilarApi = async ({ query, source }) => {
const dataProductSimilar = await axios(
`${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/search?q=${query}&page=1&orderBy=popular-weekly&operation=OR`
)
- dataProductSimilar.data.response.products = [
- ...dataflashSale,
- ...dataProductSimilar.data.response.products,
- ];
+ if (dataflashSale) {
+ dataProductSimilar.data.response.products = [
+ ...dataflashSale,
+ ...dataProductSimilar.data.response.products
+ ]
+ } else {
+ dataProductSimilar.data.response.products = [...dataProductSimilar.data.response.products]
+ }
return dataProductSimilar.data.response
}
diff --git a/src/lib/product/components/Product/ProductDesktop.jsx b/src/lib/product/components/Product/ProductDesktop.jsx
index 937f2746..6da289bc 100644
--- a/src/lib/product/components/Product/ProductDesktop.jsx
+++ b/src/lib/product/components/Product/ProductDesktop.jsx
@@ -22,6 +22,7 @@ import ImageNext from 'next/image'
import CountDown2 from '@/core/components/elements/CountDown/CountDown2'
import { LazyLoadComponent } from 'react-lazy-load-image-component'
import ColumnsSLA from './ColumnsSLA'
+import { useProductCartContext } from '@/contexts/ProductCartContext'
const ProductDesktop = ({ products, wishlist, toggleWishlist }) => {
const router = useRouter()
@@ -39,6 +40,8 @@ const ProductDesktop = ({ products, wishlist, toggleWishlist }) => {
const [selectVariantPromoActive, setSelectVariantPromoActive] = useState(null)
const [backgorundFlashSale, setBackgorundFlashSale] = useState(null)
+ const {setRefreshCart , refreshCart} = useProductCartContext()
+
const getLowestPrice = useCallback(() => {
const prices = product.variants.map((variant) => variant.price)
const lowest = prices.reduce((lowest, price) => {
@@ -116,6 +119,7 @@ const ProductDesktop = ({ products, wishlist, toggleWishlist }) => {
let source = 'cart'
updateCart(variantId, quantity, source)
+ setRefreshCart(true)
setAddCartAlert(true)
}
diff --git a/src/pages/_app.jsx b/src/pages/_app.jsx
index 7f23b94b..0062f7fc 100644
--- a/src/pages/_app.jsx
+++ b/src/pages/_app.jsx
@@ -10,12 +10,14 @@ import { createContext, useContext, useEffect, useState } from 'react'
import LogoSpinner from '@/core/components/elements/Spinner/LogoSpinner'
import { SessionProvider } from 'next-auth/react'
import { getAuth } from '@/core/utils/auth'
+import { ProductProvider } from '@/contexts/ProductContext'
+import { ProductCartProvider } from '@/contexts/ProductCartContext'
const queryClient = new QueryClient()
export const AuthContext = createContext({
- authenticated : false,
- setAuthenticated : (auth) => {}
+ authenticated: false,
+ setAuthenticated: (auth) => {}
})
function MyApp({ Component, pageProps: { session, ...pageProps } }) {
@@ -62,41 +64,45 @@ function MyApp({ Component, pageProps: { session, ...pageProps } }) {
return (
// <AuthContext.Provider value={{authenticated, setAuthenticated}}>
- <SessionProvider session={session}>
- <AnimatePresence>
- {animateLoader && (
- <motion.div
- initial={{ opacity: 0 }}
- animate={{ opacity: 1 }}
- exit={{ opacity: 0 }}
- transition={{
- duration: 0.1
- }}
- className='fixed w-screen h-screen z-[500] bg-white flex justify-center items-center'
- >
- <LogoSpinner />
- </motion.div>
- )}
- </AnimatePresence>
- <Toaster
- position='top-center'
- containerStyle={toasterStyle}
- toastOptions={{
- duration: 3000,
- className: 'border border-gray_r-8'
- }}
- />
- <NextProgress color='#F01C21' options={{ showSpinner: false }} />
- <QueryClientProvider client={queryClient}>
- <AnimatePresence
- mode='popLayout'
- initial={false}
- onExitComplete={() => window.scrollTo(0, 0)}
+ <SessionProvider session={session}>
+ <AnimatePresence>
+ {animateLoader && (
+ <motion.div
+ initial={{ opacity: 0 }}
+ animate={{ opacity: 1 }}
+ exit={{ opacity: 0 }}
+ transition={{
+ duration: 0.1
+ }}
+ className='fixed w-screen h-screen z-[500] bg-white flex justify-center items-center'
>
- {!animateLoader && <Component {...pageProps} key={router.asPath} />}
- </AnimatePresence>
- </QueryClientProvider>
- </SessionProvider>
+ <LogoSpinner />
+ </motion.div>
+ )}
+ </AnimatePresence>
+ <Toaster
+ position='top-center'
+ containerStyle={toasterStyle}
+ toastOptions={{
+ duration: 3000,
+ className: 'border border-gray_r-8'
+ }}
+ />
+ <NextProgress color='#F01C21' options={{ showSpinner: false }} />
+ <QueryClientProvider client={queryClient}>
+ <AnimatePresence
+ mode='popLayout'
+ initial={false}
+ onExitComplete={() => window.scrollTo(0, 0)}
+ >
+ <ProductProvider>
+ <ProductCartProvider>
+ {!animateLoader && <Component {...pageProps} key={router.asPath} />}
+ </ProductCartProvider>
+ </ProductProvider>
+ </AnimatePresence>
+ </QueryClientProvider>
+ </SessionProvider>
// </AuthContext.Provider>
)
}
diff --git a/src/pages/api/shop/product-detail.js b/src/pages/api/shop/product-detail.js
index 9020103b..5c3a8231 100644
--- a/src/pages/api/shop/product-detail.js
+++ b/src/pages/api/shop/product-detail.js
@@ -7,7 +7,7 @@ export default async function handler(req, res) {
)
let productVariants = await axios(
process.env.SOLR_HOST +
- `/solr/variants/select?q=template_id_i:${req.query.id}&q.op=OR&indent=true`
+ `/solr/variants/select?q=template_id_i:${req.query.id}&q.op=OR&indent=true&rows=100`
)
let { auth } = req.cookies
if (auth) auth = JSON.parse(auth)
diff --git a/src/pages/api/shop/product-homepage.js b/src/pages/api/shop/product-homepage.js
new file mode 100644
index 00000000..02c01ee0
--- /dev/null
+++ b/src/pages/api/shop/product-homepage.js
@@ -0,0 +1,43 @@
+import axios from 'axios'
+import { array } from 'yup'
+
+export default async function handler(req, res) {
+ try {
+ const products = []
+ let GetproductHomepage = await axios(
+ process.env.SOLR_HOST +
+ `/solr/product_category_homepage/select?q=id:${req.query.id}&q.op=OR&indent=true`
+ )
+ let productHomepage = GetproductHomepage.data.response.docs[0]
+ let idProducts = productHomepage.product_ids
+ await Promise.all(
+ idProducts.map(async (id) => {
+ let product = await axios(
+ `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/product-detail?id=${id}`
+ )
+ if (product && product.data.length > 0) {
+ products.push(product.data[0])
+ }
+ })
+ )
+ let result = respoonseMap(productHomepage, products)
+ res.status(200).json(result)
+ } catch (error) {
+ console.error('Error fetching data from Solr:', error)
+ res.status(500).json({ error: 'Internal Server Error' })
+ }
+}
+
+const respoonseMap = (productHomepage, products) => {
+ let productMapped ={
+ id: productHomepage.id,
+ sequence: productHomepage.sequence_i,
+ categoryId: productHomepage.category_id_i,
+ name: productHomepage.name_s,
+ image: productHomepage.image_s,
+ url: productHomepage.url_s,
+ products: products
+ }
+
+ return productMapped
+}
diff --git a/src/pages/shop/product/[slug].jsx b/src/pages/shop/product/[slug].jsx
index af20413f..534aa8da 100644
--- a/src/pages/shop/product/[slug].jsx
+++ b/src/pages/shop/product/[slug].jsx
@@ -7,6 +7,8 @@ import dynamic from 'next/dynamic'
import { useRouter } from 'next/router'
import cookie from 'cookie'
import axios from 'axios'
+import { useProductContext } from '@/contexts/ProductContext'
+import { useEffect } from 'react'
const BasicLayout = dynamic(() => import('@/core/components/layouts/BasicLayout'))
const Product = dynamic(() => import('@/lib/product/components/Product/Product'))
@@ -19,7 +21,7 @@ export async function getServerSideProps(context) {
const authToken = auth?.token || ''
let response = await axios(
- `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/product-detail?id=`+getIdFromSlug(slug)
+ `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/product-detail?id=` + getIdFromSlug(slug)
)
let product = response.data
// let productSolr = await productApi({ id: getIdFromSlug(slug), headers: { Token: authToken } })
@@ -41,8 +43,15 @@ export async function getServerSideProps(context) {
}
}
-export default function ProductDetail({ product}) {
+export default function ProductDetail({ product }) {
const router = useRouter()
+ const { setProduct } = useProductContext()
+
+ useEffect(() => {
+ if (product) {
+ setProduct(product)
+ }
+ }, [product, setProduct])
if (!product) return <PageNotFound />