2022-06-02 02:26:52 +00:00
|
|
|
import { Event } from "@prisma/client"
|
2022-06-01 16:54:59 +00:00
|
|
|
import { useTranslation } from "next-i18next"
|
2022-06-02 02:26:52 +00:00
|
|
|
import { useRouter } from "next/router"
|
2022-06-02 02:30:34 +00:00
|
|
|
import { useState } from "react"
|
2022-06-02 02:26:52 +00:00
|
|
|
import { useAxiosRequest } from "../hooks/useAxiosRequest"
|
2022-06-01 16:54:59 +00:00
|
|
|
import { Loading } from "./Loading"
|
2022-06-04 03:13:19 +00:00
|
|
|
import { ErrorBlock } from "./errors/ErrorBlock"
|
2022-06-08 00:12:03 +00:00
|
|
|
import { FestaIcon } from "./extensions/FestaIcon"
|
|
|
|
import { faPlus } from "@fortawesome/free-solid-svg-icons"
|
|
|
|
import { FormMonorow } from "./form/FormMonorow"
|
2022-06-01 16:54:59 +00:00
|
|
|
|
|
|
|
export function EventCreate() {
|
2022-06-03 01:56:43 +00:00
|
|
|
const { t } = useTranslation()
|
2022-06-02 02:26:52 +00:00
|
|
|
const router = useRouter()
|
2022-06-01 16:54:59 +00:00
|
|
|
const [name, setName] = useState<string>("")
|
|
|
|
|
2022-06-03 02:54:09 +00:00
|
|
|
const createEvent = useAxiosRequest<Event>(
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
url: "/api/events/",
|
|
|
|
data: { name }
|
|
|
|
},
|
|
|
|
(response) => {
|
|
|
|
router.push(`/events/${response.data.slug}`)
|
|
|
|
}
|
|
|
|
)
|
2022-06-01 16:54:59 +00:00
|
|
|
|
2022-06-03 01:56:43 +00:00
|
|
|
if (createEvent.running) return <Loading text={t("eventListCreateRunning")} />
|
2022-06-03 02:54:09 +00:00
|
|
|
if (createEvent.data) return <Loading text={t("eventListCreateRedirecting")} />
|
2022-06-01 16:54:59 +00:00
|
|
|
|
2022-06-02 02:26:52 +00:00
|
|
|
return <>
|
2022-06-08 00:12:03 +00:00
|
|
|
<FormMonorow
|
2022-06-03 01:56:43 +00:00
|
|
|
onSubmit={e => { e.preventDefault(); createEvent.run() }}
|
2022-06-01 16:54:59 +00:00
|
|
|
noValidate
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
placeholder={t("eventListCreateEventNameLabel")}
|
|
|
|
value={name}
|
|
|
|
onChange={e => setName(e.target.value)}
|
|
|
|
required
|
|
|
|
/>
|
2022-06-08 00:12:03 +00:00
|
|
|
<button
|
2022-06-01 16:54:59 +00:00
|
|
|
aria-label={t("eventListCreateSubmitLabel")}
|
2022-06-08 00:12:03 +00:00
|
|
|
className="positive"
|
2022-06-02 02:26:52 +00:00
|
|
|
onClick={e => createEvent.run()}
|
2022-06-01 16:54:59 +00:00
|
|
|
disabled={!name}
|
2022-06-08 00:12:03 +00:00
|
|
|
>
|
|
|
|
<FestaIcon icon={faPlus}/>
|
|
|
|
</button>
|
|
|
|
</FormMonorow>
|
2022-06-03 01:56:43 +00:00
|
|
|
{createEvent.error ? <ErrorBlock error={createEvent.error} text={t("eventListCreateError")} /> : null}
|
2022-06-02 02:26:52 +00:00
|
|
|
</>
|
2022-06-01 16:54:59 +00:00
|
|
|
}
|