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

More things

This commit is contained in:
Steffo 2022-06-03 04:54:09 +02:00
parent 11f7a6aa0e
commit 08636d4419
Signed by: steffo
GPG key ID: 6965406171929D01
5 changed files with 29 additions and 22 deletions

View file

@ -11,21 +11,20 @@ export function EventCreate() {
const { t } = useTranslation() const { t } = useTranslation()
const router = useRouter() const router = useRouter()
const [name, setName] = useState<string>("") const [name, setName] = useState<string>("")
const createEvent = useAxiosRequest<Event>({
method: "POST",
url: "/api/events/",
data: { name }
})
// This is a pretty bad hack... or not? const createEvent = useAxiosRequest<Event>(
// Idc, as long as it works {
useEffect(() => { method: "POST",
if (createEvent.error) return url: "/api/events/",
if (!createEvent.data) return data: { name }
router.push(`/event/${createEvent.data.slug}`) },
}) (response) => {
router.push(`/events/${response.data.slug}`)
}
)
if (createEvent.running) return <Loading text={t("eventListCreateRunning")} /> if (createEvent.running) return <Loading text={t("eventListCreateRunning")} />
if (createEvent.data) return <Loading text={t("eventListCreateRedirecting")} />
return <> return <>
<form <form

View file

@ -13,8 +13,8 @@ type ReducerState<T, D = any> = {
error: any | undefined, error: any | undefined,
} }
export function useAxiosRequest<T, D = any>(config: AxiosRequestConfig<D> = {}, hookConfig: AxiosRequestConfig<D> = {}) { export function useAxiosRequest<T, D = any>(config: AxiosRequestConfig<D> = {}, onSuccess?: (response: AxiosResponse<T>) => void, onError?: (error: any) => void) {
const axios = useAxios(hookConfig) const axios = useAxios()
const [state, dispatch] = useReducer( const [state, dispatch] = useReducer(
(prev: ReducerState<T, D>, action: ReducerAction<T, D>) => { (prev: ReducerState<T, D>, action: ReducerAction<T, D>) => {
@ -56,12 +56,14 @@ export function useAxiosRequest<T, D = any>(config: AxiosRequestConfig<D> = {},
} }
catch (error) { catch (error) {
dispatch({ type: "error", error }) dispatch({ type: "error", error })
onError?.(error)
return return
} }
dispatch({ type: "done", response }) dispatch({ type: "done", response })
onSuccess?.(response)
}, },
[axios, hookConfig] [axios]
) )
return { return {

View file

@ -1,4 +1,4 @@
import { Event } from "@prisma/client"; import { Event, User } from "@prisma/client";
import { NextPageContext } from "next"; import { NextPageContext } from "next";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import { serverSideTranslations } from "next-i18next/serverSideTranslations"; import { serverSideTranslations } from "next-i18next/serverSideTranslations";
@ -14,7 +14,10 @@ export async function getServerSideProps(context: NextPageContext) {
return {notFound: true} return {notFound: true}
} }
const event = await database.event.findUnique({where: {slug}}) const event = await database.event.findUnique({
where: {slug},
include: {creator: true}
})
if(!event) { if(!event) {
return {notFound: true} return {notFound: true}
} }
@ -29,7 +32,7 @@ export async function getServerSideProps(context: NextPageContext) {
type PageEventDetailProps = { type PageEventDetailProps = {
event: Event event: Event & {creator: User}
} }
@ -48,9 +51,11 @@ export default function PageEventDetail({event}: PageEventDetailProps) {
return ( return (
<main id="page-event-detail" className="page"> <main id="page-event-detail" className="page">
<h1> <hgroup>
{event.name} <h1>
</h1> {event.name}
</h1>
</hgroup>
</main> </main>
) )
} }

View file

@ -21,5 +21,6 @@
"eventListCreateEventNameLabel": "Nome evento", "eventListCreateEventNameLabel": "Nome evento",
"eventListCreateSubmitLabel": "Crea", "eventListCreateSubmitLabel": "Crea",
"eventListCreateRunning": "Creazione in corso...", "eventListCreateRunning": "Creazione in corso...",
"eventListCreateRedirecting": "Caricamento dell'evento in corso...",
"eventListCreateError": "Si è verificato il seguente errore nella creazione del tuo evento:" "eventListCreateError": "Si è verificato il seguente errore nella creazione del tuo evento:"
} }

View file

@ -214,6 +214,6 @@ input.negative, button.negative {
width: 100%; width: 100%;
} }
#page-event-detail h1 { #page-event-detail hgroup {
text-align: center; text-align: center;
} }