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-02 02:26:52 +00:00
|
|
|
import { useEffect } from "react"
|
2022-06-04 03:13:19 +00:00
|
|
|
import { ErrorBlock } from "./errors/ErrorBlock"
|
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-03 01:56:43 +00:00
|
|
|
<form
|
2022-06-01 16:54:59 +00:00
|
|
|
className="form-monorow"
|
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
|
|
|
|
/>
|
|
|
|
<input
|
|
|
|
type="submit"
|
|
|
|
aria-label={t("eventListCreateSubmitLabel")}
|
|
|
|
className="positive square-40"
|
|
|
|
value="→"
|
2022-06-02 02:26:52 +00:00
|
|
|
onClick={e => createEvent.run()}
|
2022-06-01 16:54:59 +00:00
|
|
|
disabled={!name}
|
|
|
|
/>
|
|
|
|
</form>
|
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
|
|
|
}
|