summaryrefslogtreecommitdiff
path: root/src/pages
diff options
context:
space:
mode:
authortrisusilo <tri.susilo@altama.co.id>2023-09-22 02:05:29 +0000
committertrisusilo <tri.susilo@altama.co.id>2023-09-22 02:05:29 +0000
commit23b667695991fafeae523aff1de7df81770461cd (patch)
tree6ce4958f4000e3db72ceddebe7ffb468eefe395b /src/pages
parent6ac18fd7baaf617f12f8fd6edde8a4881c547330 (diff)
parentbda91439b6ef4605a579bde8bef603b551aab3dd (diff)
Merged in Feature/popup_cart (pull request #72)
Feature/popup cart
Diffstat (limited to 'src/pages')
-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
4 files changed, 97 insertions, 39 deletions
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 />