2023-10-23 23:33:39 +02:00
|
|
|
import { useRouter } from "next/router"
|
|
|
|
import useTranslation from "next-translate/useTranslation"
|
|
|
|
import { Form, useForm } from "react-component-form"
|
|
|
|
import type { HandleUseFormCallback } from "react-component-form"
|
2022-03-05 18:22:30 +01:00
|
|
|
|
2023-10-23 23:33:39 +02:00
|
|
|
import { useAuthentication } from "../../../tools/authentication"
|
|
|
|
import { Input } from "../../design/Input"
|
|
|
|
import { Main } from "../../design/Main"
|
|
|
|
import { Button } from "../../design/Button"
|
|
|
|
import { FormState } from "../../design/FormState"
|
|
|
|
import type { Channel } from "../../../models/Channel"
|
|
|
|
import { channelSchema } from "../../../models/Channel"
|
|
|
|
import { useGuildMember } from "../../../contexts/GuildMember"
|
|
|
|
import { useFormTranslation } from "../../../hooks/useFormTranslation"
|
2022-08-28 18:26:56 +02:00
|
|
|
|
|
|
|
const schema = {
|
2023-10-23 23:33:39 +02:00
|
|
|
name: channelSchema.name,
|
2022-08-28 18:26:56 +02:00
|
|
|
}
|
2022-03-05 18:22:30 +01:00
|
|
|
|
|
|
|
export const CreateChannel: React.FC = () => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const router = useRouter()
|
|
|
|
const { guild } = useGuildMember()
|
|
|
|
|
2022-08-28 18:26:56 +02:00
|
|
|
const { handleUseForm, fetchState, message, errors } = useForm(schema)
|
|
|
|
const { getFirstErrorTranslation } = useFormTranslation()
|
2022-03-05 18:22:30 +01:00
|
|
|
|
|
|
|
const { authentication } = useAuthentication()
|
|
|
|
|
2022-08-28 18:26:56 +02:00
|
|
|
const onSubmit: HandleUseFormCallback<typeof schema> = async (formData) => {
|
2022-03-05 18:22:30 +01:00
|
|
|
try {
|
|
|
|
const { data: channel } = await authentication.api.post<Channel>(
|
|
|
|
`/guilds/${guild.id}/channels`,
|
2023-10-23 23:33:39 +02:00
|
|
|
formData,
|
2022-03-05 18:22:30 +01:00
|
|
|
)
|
|
|
|
await router.push(`/application/${guild.id}/${channel.id}`)
|
|
|
|
return null
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
2023-10-23 23:33:39 +02:00
|
|
|
type: "error",
|
|
|
|
message: "errors:server-error",
|
2022-03-05 18:22:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Main>
|
2023-10-23 23:33:39 +02:00
|
|
|
<Form className="w-4/6 max-w-xs" onSubmit={handleUseForm(onSubmit)}>
|
2022-03-05 18:22:30 +01:00
|
|
|
<Input
|
2023-10-23 23:33:39 +02:00
|
|
|
type="text"
|
|
|
|
placeholder={t("common:name")}
|
|
|
|
name="name"
|
|
|
|
label={t("common:name")}
|
2022-08-28 18:26:56 +02:00
|
|
|
error={getFirstErrorTranslation(errors.name)}
|
2023-10-23 23:33:39 +02:00
|
|
|
data-cy="channel-name-input"
|
2022-03-05 18:22:30 +01:00
|
|
|
/>
|
|
|
|
<Button
|
2023-10-23 23:33:39 +02:00
|
|
|
className="mt-6 w-full"
|
|
|
|
type="submit"
|
|
|
|
data-cy="button-create-channel"
|
2022-03-05 18:22:30 +01:00
|
|
|
>
|
2023-10-23 23:33:39 +02:00
|
|
|
{t("application:create")}
|
2022-03-05 18:22:30 +01:00
|
|
|
</Button>
|
|
|
|
</Form>
|
2023-10-23 23:33:39 +02:00
|
|
|
<FormState id="message" state={fetchState} message={message} />
|
2022-03-05 18:22:30 +01:00
|
|
|
</Main>
|
|
|
|
)
|
|
|
|
}
|