From 7f628c68c2719bdb2f170846df0d38d7575b137c Mon Sep 17 00:00:00 2001 From: Katia Ghezali Date: Tue, 21 Nov 2023 19:22:30 +0100 Subject: [PATCH] feat(single product page): work on responsive details #31 --- .../ProductDetails/ProductDetails.jsx | 96 +++++++++---------- src/pages/singleproduct/[productId].jsx | 19 ++-- 2 files changed, 56 insertions(+), 59 deletions(-) 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 (
-
+
{
{/* Product Details */} -
+

{productData?.title}

@@ -160,7 +158,7 @@ const ProductDetails = ({ productId }) => {
@@ -173,22 +171,24 @@ const ProductDetails = ({ productId }) => { {/* User Info */}
- {userData && currentUser ? ( + {currentUser ? (
- {/*
+
user picture -
*/} +

- {userData?.name}{" "} - {userData?.username} + {userData?.name} {userData?.surname}

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 ( -

+
+ {/* Conditionally render MapComponent based on screen size */} - {productId && ( -
- {/* This will be visible on screens larger than or equal to lg (large) */} - {/* */} + {/* {productId && ( +
*/} + {/* This will be visible on screens larger than or equal to lg (large) */} + {/*
- )} + )} */}
);