This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
website/components/Application/CreateGuild/CreateGuild.tsx

75 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-10-26 16:38:55 +02:00
import { useRouter } from 'next/router'
import useTranslation from 'next-translate/useTranslation'
import { Form, useForm } from 'react-component-form'
2021-10-26 16:38:55 +02:00
import { AxiosResponse } from 'axios'
import type { HandleUseFormCallback } from 'react-component-form'
2021-10-26 16:38:55 +02:00
import { useAuthentication } from '../../../tools/authentication'
2021-10-26 16:38:55 +02:00
import { GuildComplete, guildSchema } from '../../../models/Guild'
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'
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()
const { handleUseForm, fetchState, message, errors } = useForm(schema as any)
const { getFirstErrorTranslation } = useFormTranslation()
2021-10-26 16:38:55 +02:00
const { authentication } = useAuthentication()
const onSubmit: HandleUseFormCallback<any> = 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
const channelId = data.guild.channels[0].id
await router.push(`/application/${guildId}/${channelId}`)
return null
} catch (error) {
return {
type: 'error',
value: 'errors:server-error'
}
}
}
return (
<Main>
<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')}
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'
/>
<Button className='mt-6 w-full' type='submit' data-cy='submit'>
2021-10-26 16:38:55 +02:00
{t('application:create')}
</Button>
</Form>
<FormState
id='message'
state={fetchState}
message={message != null ? t(message) : undefined}
/>
2021-10-26 16:38:55 +02:00
</Main>
)
}