2022-12-21 21:27:50 +00:00
|
|
|
import type { FormikHelpers } from 'formik';
|
|
|
|
import { Form, Formik } from 'formik';
|
|
|
|
import { useState } from 'react';
|
|
|
|
import tw from 'twin.macro';
|
|
|
|
import { object, string } from 'yup';
|
|
|
|
|
2022-12-16 02:06:14 +00:00
|
|
|
import createNest from '@/api/admin/nests/createNest';
|
|
|
|
import getNests from '@/api/admin/nests/getNests';
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import Modal from '@/components/elements/Modal';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
|
|
|
|
interface Values {
|
2022-12-21 21:27:50 +00:00
|
|
|
name: string;
|
|
|
|
description: string;
|
2022-12-16 02:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const schema = object().shape({
|
2022-12-21 21:27:50 +00:00
|
|
|
name: string().required('A nest name must be provided.').max(32, 'Nest name must not exceed 32 characters.'),
|
|
|
|
description: string().max(255, 'Nest description must not exceed 255 characters.'),
|
2022-12-16 02:06:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default () => {
|
2022-12-21 21:27:50 +00:00
|
|
|
const [visible, setVisible] = useState(false);
|
2022-12-16 02:06:14 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
|
|
|
const { mutate } = getNests();
|
|
|
|
|
|
|
|
const submit = ({ name, description }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
|
|
|
clearFlashes('nest:create');
|
|
|
|
setSubmitting(true);
|
|
|
|
|
|
|
|
createNest(name, description)
|
2022-12-21 21:27:50 +00:00
|
|
|
.then(async nest => {
|
2022-12-16 02:06:14 +00:00
|
|
|
await mutate(data => ({ ...data!, items: data!.items.concat(nest) }), false);
|
|
|
|
setVisible(false);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
clearAndAddHttpError({ key: 'nest:create', error });
|
|
|
|
setSubmitting(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-12-21 21:27:50 +00:00
|
|
|
<Formik onSubmit={submit} initialValues={{ name: '', description: '' }} validationSchema={schema}>
|
|
|
|
{({ isSubmitting, resetForm }) => (
|
|
|
|
<Modal
|
|
|
|
visible={visible}
|
|
|
|
dismissable={!isSubmitting}
|
|
|
|
showSpinnerOverlay={isSubmitting}
|
|
|
|
onDismissed={() => {
|
|
|
|
resetForm();
|
|
|
|
setVisible(false);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<FlashMessageRender byKey={'nest:create'} css={tw`mb-6`} />
|
|
|
|
|
|
|
|
<h2 css={tw`mb-6 text-2xl text-neutral-100`}>New Nest</h2>
|
2022-12-16 02:06:14 +00:00
|
|
|
|
2022-12-21 21:27:50 +00:00
|
|
|
<Form css={tw`m-0`}>
|
|
|
|
<Field
|
|
|
|
type={'text'}
|
|
|
|
id={'name'}
|
|
|
|
name={'name'}
|
|
|
|
label={'Name'}
|
|
|
|
description={'A short name used to identify this nest.'}
|
|
|
|
autoFocus
|
|
|
|
/>
|
2022-12-16 02:06:14 +00:00
|
|
|
|
2022-12-21 21:27:50 +00:00
|
|
|
<div css={tw`mt-6`}>
|
2022-12-16 02:06:14 +00:00
|
|
|
<Field
|
|
|
|
type={'text'}
|
2022-12-21 21:27:50 +00:00
|
|
|
id={'description'}
|
|
|
|
name={'description'}
|
|
|
|
label={'Description'}
|
|
|
|
description={'A description for this nest.'}
|
2022-12-16 02:06:14 +00:00
|
|
|
/>
|
2022-12-21 21:27:50 +00:00
|
|
|
</div>
|
2022-12-16 02:06:14 +00:00
|
|
|
|
2022-12-21 21:27:50 +00:00
|
|
|
<div css={tw`flex flex-wrap justify-end mt-6`}>
|
|
|
|
<Button
|
|
|
|
type={'button'}
|
|
|
|
isSecondary
|
|
|
|
css={tw`w-full sm:w-auto sm:mr-2`}
|
|
|
|
onClick={() => setVisible(false)}
|
|
|
|
>
|
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
<Button css={tw`w-full mt-4 sm:w-auto sm:mt-0`} type={'submit'}>
|
|
|
|
Create Nest
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
)}
|
2022-12-16 02:06:14 +00:00
|
|
|
</Formik>
|
|
|
|
|
2022-12-21 21:27:50 +00:00
|
|
|
<Button
|
|
|
|
type={'button'}
|
|
|
|
size={'large'}
|
|
|
|
css={tw`h-10 px-4 py-0 whitespace-nowrap`}
|
|
|
|
onClick={() => setVisible(true)}
|
|
|
|
>
|
2022-12-16 02:06:14 +00:00
|
|
|
New Nest
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|