1
0
ohmyform/ui/pages/admin/forms/create.tsx
Michael Schramm 9c4c325e5a
Switch to single branch (#221)
* remove submodules
* add api and ui files
* update github actions
* use sparse checkout
* update node setup
* update checkout
* update docker
* change permissions
* update mariadb health check
* update changelog
2023-12-02 19:22:40 +01:00

132 lines
3.3 KiB
TypeScript

import { Button, Form, Input, message, Tabs } from 'antd'
import { cleanInput } from 'components/clean.input'
import { BaseDataTab } from 'components/form/admin/base.data.tab'
import Structure from 'components/structure'
import { withAuth } from 'components/with.auth'
import { FormFragment } from 'graphql/fragment/form.fragment'
import { useFormCreateMutation } from 'graphql/mutation/form.create.mutation'
import { NextPage } from 'next'
import { useRouter } from 'next/router'
import React, { useState } from 'react'
import { useTranslation } from 'react-i18next'
interface FormData {
form: FormFragment
}
const Create: NextPage = () => {
const { t } = useTranslation()
const router = useRouter()
const [form] = Form.useForm<FormData>()
const [saving, setSaving] = useState(false)
const [create] = useFormCreateMutation()
const save = async (formData: FormData) => {
setSaving(true)
try {
const next = (
await create({
variables: cleanInput(formData),
})
).data
await message.success(t('form:created'))
await router.replace('/admin/forms/[id]', `/admin/forms/${next.form.id}`)
} catch (e) {
console.error('failed to save', e)
await message.error(t('form:creationError'))
}
setSaving(false)
}
return (
<Structure
loading={saving}
title={t('form:create')}
selected={'forms'}
breadcrumbs={[
{ href: '/admin', name: t('admin:home') },
{ href: '/admin/forms', name: t('admin:forms') },
]}
extra={[
<Button key={'create'} onClick={form.submit} type={'primary'}>
{t('form:createNow')}
</Button>,
]}
style={{ paddingTop: 0 }}
>
<Form
form={form}
onFinish={save}
onFinishFailed={async () => {
// TODO process errors
await message.error(t('validation:mandatoryFieldsMissing'))
}}
labelCol={{
xs: { span: 24 },
sm: { span: 6 },
}}
wrapperCol={{
xs: { span: 24 },
sm: { span: 18 },
}}
>
<Form.Item noStyle name={['form', 'id']}>
<Input type={'hidden'} />
</Form.Item>
<Form.Item
noStyle
name={[
'form',
'layout',
]}
initialValue={'card'}
>
<Input type={'hidden'} />
</Form.Item>
<Form.Item
noStyle
name={[
'form', 'endPage', 'show',
]}
initialValue={true}
>
<Input type={'hidden'} />
</Form.Item>
<Form.Item
noStyle
name={[
'form', 'endPage', 'title',
]}
initialValue={t('form:submitted')}
>
<Input type={'hidden'} />
</Form.Item>
<Form.Item
noStyle
label={t('form:endPage.continueButtonText')}
name={[
'form', 'endPage', 'buttonText',
]}
initialValue={t('form:restart')}
>
<Input type={'hidden'} />
</Form.Item>
<Tabs>
<BaseDataTab key={'base_data'} tab={t('form:baseDataTab')} />
</Tabs>
</Form>
</Structure>
)
}
export default withAuth(Create, ['admin'])