index.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import Layout from "@/components/Layout";
  2. import { GetStaticProps, InferGetStaticPropsType } from "next";
  3. import { getAllRecipes, getAllTags } from '@/utils/api';
  4. import Header from '@/components/Layout/Header';
  5. import Container from '@/components/Layout/Container';
  6. import { useRouter } from 'next/router';
  7. import { Subtitle } from 'components/Typography';
  8. import RecipeItem from "components/RecipeItem";
  9. import { TextInput, ChipGroup, Chip } from "@/components/Forms";
  10. import { MouseEventHandler } from "react";
  11. export const getStaticProps = async () => {
  12. const recipeList = await getAllRecipes();
  13. const tags = await getAllTags();
  14. return {
  15. props: {
  16. recipeList,
  17. tags: tags.map(tag => tag.name),
  18. },
  19. };
  20. };
  21. const Recipes = ({
  22. recipeList = [],
  23. tags
  24. }: InferGetStaticPropsType<typeof getStaticProps>) => {
  25. const router = useRouter();
  26. const openRecipe: (id: string) => MouseEventHandler<Element> = (id) => (e) => {
  27. e.preventDefault();
  28. router.push(`/recipes/${id}`);
  29. }
  30. return (
  31. <Layout>
  32. <Header>
  33. <TextInput placeholder='search' id='recipe-search' />
  34. <ChipGroup>
  35. {tags.map(tag => (
  36. <Chip key={tag} label={tag} color='primary' />
  37. ))}
  38. </ChipGroup>
  39. </Header>
  40. <Container>
  41. <Subtitle>Favorites</Subtitle>
  42. </Container>
  43. <Container>
  44. <Subtitle>All</Subtitle>
  45. {recipeList.map((recipe) => (
  46. <RecipeItem recipe={recipe} key={recipe._id} onClick={openRecipe(recipe._id)} />
  47. ))}
  48. </Container>
  49. </Layout>
  50. );
  51. };
  52. export default Recipes;