2022-06-11 03:08:49 +00:00
|
|
|
import { NextPage, NextPageContext } from 'next'
|
|
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
|
|
|
|
import { default as Head } from 'next/head'
|
2022-06-09 21:43:38 +00:00
|
|
|
import defaultPostcard from "../../public/postcards/adi-goldstein-Hli3R6LKibo-unsplash.jpg"
|
2022-06-11 03:08:49 +00:00
|
|
|
import { Postcard } from '../../components/postcard/changer'
|
|
|
|
import useSWR from 'swr'
|
|
|
|
import { Event } from '@prisma/client'
|
2022-07-18 02:47:02 +00:00
|
|
|
import { useCallback, useState } from 'react'
|
2022-07-16 16:18:41 +00:00
|
|
|
import { ToolBar } from '../../components/generic/toolbar/bar'
|
|
|
|
import { ToolToggleEditing } from '../../components/events/toolbar/toolToggleEditing'
|
|
|
|
import { ToolToggleVisibility } from '../../components/postcard/toolbar/toolToggleVisibility'
|
|
|
|
import { WIPBanner } from '../../components/generic/wip/banner'
|
2022-07-16 18:02:03 +00:00
|
|
|
import { AuthContext } from '../../components/auth/base'
|
|
|
|
import { useDefinedContext } from '../../utils/definedContext'
|
2022-07-17 02:28:46 +00:00
|
|
|
import { useAxios } from '../../components/auth/requests'
|
2022-07-19 17:04:29 +00:00
|
|
|
import { database } from '../../utils/prismaClient'
|
2022-07-19 18:25:11 +00:00
|
|
|
import { EventsActionEdit } from '../../components/events/actions/edit'
|
|
|
|
import { EventsActionView } from '../../components/events/actions/view'
|
2022-07-20 11:26:34 +00:00
|
|
|
import { usePromise } from '../../components/generic/loading/promise'
|
2022-06-03 01:55:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
export async function getServerSideProps(context: NextPageContext) {
|
2022-07-19 17:04:29 +00:00
|
|
|
const slug = context.query.slug as string
|
|
|
|
|
2022-06-03 01:55:02 +00:00
|
|
|
return {
|
|
|
|
props: {
|
2022-07-19 17:04:29 +00:00
|
|
|
slug,
|
2022-07-26 23:46:09 +00:00
|
|
|
defaultData: await database.event.findUnique({ where: { slug } }),
|
2022-06-03 01:55:02 +00:00
|
|
|
...(await serverSideTranslations(context.locale ?? "it-IT", ["common"]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-11 03:08:49 +00:00
|
|
|
type PageEventProps = {
|
2022-07-19 17:04:29 +00:00
|
|
|
slug: string,
|
2022-07-26 23:46:09 +00:00
|
|
|
defaultData: Event,
|
2022-06-03 01:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-26 23:46:09 +00:00
|
|
|
const PageEvent: NextPage<PageEventProps> = ({ slug, defaultData }) => {
|
2022-07-18 04:13:19 +00:00
|
|
|
const { t } = useTranslation()
|
|
|
|
const axios = useAxios()
|
2022-06-10 03:21:02 +00:00
|
|
|
|
2022-07-19 18:25:11 +00:00
|
|
|
const [eventEditing, eventSetEditing] = useState<boolean>(false)
|
2022-07-26 23:46:09 +00:00
|
|
|
const [data, setData] = useState<Event>(defaultData)
|
2022-07-20 10:29:07 +00:00
|
|
|
const [auth,] = useDefinedContext(AuthContext)
|
2022-07-19 18:25:11 +00:00
|
|
|
|
2022-07-20 11:26:34 +00:00
|
|
|
const save = usePromise<void, void>(useCallback(
|
2022-07-17 00:31:04 +00:00
|
|
|
async () => {
|
2022-07-19 18:25:11 +00:00
|
|
|
const response = await axios.patch<Event>(`/api/events/${slug}`, data!)
|
2022-07-26 23:46:09 +00:00
|
|
|
setData(response.data)
|
2022-07-16 18:02:03 +00:00
|
|
|
},
|
2022-07-26 23:46:09 +00:00
|
|
|
[axios, data, setData, slug]
|
2022-07-20 11:26:34 +00:00
|
|
|
))
|
2022-07-16 18:02:03 +00:00
|
|
|
|
2022-07-19 18:25:11 +00:00
|
|
|
const eventName = data?.name ?? slug
|
|
|
|
const eventPostcard = data?.postcard || defaultPostcard
|
|
|
|
const eventCanEdit = auth && data && auth.userId === data.creatorId
|
|
|
|
|
2022-06-04 03:13:19 +00:00
|
|
|
return <>
|
|
|
|
<Head>
|
2022-07-19 18:25:11 +00:00
|
|
|
<title key="title">{eventName} - {t("siteTitle")}</title>
|
2022-06-04 03:13:19 +00:00
|
|
|
</Head>
|
2022-07-19 18:25:11 +00:00
|
|
|
<Postcard src={eventPostcard} />
|
2022-07-16 16:18:41 +00:00
|
|
|
<WIPBanner />
|
2022-07-19 18:25:11 +00:00
|
|
|
<ToolBar vertical="vadapt" horizontal="right">
|
|
|
|
<ToolToggleVisibility />
|
|
|
|
{eventCanEdit &&
|
|
|
|
<ToolToggleEditing
|
|
|
|
editing={eventEditing}
|
|
|
|
setEditing={eventSetEditing}
|
|
|
|
save={save}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
</ToolBar>
|
|
|
|
{eventEditing ?
|
2022-07-26 23:46:09 +00:00
|
|
|
<EventsActionEdit data={data!} setData={setData} save={save} setEditing={eventSetEditing} />
|
2022-07-19 18:25:11 +00:00
|
|
|
:
|
|
|
|
<EventsActionView data={data!} />
|
|
|
|
}
|
2022-06-04 03:13:19 +00:00
|
|
|
</>
|
2022-06-11 03:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default PageEvent
|