Skip to content
Snippets Groups Projects
News.tsx 1.46 KiB
Newer Older
tovo's avatar
tovo committed
import React from "react";
tovo's avatar
tovo committed
import { useStaticQuery, graphql } from "gatsby";
tovo's avatar
tovo committed

import CardList from "./core/CardList";
tovo's avatar
tovo committed
import Button from "./core/Button";
tovo's avatar
tovo committed

tovo's avatar
tovo committed
interface INews {
  title?: string;
  action?: {
tovo's avatar
tovo committed
    title?: string;
    to?: string;
tovo's avatar
tovo committed
    classname?: string;
  };
  anime?: boolean;
tovo's avatar
tovo committed
}

tovo's avatar
tovo committed
const News = (props: INews) => {
  const data = useStaticQuery(graphql`
    query getNews {
      directus {
        news(
          limit: 3
          filter: { status: { _eq: "published" } }
          sort: "-date_published"
        ) {
          id
          content
          title
          date_published
          image {
            id
            imageFile {
              childImageSharp {
                gatsbyImageData
              }
            }
          }
        }
      }
    }
  `);
tovo's avatar
tovo committed

tovo's avatar
tovo committed
  const { title = "", anime = true, action } = props;
tovo's avatar
tovo committed

tovo's avatar
tovo committed
  const news = data.directus.news.map((dtNew) => {
    return {
      title: dtNew.title,
      content: dtNew.content,
      image: dtNew.image?.imageFile,
      to: "#",
    };
  });
tovo's avatar
tovo committed

tovo's avatar
tovo committed
  return (
    <>
      <h2 className="mt-2 text-3xl font-extrabold tracking-tight sm:text-4xl mb-8">
        {title}
      </h2>
      <CardList cards={news} layout="grid" anime={anime} />
      <div className="flex justify-center mt-8">
        {action && (
          <Button
            title={action.title}
            to={action.to}
            className={action.classname}
          />
        )}
      </div>
    </>
  );
tovo's avatar
tovo committed
};

tovo's avatar
tovo committed
export default News;