1
Fork 0
mirror of https://github.com/Steffo99/festa.git synced 2024-12-22 22:54:22 +00:00

Make button to save changes actually do that

This commit is contained in:
Steffo 2022-07-17 04:28:46 +02:00
parent 037f97c68d
commit e2aef26685
Signed by: steffo
GPG key ID: 6965406171929D01

View file

@ -7,7 +7,7 @@ import { Postcard } from '../../components/postcard/changer'
import { ViewEvent } from '../../components/events/views/event' import { ViewEvent } from '../../components/events/views/event'
import useSWR from 'swr' import useSWR from 'swr'
import { Event } from '@prisma/client' import { Event } from '@prisma/client'
import { EditableText } from '../../components/generic/editable/inputs' import { EditableFilePicker, EditableText } from '../../components/generic/editable/inputs'
import { EditingContext, EditingMode } from '../../components/generic/editable/base' import { EditingContext, EditingMode } from '../../components/generic/editable/base'
import { useCallback, useContext, useState } from 'react' import { useCallback, useContext, useState } from 'react'
import { ToolBar } from '../../components/generic/toolbar/bar' import { ToolBar } from '../../components/generic/toolbar/bar'
@ -17,6 +17,8 @@ import { WIPBanner } from '../../components/generic/wip/banner'
import { AuthContext } from '../../components/auth/base' import { AuthContext } from '../../components/auth/base'
import { useDefinedContext } from '../../utils/definedContext' import { useDefinedContext } from '../../utils/definedContext'
import { asleep } from '../../utils/asleep' import { asleep } from '../../utils/asleep'
import { ViewContent } from '../../components/generic/views/content'
import { useAxios } from '../../components/auth/requests'
export async function getServerSideProps(context: NextPageContext) { export async function getServerSideProps(context: NextPageContext) {
@ -38,51 +40,48 @@ const PageEvent: NextPage<PageEventProps> = ({ slug }) => {
const { t } = useTranslation() const { t } = useTranslation()
const { data, error, mutate } = useSWR<Event>(`/api/events/${slug}`) const { data, error, mutate } = useSWR<Event>(`/api/events/${slug}`)
const [auth, _setAuth] = useDefinedContext(AuthContext) const [auth, _setAuth] = useDefinedContext(AuthContext)
const axios = useAxios()
const displayTitle = data?.name ?? slug const displayTitle = data?.name ?? slug
const displayPostcard = data?.postcard ?? defaultPostcard const displayPostcard = data?.postcard ?? defaultPostcard
const displayDescription = data?.description ?? "" const displayDescription = data?.description ?? ""
const saveEdits = useCallback( const save = useCallback(
async () => { async () => {
await asleep(1000) await axios.patch(`/api/events/${slug}`, data)
mutate() mutate(data)
}, },
[] [axios, data]
) )
return <> return <>
<Head> <Head>
<title key="title">{displayTitle} - {t("siteTitle")}</title> <title key="title">{displayTitle} - {t("siteTitle")}</title>
<link rel="preload" href={`/api/events/${slug}`} as="fetch" />
</Head> </Head>
<Postcard <Postcard
src={displayPostcard} src={displayPostcard}
/> />
<WIPBanner /> <WIPBanner />
<EditingContext.Provider value={useState<EditingMode>(EditingMode.VIEW)}> <EditingContext.Provider value={useState<EditingMode>(EditingMode.VIEW)}>
<ViewEvent <ViewContent
title={<> title={
<EditableText <EditableText
value={displayTitle} value={displayTitle}
onChange={e => mutate(async state => state ? { ...state, name: e.target.value } : undefined, { revalidate: false })} onChange={e => mutate(async state => state ? { ...state, name: e.target.value } : undefined, { revalidate: false })}
/> />
</>} }
postcard={<></>} content={<>
description={<>
<EditableText <EditableText
value={displayDescription} value={displayDescription}
onChange={e => mutate(async state => state ? { ...state, description: e.target.value } : undefined, { revalidate: false })} onChange={e => mutate(async state => state ? { ...state, description: e.target.value } : undefined, { revalidate: false })}
/> />
</>} </>}
daterange={<></>}
/> />
<ToolBar vertical="vadapt" horizontal="right"> <ToolBar vertical="vadapt" horizontal="right">
<ToolToggleVisibility /> <ToolToggleVisibility />
{data && auth?.userId === data?.creatorId && {data && auth?.userId === data?.creatorId &&
<ToolToggleEditing <ToolToggleEditing
onEditEnd={saveEdits} onEditEnd={save}
/> />
} }
</ToolBar> </ToolBar>