Skip to content

Commit

Permalink
feature(favorites): done listing user favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
devinpapalangi committed Aug 8, 2024
1 parent 07c611a commit a61ddf1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
30 changes: 30 additions & 0 deletions app/actions/getFavoriteListing.ts
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);
}
}
5 changes: 4 additions & 1 deletion app/components/Navbar/UserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ const UserMenu: React.FC<Props> = ({ currentUser }) => {
onClick={() => router.push("/trips")}
label={"My trips"}
/>
<MenuItem onClick={() => {}} label={"My Favorites"} />
<MenuItem
onClick={() => router.push("/favorites")}
label={"My Favorites"}
/>
<MenuItem
onClick={() => router.push("/reservations")}
label={"My Reservations"}
Expand Down
31 changes: 31 additions & 0 deletions app/favorites/FavoritesClient.tsx
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;
39 changes: 39 additions & 0 deletions app/favorites/page.tsx
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;

0 comments on commit a61ddf1

Please sign in to comment.