link-stack/apps/link/pages/tickets/[id].tsx

113 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-02-22 13:05:52 +00:00
import { GetServerSideProps, GetServerSidePropsContext } from "next";
import Head from "next/head";
import useSWR from "swr";
2023-03-07 14:09:49 +00:00
import { request, gql } from "graphql-request";
2023-02-22 13:05:52 +00:00
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 }) => {
2023-03-01 11:02:15 +00:00
const origin = typeof window !== 'undefined' && window.location.origin
? window.location.origin
: '';
2023-03-07 14:09:49 +00:00
const graphQLFetcher = async ({ document, variables }: any) => {
const data = await request({
url: `${origin}/graphql`,
document,
variables
})
console.log({ data })
return data;
};
const { data: ticketData, error: ticketError }: any = useSWR(
{
document: gql`
query getTicket($ticketId: Int!) {
ticket(
ticket: {
ticketInternalId: $ticketId
}
) {
id,
internalId,
title,
note,
number,
createdAt,
updatedAt,
closeAt,
articles {
edges {
node {
id,
body,
internal,
sender {
name
}
}
}
}
}}`, variables: { ticketId: parseInt(id, 10) }
},
graphQLFetcher,
{ refreshInterval: 1000 }
2023-02-22 13:05:52 +00:00
);
2023-03-07 14:09:49 +00:00
const { data: graphqlData2, error: graphqlError2 } = useSWR(
{
document: gql`
{
__schema {
queryType {
name
fields {
name
}
}
}
}`, variables: {}
},
graphQLFetcher
2023-02-22 13:05:52 +00:00
);
2023-03-07 14:09:49 +00:00
const shouldRender = !ticketError && ticketData;
2023-02-22 13:05:52 +00:00
return (
<Layout>
<Head>
<title>Link Shell</title>
</Head>
{shouldRender && (
<Grid container spacing={0} sx={{ height: "100vh" }} direction="row">
2023-03-07 14:09:49 +00:00
<Grid item sx={{ height: "100vh" }} xs={12}>
2023-02-22 13:05:52 +00:00
2023-03-07 14:09:49 +00:00
<TicketDetail ticket={ticketData.ticket} />
2023-02-22 13:05:52 +00:00
</Grid>
2023-03-07 14:09:49 +00:00
{/*<Grid item xs={0} sx={{ height: "100vh" }}>
<TicketEdit ticket={ticketData.ticket} />
</Grid>*/}
2023-02-22 13:05:52 +00:00
</Grid>)}
{ticketError && <div>{ticketError.toString()}</div>}
</Layout>
);
}
export const getServerSideProps: GetServerSideProps = async (
context: GetServerSidePropsContext) => {
const { id } = context.query;
return { props: { id } };
}
export default Ticket;