From fafddc341f5b746d8afcc4b0f92fa972fd63e584 Mon Sep 17 00:00:00 2001 From: Blair Kayigamba Date: Wed, 17 Jul 2024 08:10:05 +0200 Subject: [PATCH] bg(bug)-fixproductview (#67) --- src/components/Header.tsx | 67 ++++++++------- src/components/ReviewsWrapper.tsx | 135 ++++++++++++++++++------------ src/components/Table.tsx | 2 +- src/components/singleproduct.tsx | 7 +- src/redux/slices/wishlistSlice.ts | 2 +- 5 files changed, 126 insertions(+), 87 deletions(-) diff --git a/src/components/Header.tsx b/src/components/Header.tsx index 5507273..62d26f4 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -35,14 +35,17 @@ import { } from '@nextui-org/react'; import { useRouter } from 'next/navigation'; const Header = () => { - const { wishNumber } = useAppSelector( - (state: RootState) => state.wishlist - ) - - const { isOrdersOverlayOpen, setIsOrdersOverlayOpen, toggleOrdersSlider } = OrdersOverlay(); + const { wishNumber } = useAppSelector((state: RootState) => state.wishlist); + + const { isOrdersOverlayOpen, setIsOrdersOverlayOpen, toggleOrdersSlider } = + OrdersOverlay(); const [activelink, setActivelink] = useState('home'); - const { isWishlistOverlayOpen, setIsWishlistOverlayOpen, toggleWishlistSlider } = WishlistOverlay(); + const { + isWishlistOverlayOpen, + setIsWishlistOverlayOpen, + toggleWishlistSlider, + } = WishlistOverlay(); const [showlModal, setShowmodal] = useState(false); const [showCart, setShowCart] = useState(false); @@ -53,16 +56,16 @@ const Header = () => { 'cart' | 'notification' | null >(null); - const handleShowWishlist=()=>{ + const handleShowWishlist = () => { setIsOrdersOverlayOpen(false); setOverlayComponent(null); toggleWishlistSlider(); - } - const handleShowOrders=()=>{ + }; + const handleShowOrders = () => { setIsWishlistOverlayOpen(false); setOverlayComponent(null); toggleOrdersSlider(); - } + }; const handleShowCart = () => { setIsWishlistOverlayOpen(false); setIsOrdersOverlayOpen(false); @@ -90,7 +93,6 @@ const Header = () => { setShowmodal(!showlModal); }; - const [viewMenu, setViewmenu] = useState(false); const [userdata, setUserdata] = useState(null); useEffect(() => { @@ -127,30 +129,35 @@ const Header = () => { {'logo'} -
+
{userdata && userdata.User.Role.name === 'buyer' && ( <> - - - {cart?.product.length} - - - - - + + + {cart?.product.length} + + + + + {wishNumber} - - + + )} {userdata ? ( <> - - + + + ) : ( '' @@ -277,7 +284,9 @@ const Header = () => { ) : ( <> dropDownShowEvent(isOpen)} + onOpenChange={(isOpen: boolean) => + dropDownShowEvent(isOpen) + } >
- + {overlayComponent && ( {overlayComponent === 'cart' && ( diff --git a/src/components/ReviewsWrapper.tsx b/src/components/ReviewsWrapper.tsx index a3330f8..739d149 100644 --- a/src/components/ReviewsWrapper.tsx +++ b/src/components/ReviewsWrapper.tsx @@ -1,5 +1,9 @@ import { ReviewType } from '@/types/Product'; -import { QueryObserverResult, RefetchOptions, useQuery } from '@tanstack/react-query'; +import { + QueryObserverResult, + RefetchOptions, + useQuery, +} from '@tanstack/react-query'; import React, { useEffect, useState } from 'react'; import ReviewCard from './ReviewCard'; import Button from './Button'; @@ -7,61 +11,84 @@ import ReviewPopup from '@/hooks/reviewPopup'; import ReviewProduct from './ReviewProductPopup'; import request from '@/utils/axios'; -interface reviewWrapper{ - refetch: (options?: RefetchOptions | undefined) => Promise>; - reviews:ReviewType[]; - productId:string; +interface reviewWrapper { + refetch: ( + options?: RefetchOptions | undefined, + ) => Promise>; + reviews: ReviewType[]; + productId: string; } -const ReviewWrapper:React.FC=({ refetch, reviews, productId })=>{ - const { isReviewPopupOpen, setIsReviewPopupOpen, toggleReviewPopup} = ReviewPopup(); - const [currentUser, setCurrentUser] = useState([]); +const ReviewWrapper: React.FC = ({ + refetch, + reviews, + productId, +}) => { + const { isReviewPopupOpen, setIsReviewPopupOpen, toggleReviewPopup } = + ReviewPopup(); + const [currentUser, setCurrentUser] = useState([]); - useEffect(()=>{ - const { User } = JSON.parse(localStorage.getItem('profile') as string); - const { id } = User - if(User){ - setCurrentUser(id) - } - }) - - const { isLoading, error, data } = useQuery({ - queryKey: ['CheckBuyerOrders'], - queryFn: () => request.get('/orders'), - }); - //setOrders(data) - var ordersData = data?.orders ?? []; - const canUserAddReview = ordersData.filter((item: { buyerId: any; productId:string; isPaid: any; }) => item.buyerId == currentUser && item.productId == productId && item.isPaid) - return( - <> -
-
-

Reviews:

- {canUserAddReview.length ? - - :"" } -
-
- {reviews && reviews.length > 0 ? ( - reviews.map((review: ReviewType) => ( - - )) - ) : ( -

No ratings yet.

- )} -
-
- {isReviewPopupOpen && - - } - - ) + useEffect(() => { + const data = JSON.parse(localStorage.getItem('profile') as string); + const id = data?.User?.id; + if (data?.User) { + setCurrentUser(id); + } + }); + + const { isLoading, error, data } = useQuery({ + queryKey: ['CheckBuyerOrders'], + queryFn: () => request.get('/orders'), + enabled: currentUser.length > 0, + }); + //setOrders(data) + var ordersData = data?.orders ?? []; + const canUserAddReview = ordersData.filter( + (item: { buyerId: any; productId: string; isPaid: any }) => + item.buyerId == currentUser && item.productId == productId && item.isPaid, + ); + return ( + <> +
+
+

Reviews:

+ {canUserAddReview.length ? ( + + ) : ( + '' + )} +
+
+ {reviews && reviews.length > 0 ? ( + reviews.map((review: ReviewType) => ( + + )) + ) : ( +

No ratings yet.

+ )} +
+
+ {isReviewPopupOpen && ( + + )} + + ); }; -export default ReviewWrapper; \ No newline at end of file +export default ReviewWrapper; + diff --git a/src/components/Table.tsx b/src/components/Table.tsx index 37be6ba..f961f43 100644 --- a/src/components/Table.tsx +++ b/src/components/Table.tsx @@ -129,7 +129,7 @@ const ProductsTable: React.FC = ({ Role }) => { {product.productName} {product.stockLevel} - {product.productPrice} {product.productCurrency} + {product.productPrice?.toLocaleString()} {product.productCurrency} {product.productDiscount} diff --git a/src/components/singleproduct.tsx b/src/components/singleproduct.tsx index 9fe370d..1cc8383 100644 --- a/src/components/singleproduct.tsx +++ b/src/components/singleproduct.tsx @@ -152,8 +152,11 @@ const Singleproduct: React.FC = ({ role }) => {
)} - - {productPrice} RWF + + + {productPrice?.toLocaleString()} + {' '} + RWF
diff --git a/src/redux/slices/wishlistSlice.ts b/src/redux/slices/wishlistSlice.ts index 9e820bd..f5963a0 100644 --- a/src/redux/slices/wishlistSlice.ts +++ b/src/redux/slices/wishlistSlice.ts @@ -9,7 +9,7 @@ const handleFetchUserWishes = async ():Promise=>{ const response:any = await request.get('/wishes'); console.log('this is data from wishlist', response); if(response.status == 200){ - return response.wishes.length; + return response?.wishes?.length; } } return 0;