memorium/routes/recipes/index.tsx

54 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Handlers, PageProps } from "$fresh/server.ts";
import { RecipeCard } from "@components/RecipeCard.tsx";
import { MainLayout } from "@components/layouts/main.tsx";
import { getAllRecipes, Recipe } from "@lib/resource/recipes.ts";
import { Grid } from "@components/Grid.tsx";
import { IconArrowLeft } from "@components/icons.tsx";
import { KMenu } from "@islands/KMenu.tsx";
import { fetchQueryResource, RedirectSearchHandler } from "@islands/Search.tsx";
import { parseResourceUrl, searchResource } from "@lib/search.ts";
import { SearchResult } from "@lib/types.ts";
export const handler: Handlers<Recipe[] | null> = {
async GET(req, ctx) {
const recipes = await getAllRecipes();
const searchParams = parseResourceUrl(req.url);
const searchResults = searchParams &&
await searchResource({ ...searchParams, type: "recipe" });
return ctx.render({ recipes, searchResults });
},
};
export default function Greet(
props: PageProps<{ recipes: Recipe[] | null; searchResults: SearchResult }>,
) {
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?.map((doc) => {
return <RecipeCard recipe={doc} />;
})}
</Grid>
</MainLayout>
);
}