mirror of
https://github.com/Steffo99/festa.git
synced 2024-12-22 14:44:21 +00:00
Capture onSubmit to save the form
This commit is contained in:
parent
8f934e70f2
commit
2e69a0f963
4 changed files with 56 additions and 46 deletions
|
@ -1,20 +1,28 @@
|
|||
import { Event } from "@prisma/client"
|
||||
import { useCallback, useMemo } from "react"
|
||||
import { Dispatch, useMemo } from "react"
|
||||
import { KeyedMutator } from "swr"
|
||||
import { UsePromise } from "../../generic/loading/promise"
|
||||
import { ViewContent } from "../../generic/views/content"
|
||||
|
||||
|
||||
export type EventsActionViewProps = {
|
||||
data: Event,
|
||||
mutate: KeyedMutator<Event>,
|
||||
save: UsePromise<void, void>,
|
||||
setEditing: Dispatch<boolean>,
|
||||
}
|
||||
|
||||
|
||||
export const EventsActionEdit = ({ data, mutate }: EventsActionViewProps) => {
|
||||
export const EventsActionEdit = ({ data, mutate, save, setEditing }: EventsActionViewProps) => {
|
||||
const name = data.name
|
||||
const description = data.description
|
||||
|
||||
return (
|
||||
<form onSubmit={e => {
|
||||
e.preventDefault()
|
||||
save.run()
|
||||
setEditing(false)
|
||||
}}>
|
||||
<ViewContent
|
||||
title={
|
||||
useMemo(
|
||||
|
@ -41,5 +49,6 @@ export const EventsActionEdit = ({ data, mutate }: EventsActionViewProps) => {
|
|||
)}
|
||||
</>}
|
||||
/>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ export type EventsActionViewProps = {
|
|||
|
||||
export const EventsActionView = memo(({ data }: EventsActionViewProps) => {
|
||||
return (
|
||||
<form>
|
||||
<ViewContent
|
||||
title={<div style={{ padding: "10px" }}>
|
||||
{data.name}
|
||||
|
@ -19,6 +20,7 @@ export const EventsActionView = memo(({ data }: EventsActionViewProps) => {
|
|||
<FestaMarkdownRenderer code={data.description} />
|
||||
</div>}
|
||||
/>
|
||||
</form>
|
||||
)
|
||||
})
|
||||
EventsActionView.displayName = "EventsActionView"
|
|
@ -1,6 +1,6 @@
|
|||
import { faAsterisk, faPencil, faSave } from "@fortawesome/free-solid-svg-icons"
|
||||
import { useTranslation } from "next-i18next"
|
||||
import { usePromise, UsePromiseStatus } from "../../generic/loading/promise"
|
||||
import { UsePromise, usePromise, UsePromiseStatus } from "../../generic/loading/promise"
|
||||
import { FestaIcon } from "../../generic/renderers/fontawesome"
|
||||
import { Tool } from "../../generic/toolbar/tool"
|
||||
import cursor from "../../../styles/cursor.module.css"
|
||||
|
@ -11,19 +11,17 @@ import { Dispatch } from "react"
|
|||
export type ToolToggleEditingProps = {
|
||||
editing: boolean,
|
||||
setEditing: Dispatch<boolean>,
|
||||
save: () => Promise<void>,
|
||||
save: UsePromise<void, void>,
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ToolBar {@link Tool} which allows the user to start editing an event and then save their changes.
|
||||
*/
|
||||
export function ToolToggleEditing({ editing, setEditing, save: upperSave }: ToolToggleEditingProps) {
|
||||
export function ToolToggleEditing({ editing, setEditing, save }: ToolToggleEditingProps) {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const { run: save, status: saveStatus } = usePromise<void, void>(upperSave)
|
||||
|
||||
if (saveStatus === UsePromiseStatus.PENDING) {
|
||||
if (save.status === UsePromiseStatus.PENDING) {
|
||||
return (
|
||||
<Tool
|
||||
aria-label={t("toolToggleEditingSaving")}
|
||||
|
@ -39,7 +37,7 @@ export function ToolToggleEditing({ editing, setEditing, save: upperSave }: Tool
|
|||
<Tool
|
||||
aria-label={t("toolToggleEditingSave")}
|
||||
onClick={() => {
|
||||
save()
|
||||
save.run()
|
||||
setEditing(false)
|
||||
}}
|
||||
className={mood.positive}
|
||||
|
|
|
@ -17,6 +17,7 @@ import { useAxios } from '../../components/auth/requests'
|
|||
import { database } from '../../utils/prismaClient'
|
||||
import { EventsActionEdit } from '../../components/events/actions/edit'
|
||||
import { EventsActionView } from '../../components/events/actions/view'
|
||||
import { usePromise } from '../../components/generic/loading/promise'
|
||||
|
||||
|
||||
export async function getServerSideProps(context: NextPageContext) {
|
||||
|
@ -46,13 +47,13 @@ const PageEvent: NextPage<PageEventProps> = ({ slug, fallbackData }) => {
|
|||
const { data, mutate } = useSWR<Event>(`/api/events/${slug}`, { fallbackData, revalidateOnFocus: eventEditing, revalidateOnReconnect: eventEditing, refreshInterval: eventEditing ? 0 : 30000 })
|
||||
const [auth,] = useDefinedContext(AuthContext)
|
||||
|
||||
const save = useCallback(
|
||||
const save = usePromise<void, void>(useCallback(
|
||||
async () => {
|
||||
const response = await axios.patch<Event>(`/api/events/${slug}`, data!)
|
||||
mutate(response.data, { revalidate: false })
|
||||
},
|
||||
[axios, data, mutate, slug]
|
||||
)
|
||||
))
|
||||
|
||||
const eventName = data?.name ?? slug
|
||||
const eventPostcard = data?.postcard || defaultPostcard
|
||||
|
@ -75,7 +76,7 @@ const PageEvent: NextPage<PageEventProps> = ({ slug, fallbackData }) => {
|
|||
}
|
||||
</ToolBar>
|
||||
{eventEditing ?
|
||||
<EventsActionEdit data={data!} mutate={mutate} />
|
||||
<EventsActionEdit data={data!} mutate={mutate} save={save} setEditing={eventSetEditing} />
|
||||
:
|
||||
<EventsActionView data={data!} />
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue