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"
|
|
|
|
import { ErrorBlock } from "./ErrorBlock"
|
2022-06-01 16:54:59 +00:00
|
|
|
|
|
|
|
export function EventCreate() {
|
|
|
|
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-02 02:26:52 +00:00
|
|
|
const createEvent = useAxiosRequest<Event>({
|
|
|
|
method: "POST",
|
|
|
|
url: "/api/events/",
|
|
|
|
data: {name}
|
|
|
|
})
|
2022-06-01 16:54:59 +00:00
|
|
|
|
2022-06-02 02:26:52 +00:00
|
|
|
// This is a pretty bad hack... or not?
|
|
|
|
// Idc, as long as it works
|
|
|
|
useEffect(() => {
|
|
|
|
if(createEvent.error) return
|
|
|
|
if(!createEvent.data) return
|
|
|
|
router.push(`/event/${createEvent.data.slug}`)
|
|
|
|
})
|
2022-06-01 16:54:59 +00:00
|
|
|
|
2022-06-02 02:26:52 +00:00
|
|
|
if(createEvent.running) return <Loading text={t("eventListCreateRunning")}/>
|
2022-06-01 16:54:59 +00:00
|
|
|
|
2022-06-02 02:26:52 +00:00
|
|
|
return <>
|
2022-06-01 16:54:59 +00:00
|
|
|
<form
|
|
|
|
className="form-monorow"
|
2022-06-02 02:26:52 +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-02 02:26:52 +00:00
|
|
|
{createEvent.error ? <ErrorBlock error={createEvent.error}/> : null}
|
|
|
|
</>
|
2022-06-01 16:54:59 +00:00
|
|
|
}
|