-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(favorites): done listing user favorites
- Loading branch information
1 parent
07c611a
commit a61ddf1
Showing
4 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { getCurrentUser } from "./getCurrentUser"; | ||
import prisma from "../libs/prismadb"; | ||
|
||
export default async function getFavoriteListing() { | ||
try { | ||
const currentUser = await getCurrentUser(); | ||
|
||
if (!currentUser) { | ||
return []; | ||
} | ||
|
||
const favorites = await prisma.listing.findMany({ | ||
where: { | ||
id: { | ||
in: [...(currentUser.favoriteIds || [])], | ||
}, | ||
}, | ||
}); | ||
|
||
const safeFavorites = favorites.map((favorite) => ({ | ||
...favorite, | ||
createdAt: favorite.createdAt.toISOString(), | ||
updatedAt: favorite.updatedAt.toISOString(), | ||
})); | ||
|
||
return safeFavorites; | ||
} catch (error: any) { | ||
throw new Error(error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
import { SafeListing, SafeUser } from "../types"; | ||
import Container from "../components/Container"; | ||
import Heading from "../components/Heading"; | ||
import ListingCard from "../components/Listing/ListingCard"; | ||
|
||
interface Props { | ||
listings: SafeListing[]; | ||
currentUser?: SafeUser | null; | ||
} | ||
|
||
const FavoritesClient: React.FC<Props> = ({ listings, currentUser }) => { | ||
return ( | ||
<Container> | ||
<Heading title="Favorites" subtitle="List of places you have favorited" /> | ||
<div className="mt-10 grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 2xl:grid-cols-6 gap-8"> | ||
{listings.map((listing) => ( | ||
<ListingCard | ||
key={listing.id} | ||
data={listing} | ||
currentUser={currentUser} | ||
/> | ||
))} | ||
</div> | ||
</Container> | ||
); | ||
}; | ||
|
||
export default FavoritesClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from "react"; | ||
import { getCurrentUser } from "../actions/getCurrentUser"; | ||
import ClientOnly from "../components/ClientOnly"; | ||
import EmptyState from "../components/EmptyState"; | ||
import getFavoriteListing from "../actions/getFavoriteListing"; | ||
import FavoritesClient from "./FavoritesClient"; | ||
|
||
const FavoritesPage = async () => { | ||
const currentUser = await getCurrentUser(); | ||
const favorites = await getFavoriteListing(); | ||
const isEmptyFavorites = favorites.length === 0; | ||
|
||
if (!currentUser) { | ||
return ( | ||
<ClientOnly> | ||
<EmptyState title="Unauthorized" subtitle="Please login" /> | ||
</ClientOnly> | ||
); | ||
} | ||
|
||
if (isEmptyFavorites) { | ||
return ( | ||
<ClientOnly> | ||
<EmptyState | ||
title="No favorites found" | ||
subtitle="You have no favorites" | ||
/> | ||
</ClientOnly> | ||
); | ||
} | ||
|
||
return ( | ||
<ClientOnly> | ||
<FavoritesClient listings={favorites} currentUser={currentUser} /> | ||
</ClientOnly> | ||
); | ||
}; | ||
|
||
export default FavoritesPage; |