diff --git a/src/components/ProductDetails/ProductDetails.jsx b/src/components/ProductDetails/ProductDetails.jsx index 4693094..6aecfb0 100644 --- a/src/components/ProductDetails/ProductDetails.jsx +++ b/src/components/ProductDetails/ProductDetails.jsx @@ -16,54 +16,52 @@ const ProductDetails = ({ productId }) => { const { currentUser } = useAuth(); - useEffect(() => { - const fetchProductData = async () => { - try { - const productRef = doc(db, "products", productId); - const productDoc = await getDoc(productRef); - - if (productDoc.exists()) { - setProductData({ ...productDoc.data(), id: productDoc.id }); - setLoading(false); - } else { - console.log("No such product document!"); - setLoading(false); - } - } catch (error) { - console.error("Error fetching product data:", error); + const fetchProductData = async () => { + try { + const productRef = doc(db, "products", productId); + const productDoc = await getDoc(productRef); + + if (productDoc.exists()) { + setProductData({ ...productDoc.data(), id: productDoc.id }); + setLoading(false); + } else { + console.log("No such product document!"); setLoading(false); } - }; - - const fetchDataAndAddToCart = async () => { - await fetchProductData(); - - // Move fetchUserData inside the fetchDataAndAddToCart function - try { - if (productData && currentUser) { - const userRef = doc( - db, - "userinfo", - productData.currentUserUid - ); - const userDoc = await getDoc(userRef); - - if (userDoc.exists()) { - setUserData(userDoc.data()); - } else { - console.log("No such user document!"); - } + } catch (error) { + console.error("Error fetching product data:", error); + setLoading(false); + } + }; + + const fetchDataAndAddToCart = async () => { + await fetchProductData(); + + // Move fetchUserData inside the fetchDataAndAddToCart function + try { + if (productData && currentUser) { + const userRef = doc(db, "userinfo", productData.currentUserUid); + const userDoc = await getDoc(userRef); + + if (userDoc.exists()) { + setUserData(userDoc.data()); + } else { + console.log("No such user document!"); + // Optionally set userData to a default value or handle accordingly } - } catch (error) { - console.error("Error fetching user data:", error); } - }; + } catch (error) { + console.error("Error fetching user data:", error); + } + }; + useEffect(() => { if (productId) { fetchDataAndAddToCart(); } }, [productId, currentUser]); + console.log(productData); const handleAddToCart = () => { // Call addItemToCart with the entire product details if (productData) { @@ -77,14 +75,14 @@ const ProductDetails = ({ productId }) => { return (
diff --git a/src/pages/singleproduct/[productId].jsx b/src/pages/singleproduct/[productId].jsx
index d52e832..31948b8 100644
--- a/src/pages/singleproduct/[productId].jsx
+++ b/src/pages/singleproduct/[productId].jsx
@@ -1,27 +1,24 @@
import ProductDetails from "@/components/ProductDetails/ProductDetails";
import { useRouter } from "next/router";
-import { doc, getDoc } from "firebase/firestore";
-import React, { useState, useEffect } from "react";
-import { db } from "@/util/firebase";
-// import { onSnapshot } from "firebase/firestore";
import { CartProvider } from "@/context/CartContext";
+// import MapComponent from "@/components/Map/Map";
function SingleProduct() {
const router = useRouter();
const { productId } = router.query;
- const [setProduct] = useState(null);
return (