From 5e1ff05c0aa33473ac393ae7ebbeee0f45ce0b1f Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 3 Jun 2022 03:55:02 +0200 Subject: [PATCH] Create event page --- hooks/useEventDetail.ts | 6 ++++++ hooks/useMyEvents.ts | 5 +++-- pages/events/[slug].tsx | 48 +++++++++++++++++++++++++++++++++++++++++ pages/index.tsx | 2 +- styles/globals.css | 4 ++++ utils/interrupt.ts | 3 +++ 6 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 hooks/useEventDetail.ts create mode 100644 pages/events/[slug].tsx diff --git a/hooks/useEventDetail.ts b/hooks/useEventDetail.ts new file mode 100644 index 0000000..45f9e84 --- /dev/null +++ b/hooks/useEventDetail.ts @@ -0,0 +1,6 @@ +import { Event } from "@prisma/client"; +import useSWR, { SWRResponse } from "swr"; + +export function useEventDetail(slug: string): SWRResponse { + return useSWR(`/api/events/${slug}`) +} diff --git a/hooks/useMyEvents.ts b/hooks/useMyEvents.ts index 4e4e11d..a8d96f5 100644 --- a/hooks/useMyEvents.ts +++ b/hooks/useMyEvents.ts @@ -1,5 +1,6 @@ -import useSWR from "swr"; +import { Event } from "@prisma/client"; +import useSWR, { SWRResponse } from "swr"; -export function useMyEvents() { +export function useMyEvents(): SWRResponse { return useSWR("/api/events/mine") } diff --git a/pages/events/[slug].tsx b/pages/events/[slug].tsx new file mode 100644 index 0000000..f6a1df5 --- /dev/null +++ b/pages/events/[slug].tsx @@ -0,0 +1,48 @@ +import { Event } from "@prisma/client"; +import { NextPageContext } from "next"; +import { useTranslation } from "next-i18next"; +import { serverSideTranslations } from "next-i18next/serverSideTranslations"; +import { useRouter } from "next/router"; +import useSWR from "swr"; +import { ErrorInline } from "../../components/ErrorInline"; +import { Loading } from "../../components/Loading"; +import {useEventDetail} from "../../hooks/useEventDetail" +import { database } from "../../utils/prismaClient"; + + +export async function getServerSideProps(context: NextPageContext) { + const slug = context.query.slug as string + if(typeof slug === "object") { + return {notFound: true} + } + + const event = await database.event.findUnique({where: {slug}}) + if(!event) { + return {notFound: true} + } + + return { + props: { + event, + ...(await serverSideTranslations(context.locale ?? "it-IT", ["common"])) + } + } +} + + +type PageEventDetailProps = { + event: Event +} + + +export default function PageEventDetail({event}: PageEventDetailProps) { + const {t} = useTranslation() + + return ( +
+

+ {event.name} +

+
+ ) +} \ No newline at end of file diff --git a/pages/index.tsx b/pages/index.tsx index 3bea7ff..9d52770 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -17,7 +17,7 @@ export async function getStaticProps(context: NextPageContext) { export default function PageIndex() { - const { t } = useTranslation("common") + const { t } = useTranslation() const [login, _] = useDefinedContext(LoginContext) return ( diff --git a/styles/globals.css b/styles/globals.css index 1af8ef5..fd3fc19 100644 --- a/styles/globals.css +++ b/styles/globals.css @@ -211,3 +211,7 @@ input.negative, button.negative { max-width: 800px; width: 100%; } + +#page-event-detail h1 { + text-align: center; +} \ No newline at end of file diff --git a/utils/interrupt.ts b/utils/interrupt.ts index 9a27d21..54ad241 100644 --- a/utils/interrupt.ts +++ b/utils/interrupt.ts @@ -11,6 +11,9 @@ export async function handleInterrupts(res: NextApiResponse, f: () => Promise