74 lines
2.0 KiB
TypeScript
74 lines
2.0 KiB
TypeScript
import { PRICE, PrismaClient } from "@prisma/client";
|
|
import Header from "../components/Header";
|
|
import RestaurantCard from "./components/RestaurantCard";
|
|
import SearchSideBar from "./components/SearchSideBar";
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
const fetchRestaurants = (searchParams: {city?: string, location?: string, cuisine?: string, price?: PRICE} | undefined) => {
|
|
const select = {
|
|
id: true,
|
|
name: true,
|
|
main_image: true,
|
|
price: true,
|
|
cuisine: true,
|
|
location: true,
|
|
slug: true
|
|
}
|
|
|
|
if (!searchParams) return prisma.restaurant.findMany({select});
|
|
|
|
return prisma.restaurant.findMany({
|
|
where: {
|
|
location: {
|
|
name: {
|
|
equals: searchParams.city ? searchParams.city.toLowerCase() : undefined
|
|
}
|
|
},
|
|
cuisine: {
|
|
name: {
|
|
equals: searchParams.cuisine ? searchParams.cuisine : undefined
|
|
}
|
|
},
|
|
price: {
|
|
equals: searchParams.price ? searchParams.price : undefined
|
|
}
|
|
},
|
|
select
|
|
})
|
|
}
|
|
|
|
const fetchLocations = () => {
|
|
return prisma.location.findMany({select: {name: true}})
|
|
}
|
|
|
|
const fetchCuisines = () => {
|
|
return prisma.cuisine.findMany({select: {name: true}})
|
|
}
|
|
|
|
export default async function Search({searchParams}: {searchParams: {city?: string, location?: string, cuisine?: string, price?: PRICE}}) {
|
|
const restaurants = await fetchRestaurants(searchParams)
|
|
const cuisines = await fetchCuisines()
|
|
const locations = await fetchLocations()
|
|
|
|
// console.log(restaurants)
|
|
return (
|
|
<>
|
|
<Header />
|
|
<div className="flex py-4 m-auto w-2/3 justify-between items-start">
|
|
{/* @ts-expect-error Server Component */}
|
|
<SearchSideBar locations={locations} cuisines={cuisines} searchParams={searchParams}/>
|
|
<div className="w-5/6">
|
|
{
|
|
restaurants.length
|
|
? restaurants.map(restaurant => {
|
|
return <RestaurantCard restaurant={restaurant}/>
|
|
})
|
|
: <p>No restaurants</p>
|
|
}
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|