memorium/routes/recipes/index.tsx

54 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-07-26 13:47:01 +02:00
import { Handlers, PageProps } from "$fresh/server.ts";
2023-07-27 15:28:50 +02:00
import { RecipeCard } from "@components/RecipeCard.tsx";
import { MainLayout } from "@components/layouts/main.tsx";
2023-08-01 17:50:00 +02:00
import { getAllRecipes, Recipe } from "@lib/resource/recipes.ts";
2023-08-02 01:58:03 +02:00
import { Grid } from "@components/Grid.tsx";
2023-08-02 13:11:17 +02:00
import { IconArrowLeft } from "@components/icons.tsx";
2023-08-07 14:44:04 +02:00
import { KMenu } from "@islands/KMenu.tsx";
2023-08-10 16:59:18 +02:00
import { fetchQueryResource, RedirectSearchHandler } from "@islands/Search.tsx";
import { parseResourceUrl, searchResource } from "@lib/search.ts";
import { SearchResult } from "@lib/types.ts";
2023-07-30 23:55:51 +02:00
2023-07-26 13:47:01 +02:00
export const handler: Handlers<Recipe[] | null> = {
2023-08-10 16:59:18 +02:00
async GET(req, ctx) {
2023-08-01 17:50:00 +02:00
const recipes = await getAllRecipes();
2023-08-10 16:59:18 +02:00
const searchParams = parseResourceUrl(req.url);
const searchResults = searchParams &&
await searchResource({ ...searchParams, type: "recipe" });
return ctx.render({ recipes, searchResults });
2023-07-26 13:47:01 +02:00
},
};
2023-08-10 16:59:18 +02:00
export default function Greet(
props: PageProps<{ recipes: Recipe[] | null; searchResults: SearchResult }>,
) {
const { recipes, searchResults } = props.data;
2023-07-26 13:47:01 +02:00
return (
2023-08-10 16:59:18 +02:00
<MainLayout
url={props.url}
title="Recipes"
searchResults={searchResults}
context={{ type: "recipe" }}
>
2023-08-07 14:44:04 +02:00
<RedirectSearchHandler />
<KMenu type="main" context={{ type: "recipe" }} />
2023-08-02 17:35:10 +02:00
<header class="flex gap-4 items-center mb-2 lg:mb-5 md:hidden">
<a
2023-08-02 17:35:10 +02:00
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>
2023-07-30 23:55:51 +02:00
<h3 class="text-2xl text-white font-light">🍽 Recipes</h3>
</header>
2023-08-02 01:58:03 +02:00
<Grid>
2023-08-10 16:59:18 +02:00
{recipes?.map((doc) => {
2023-07-26 13:47:01 +02:00
return <RecipeCard recipe={doc} />;
})}
2023-08-02 01:58:03 +02:00
</Grid>
2023-07-26 13:47:01 +02:00
</MainLayout>
);
}