2021-10-26 16:38:55 +02:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import useTranslation from 'next-translate/useTranslation'
|
2022-08-28 18:26:56 +02:00
|
|
|
import { Form, useForm } from 'react-component-form'
|
2022-08-31 21:44:33 +02:00
|
|
|
import type { AxiosResponse } from 'axios'
|
2022-08-28 18:26:56 +02:00
|
|
|
import type { HandleUseFormCallback } from 'react-component-form'
|
2021-10-26 16:38:55 +02:00
|
|
|
|
2022-01-01 20:42:25 +01:00
|
|
|
import { useAuthentication } from '../../../tools/authentication'
|
2022-08-31 21:44:33 +02:00
|
|
|
import type { GuildComplete } from '../../../models/Guild'
|
|
|
|
import { guildSchema } from '../../../models/Guild'
|
2021-10-26 16:38:55 +02:00
|
|
|
import { Input } from '../../design/Input'
|
|
|
|
import { Main } from '../../design/Main'
|
|
|
|
import { Button } from '../../design/Button'
|
|
|
|
import { FormState } from '../../design/FormState'
|
2022-01-14 23:15:51 +01:00
|
|
|
import { Textarea } from '../../design/Textarea'
|
2022-08-28 18:26:56 +02:00
|
|
|
import { useFormTranslation } from '../../../hooks/useFormTranslation'
|
|
|
|
|
|
|
|
const schema = {
|
|
|
|
name: guildSchema.name,
|
|
|
|
description: guildSchema.description
|
|
|
|
}
|
2021-10-26 16:38:55 +02:00
|
|
|
|
|
|
|
export const CreateGuild: React.FC = () => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const router = useRouter()
|
|
|
|
|
2022-09-21 10:09:36 +02:00
|
|
|
const { handleUseForm, fetchState, message, errors } = useForm(schema)
|
2022-08-28 18:26:56 +02:00
|
|
|
const { getFirstErrorTranslation } = useFormTranslation()
|
2021-10-26 16:38:55 +02:00
|
|
|
|
|
|
|
const { authentication } = useAuthentication()
|
|
|
|
|
2022-09-21 10:09:36 +02:00
|
|
|
const onSubmit: HandleUseFormCallback<typeof schema> = async (formData) => {
|
2021-10-26 16:38:55 +02:00
|
|
|
try {
|
|
|
|
const { data } = await authentication.api.post<
|
|
|
|
any,
|
|
|
|
AxiosResponse<{ guild: GuildComplete }>
|
|
|
|
>('/guilds', { name: formData.name, description: formData.description })
|
|
|
|
const guildId = data.guild.id
|
2023-01-11 17:39:09 +01:00
|
|
|
const channel = data.guild.channels[0]
|
|
|
|
if (channel == null) {
|
|
|
|
throw new Error('No channel found')
|
|
|
|
}
|
|
|
|
const channelId = channel.id
|
2021-10-26 16:38:55 +02:00
|
|
|
await router.push(`/application/${guildId}/${channelId}`)
|
|
|
|
return null
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
type: 'error',
|
2023-04-03 00:11:19 +02:00
|
|
|
message: 'errors:server-error'
|
2021-10-26 16:38:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Main>
|
2022-08-28 18:26:56 +02:00
|
|
|
<Form className='w-4/6 max-w-xs' onSubmit={handleUseForm(onSubmit)}>
|
2021-10-26 16:38:55 +02:00
|
|
|
<Input
|
|
|
|
type='text'
|
|
|
|
placeholder={t('common:name')}
|
|
|
|
name='name'
|
|
|
|
label={t('common:name')}
|
2022-08-28 18:26:56 +02:00
|
|
|
error={getFirstErrorTranslation(errors.name)}
|
2021-10-26 16:38:55 +02:00
|
|
|
/>
|
2022-01-14 23:15:51 +01:00
|
|
|
<Textarea
|
|
|
|
label='Description'
|
|
|
|
placeholder='Description'
|
|
|
|
id='description'
|
|
|
|
/>
|
2022-02-19 23:20:33 +01:00
|
|
|
<Button className='mt-6 w-full' type='submit' data-cy='submit'>
|
2021-10-26 16:38:55 +02:00
|
|
|
{t('application:create')}
|
|
|
|
</Button>
|
|
|
|
</Form>
|
2022-08-28 18:26:56 +02:00
|
|
|
<FormState
|
|
|
|
id='message'
|
|
|
|
state={fetchState}
|
|
|
|
message={message != null ? t(message) : undefined}
|
|
|
|
/>
|
2021-10-26 16:38:55 +02:00
|
|
|
</Main>
|
|
|
|
)
|
|
|
|
}
|