1
Fork 0
mirror of https://github.com/Steffo99/festa.git synced 2024-12-22 22:54:22 +00:00
festa/pages/events/[slug].tsx

132 lines
5.2 KiB
TypeScript
Raw Normal View History

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-17 04:20:52 +00:00
import { EditableMarkdown, EditableText } from '../../components/generic/editable/inputs'
2022-07-16 16:18:41 +00:00
import { EditingContext, EditingMode } from '../../components/generic/editable/base'
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'
import { ViewContent } from '../../components/generic/views/content'
import { useAxios } from '../../components/auth/requests'
2022-07-18 02:47:02 +00:00
import { faAsterisk } from '@fortawesome/free-solid-svg-icons'
import { FestaIcon } from '../../components/generic/renderers/fontawesome'
import { promiseMultiplexer, usePromise, UsePromiseStatus } from '../../components/generic/loading/promise'
import { EditingContextProvider } from '../../components/generic/editable/provider'
import { swrMultiplexer } from '../../components/generic/loading/swr'
import { LoadingMain, LoadingInline } from '../../components/generic/loading/renderers'
import { ViewNotice } from '../../components/generic/views/notice'
import { ErrorBlock } from '../../components/generic/errors/renderers'
import { database } from '../../utils/prismaClient'
2022-06-03 01:55:02 +00:00
export async function getServerSideProps(context: NextPageContext) {
const slug = context.query.slug as string
2022-06-03 01:55:02 +00:00
return {
props: {
slug,
event: 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 = {
slug: string,
event: Event,
2022-06-03 01:55:02 +00:00
}
const PageEvent: NextPage<PageEventProps> = ({ slug, event }) => {
const { t } = useTranslation()
const swrHook = useSWR<Event>(`/api/events/${slug}`, { fallback: event })
const [auth,] = useDefinedContext(AuthContext)
const axios = useAxios()
2022-06-10 03:21:02 +00:00
const save = useCallback(
2022-07-17 00:31:04 +00:00
async () => {
if (swrHook.data === undefined) {
2022-07-18 02:47:02 +00:00
console.warn("[PageEvent] Tried to save while no data was available.")
return
}
await axios.patch(`/api/events/${slug}`, swrHook.data)
swrHook.mutate(swrHook.data)
console.debug("[PageEvent] Saved updated data successfully!")
2022-07-16 18:02:03 +00:00
},
[axios, swrHook]
2022-07-16 18:02:03 +00:00
)
2022-06-04 03:13:19 +00:00
return <>
<Head>
<title key="title">{swrHook.data?.name ?? slug} - {t("siteTitle")}</title>
2022-06-04 03:13:19 +00:00
</Head>
2022-06-11 03:08:49 +00:00
<Postcard
src={swrHook.data?.postcard || defaultPostcard}
2022-06-11 03:08:49 +00:00
/>
2022-07-16 16:18:41 +00:00
<WIPBanner />
<EditingContextProvider>
{swrMultiplexer({
hook: swrHook,
loading: () => (
<ViewNotice
notice={
<LoadingMain text={t("eventLoading")} />
}
2022-07-16 16:18:41 +00:00
/>
),
ready: (data) => (
<ViewContent
title={
<EditableText
value={data?.name ?? slug}
onChange={e => swrHook.mutate(async state => state ? { ...state, name: e.target.value } : undefined, { revalidate: false })}
placeholder={t("eventTitlePlaceholder")}
/>
}
content={
<EditableMarkdown
value={data?.description ?? ""}
onChange={e => swrHook.mutate(async state => state ? { ...state, description: e.target.value } : undefined, { revalidate: false })}
placeholder={t("eventDescriptionPlaceholder")}
/>
}
2022-07-16 16:18:41 +00:00
/>
),
error: (error) => (
<ViewNotice
notice={
<ErrorBlock
text={t("eventError")}
error={error}
/>
}
/>
)
})}
2022-07-16 16:18:41 +00:00
<ToolBar vertical="vadapt" horizontal="right">
<ToolToggleVisibility />
{swrHook.data && auth?.userId === swrHook.data?.creatorId &&
2022-07-16 18:02:03 +00:00
<ToolToggleEditing
save={save}
2022-07-16 18:02:03 +00:00
/>
}
2022-07-16 16:18:41 +00:00
</ToolBar>
</EditingContextProvider>
2022-06-04 03:13:19 +00:00
</>
2022-06-11 03:08:49 +00:00
}
export default PageEvent