59 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			59 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { Handlers, PageProps } from "$fresh/server.ts";
 | ||
| import { MainLayout } from "@components/layouts/main.tsx";
 | ||
| import { Recipe } from "@lib/recipeSchema.ts";
 | ||
| import { Grid } from "@components/Grid.tsx";
 | ||
| import { IconArrowLeft } from "@components/icons.tsx";
 | ||
| import { KMenu } from "@islands/KMenu.tsx";
 | ||
| import { RedirectSearchHandler } from "@islands/Search.tsx";
 | ||
| import { GenericResource } from "@lib/types.ts";
 | ||
| import { ResourceCard } from "@components/Card.tsx";
 | ||
| import { fetchResource } from "@lib/resources.ts";
 | ||
| import { parseResourceUrl, searchResource } from "@lib/search.ts";
 | ||
| 
 | ||
| export const handler: Handlers<
 | ||
|   { recipes: Recipe[] | null; searchResults?: GenericResource[] }
 | ||
| > = {
 | ||
|   async GET(req, ctx) {
 | ||
|     const { content: recipes } = await fetchResource("recipes");
 | ||
|     const searchParams = parseResourceUrl(req.url);
 | ||
|     const searchResults = searchParams &&
 | ||
|       await searchResource({ ...searchParams, types: ["recipe"] });
 | ||
|     return ctx.render({ recipes, searchResults });
 | ||
|   },
 | ||
| };
 | ||
| 
 | ||
| export default function Greet(
 | ||
|   props: PageProps<
 | ||
|     { recipes: Recipe[] | null; searchResults: GenericResource[] }
 | ||
|   >,
 | ||
| ) {
 | ||
|   const { recipes, searchResults } = props.data;
 | ||
|   return (
 | ||
|     <MainLayout
 | ||
|       url={props.url}
 | ||
|       title="Recipes"
 | ||
|       searchResults={searchResults}
 | ||
|       context={{ type: "recipe" }}
 | ||
|     >
 | ||
|       <RedirectSearchHandler />
 | ||
|       <KMenu type="main" context={{ type: "recipe" }} />
 | ||
|       <header class="flex gap-4 items-center mb-2 lg:mb-5 md:hidden">
 | ||
|         <a
 | ||
|           class="px-4 lg:ml-4 py-2 bg-gray-300 text-gray-800 rounded-lg flex items-center gap-1"
 | ||
|           href="/"
 | ||
|         >
 | ||
|           <IconArrowLeft class="w-5 h-5" />
 | ||
|           Back
 | ||
|         </a>
 | ||
| 
 | ||
|         <h3 class="text-2xl text-white font-light">🍽️ Recipes</h3>
 | ||
|       </header>
 | ||
|       <Grid>
 | ||
|         {recipes?.filter((s) => !!s?.content?.name).map((doc) => {
 | ||
|           return <ResourceCard sublink="recipes" key={doc.name} res={doc} />;
 | ||
|         })}
 | ||
|       </Grid>
 | ||
|     </MainLayout>
 | ||
|   );
 | ||
| }
 |