memorium/components/IngredientsList.tsx

72 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-07-26 15:48:03 +02:00
import type {
Ingredient,
IngredientGroup,
Ingredients,
} from "../lib/recipes.ts";
import { FunctionalComponent } from "preact";
2023-07-26 13:47:01 +02:00
type IngredientsProps = {
ingredients: Ingredients;
};
2023-07-26 15:48:03 +02:00
const IngredientList = ({ ingredients }: { ingredients: Ingredient[] }) => {
return (
<>
{ingredients.map((item, index) => {
// Render Ingredient
const { type, amount, unit } = item as Ingredient;
return (
<tr key={index}>
<td class="pr-4 py-2">
{amount + (typeof unit !== "undefined" ? unit : "")}
</td>
<td class="px-4 py-2">{type}</td>
</tr>
);
})}
</>
);
};
2023-07-26 13:47:01 +02:00
2023-07-26 15:48:03 +02:00
const IngredientTable: FunctionalComponent<{ ingredients: Ingredients }> = (
{ ingredients },
) => {
2023-07-26 13:47:01 +02:00
return (
2023-07-26 15:48:03 +02:00
<table class="w-full border-collapse table-auto">
<tbody>
{ingredients.map((item, index) => {
if ("name" in item) {
// Render IngredientGroup
const { name, ingredients: groupIngredients } =
item as IngredientGroup;
return (
<>
<tr key={index}>
<td colSpan={3} class="pr-4 py-2 font-italic">{name}</td>
</tr>
<IngredientList ingredients={groupIngredients} />
</>
);
} else {
// Render Ingredient
const { type, amount, unit } = item as Ingredient;
return (
<tr key={index}>
<td class="pr-4 py-2">
{(amount ? amount : "") +
(unit ? (" " + unit) : "")}
</td>
<td class="px-4 py-2">{type}</td>
</tr>
);
}
})}
</tbody>
</table>
2023-07-26 13:47:01 +02:00
);
};
2023-07-26 15:48:03 +02:00
export const IngredientsList = ({ ingredients }: IngredientsProps) => {
return <IngredientTable ingredients={ingredients} />;
};