From e7c372285233f3e03db6612e70bee108d6548f76 Mon Sep 17 00:00:00 2001 From: Shivansh Saxena <112249407+ShivanshPlays@users.noreply.github.com> Date: Fri, 8 Nov 2024 01:14:20 +0530 Subject: [PATCH] bookmarkFE: added global state and frontend modifications to implement the feature (#516) * bookmark schema and server actions * added testing API for bookmark * half baked * bookmarkFE: added global state and modifications to implement the feature --- .../user/bookmarks/GETBYUSER_bookmark.ts | 22 ++- .../(routes)/(mainNavPages)/[userId]/page.tsx | 76 +++++----- .../app/(routes)/(mainNavPages)/food/page.tsx | 4 +- .../(routes)/(mainNavPages)/for-sale/page.tsx | 1 + .../(routes)/(mainNavPages)/house/page.tsx | 1 + scruter-nextjs/app/(routes)/layout.tsx | 9 +- scruter-nextjs/components/listingCardFE.tsx | 53 ++++++- .../context/UserBookmarkProvider.tsx | 133 ++++++++++++++++++ scruter-nextjs/lib/providers.tsx | 1 + 9 files changed, 246 insertions(+), 54 deletions(-) create mode 100644 scruter-nextjs/context/UserBookmarkProvider.tsx diff --git a/scruter-nextjs/actions/user/bookmarks/GETBYUSER_bookmark.ts b/scruter-nextjs/actions/user/bookmarks/GETBYUSER_bookmark.ts index b8f9637b..a28fad0c 100644 --- a/scruter-nextjs/actions/user/bookmarks/GETBYUSER_bookmark.ts +++ b/scruter-nextjs/actions/user/bookmarks/GETBYUSER_bookmark.ts @@ -1,22 +1,34 @@ 'use server'; import prismadb from '@/lib/prismadb'; -import { Bookmark } from '@prisma/client'; +import { Bookmark, Image, Listing } from '@prisma/client'; + +// Interface for the Bookmark with Listing and Images +export interface BookmarkWithListing extends Bookmark { + listing: Listing & { + images: Image[]; // Assuming images is an array of strings (URLs) + }; +} export async function getBookmarksByUser({ userId, }: { userId: string; -}): Promise<{ success: boolean; error?: string; data?: Bookmark[] }> { +}): Promise<{ success: boolean; error?: string; data?: BookmarkWithListing[] }> { try { + // Fetch bookmarks including related listing data and images const bookmarks = await prismadb.bookmark.findMany({ where: { userId }, include: { - listing: true, // Include related listing details + listing: { + include: { + images: true, // Include the images field from the listing + }, + }, }, }); - - return { success: true, data: bookmarks }; + // console.log(bookmarks) + return { success: true, data: bookmarks as BookmarkWithListing[] }; } catch (error) { console.error('[GET_BOOKMARKS_BY_USER_ERROR]', error); return { success: false, error: 'Error fetching bookmarks' }; diff --git a/scruter-nextjs/app/(routes)/(mainNavPages)/[userId]/page.tsx b/scruter-nextjs/app/(routes)/(mainNavPages)/[userId]/page.tsx index 679694e2..c7da558e 100644 --- a/scruter-nextjs/app/(routes)/(mainNavPages)/[userId]/page.tsx +++ b/scruter-nextjs/app/(routes)/(mainNavPages)/[userId]/page.tsx @@ -6,11 +6,14 @@ import { Bookmark, Delete, Edit, Mail, Text, Trash, User } from 'lucide-react'; import { getuserById } from '@/actions/user/userGET'; import toast from 'react-hot-toast'; import { User as UserType } from '@prisma/client'; +import { useBookmark } from '@/context/UserBookmarkProvider'; export default function UserProfile() { const { data: session } = useSession(); const [user, setUser] = useState(null); - const [bookmarks, setBookmarks] = useState([]); + + const { bookmarks, fetchBookmarks, removeFromBookmarks, loading, error } = + useBookmark(); useEffect(() => { if (session) { @@ -29,31 +32,21 @@ export default function UserProfile() { } }; fetchUserData(); + + fetchBookmarks(session.user.id); } - }, [session]); + }, []); - const dummyBookmarks = [ - { - id: '1', - name: 'Spicy Fried Chicken', - price: 25, - description: 'A delicious spicy fried chicken dish', - images: ['/seed/food1a.jpeg'], - }, - { - id: '2', - name: 'Pizza', - price: 15, - description: 'A large Pizza', - images: ['/seed/food2a.jpg'], - }, - ]; + - console.log(user); + // if(!loading&&bookmarks.length>0){ + // console.log('BOOKMARKSSSSSSS' + bookmarks[0].listing); + // } + return (
{/* User Info Section */} - {user && ( + {user && user.id && ( <> @@ -76,36 +69,42 @@ export default function UserProfile() {
-
-

- - Bookmarked Listings -

-
- {dummyBookmarks.map(listing => ( + + {loading && ( +
Loading Bookmarks....
+ )} + {!loading && ( + +
+

+ + Bookmarked Listings +

+
+ {bookmarks.length>0 && bookmarks.map(bookmark => ( - {listing.name} + {bookmark.listing.name}

- {listing.description} + {bookmark.listing.description}

{listing.name} -

${listing.price}

+

${bookmark.listing.price}

- {/* Delete Button */} +
+
- {dummyBookmarks.length === 0 && ( + {bookmarks.length === 0 && (

No bookmarks yet.

)} -
+
+ )} )} diff --git a/scruter-nextjs/app/(routes)/(mainNavPages)/food/page.tsx b/scruter-nextjs/app/(routes)/(mainNavPages)/food/page.tsx index bab36215..de891e3b 100644 --- a/scruter-nextjs/app/(routes)/(mainNavPages)/food/page.tsx +++ b/scruter-nextjs/app/(routes)/(mainNavPages)/food/page.tsx @@ -5,7 +5,6 @@ import axios from 'axios'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faSearch } from '@fortawesome/free-solid-svg-icons'; // Import FontAwesomeIcon import { GetAllListing, ListingWithImages } from '@/actions/seller/listing'; -import { Listing } from '@prisma/client'; import toast, { Toaster } from 'react-hot-toast'; import ListingCardFE from '@/components/listingCardFE'; @@ -15,6 +14,8 @@ const FoodPage: React.FC = () => { const [query, setQuery] = useState(''); const [sort, setSort] = useState<"" | "asc" | "desc" | undefined>(''); + + useEffect(() => { const fetchData = async () => { setLoading(true); @@ -132,6 +133,7 @@ const FoodPage: React.FC = () => { ) : ( foodItems.map(food => ( { ) : ( items.map(item => ( { ) : ( houses.map(house => ( ) { return ( <> - - {children} -