import Head from "next/head";
import useSWR from "swr";
import { NextPage } from "next";
import { Layout } from "components/Layout";
import { TicketList } from "components/TicketList";
import { getTicketsByOverviewQuery } from "graphql/getTicketsByOverviewQuery";

const Assigned: NextPage = () => {
  const { data: ticketData, error: ticketError }: any = useSWR(
    {
      document: getTicketsByOverviewQuery,
      variables: { overviewId: "gid://zammad/Overview/1" },
    },
    { refreshInterval: 10000 }
  );

  const shouldRender = !ticketError && ticketData;
  const tickets =
    ticketData?.ticketsByOverview?.edges.map((edge: any) => edge.node) || [];

  return (
    <Layout>
      <Head>
        <title>Link Shell – Assigned Tickets</title>
      </Head>
      {shouldRender && <TicketList title="Assigned" tickets={tickets} />}
      {ticketError && <div>{ticketError.toString()}</div>}
    </Layout>
  );
};

export default Assigned;
