link-stack/apps/link/pages/tickets/[id].tsx
2023-03-01 11:02:15 +00:00

61 lines
1.7 KiB
TypeScript

import { GetServerSideProps, GetServerSidePropsContext } from "next";
import Head from "next/head";
import useSWR from "swr";
import { NextPage } from "next";
import { Grid, } from "@mui/material";
import { Layout } from "components/Layout";
import { TicketDetail } from "components/TicketDetail";
import { TicketEdit } from "components/TicketEdit";
type TicketProps = {
id: string;
};
const Ticket: NextPage<TicketProps> = ({ id }) => {
const origin = typeof window !== 'undefined' && window.location.origin
? window.location.origin
: '';
const fetcher = async (url: string) => {
const res = await fetch(url);
return res.json();
}
const { data: ticketData, error: ticketError } = useSWR(
`${origin}/api/v1/tickets/${id}`,
fetcher
);
const { data: articlesData, error: articlesError } = useSWR(
`${origin}/api/v1/ticket_articles/by_ticket/${id}`,
fetcher
);
const shouldRender = !ticketError && !articlesError && ticketData && articlesData && !ticketData.error && !articlesData.error;
return (
<Layout>
<Head>
<title>Link Shell</title>
</Head>
{shouldRender && (
<Grid container spacing={0} sx={{ height: "100vh" }} direction="row">
<Grid item sx={{ height: "100vh" }} xs={10}>
<TicketDetail ticket={ticketData} articles={articlesData} />
</Grid>
<Grid item xs={2} sx={{ height: "100vh" }}>
<TicketEdit ticket={ticketData} />
</Grid>
</Grid>)}
{ticketError && <div>{ticketError.toString()}</div>}
</Layout>
);
}
export const getServerSideProps: GetServerSideProps = async (
context: GetServerSidePropsContext) => {
const { id } = context.query;
return { props: { id } };
}
export default Ticket;