2021-07-25 21:54:16 +00:00
|
|
|
import { Form, Formik, FormikHelpers } from 'formik';
|
2020-12-28 17:09:55 +00:00
|
|
|
import React, { useState } from 'react';
|
2021-07-25 21:54:16 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import { object, string } from 'yup';
|
2020-12-28 17:08:08 +00:00
|
|
|
import createRole from '@/api/admin/roles/createRole';
|
2021-07-25 21:54:16 +00:00
|
|
|
import getRoles from '@/api/admin/roles/getRoles';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
2020-12-28 17:08:08 +00:00
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import Modal from '@/components/elements/Modal';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
|
|
|
|
interface Values {
|
|
|
|
name: string,
|
|
|
|
description: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
const schema = object().shape({
|
|
|
|
name: string()
|
|
|
|
.required('A role name must be provided.')
|
|
|
|
.max(32, 'Role name must not exceed 32 characters.'),
|
|
|
|
description: string()
|
|
|
|
.max(255, 'Role description must not exceed 255 characters.'),
|
|
|
|
});
|
|
|
|
|
|
|
|
export default () => {
|
|
|
|
const [ visible, setVisible ] = useState(false);
|
2021-01-08 17:02:49 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
2021-07-25 21:54:16 +00:00
|
|
|
const { mutate } = getRoles();
|
2020-12-28 17:08:08 +00:00
|
|
|
|
|
|
|
const submit = ({ name, description }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
|
|
|
clearFlashes('role:create');
|
|
|
|
setSubmitting(true);
|
|
|
|
|
|
|
|
createRole(name, description)
|
|
|
|
.then(role => {
|
2021-07-25 21:54:16 +00:00
|
|
|
mutate(data => ({ ...data!, items: data!.items.concat(role) }), false);
|
2020-12-28 17:08:08 +00:00
|
|
|
setVisible(false);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2021-01-08 17:02:49 +00:00
|
|
|
clearAndAddHttpError({ key: 'role:create', error });
|
2020-12-28 17:08:08 +00:00
|
|
|
setSubmitting(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{ name: '', description: '' }}
|
|
|
|
validationSchema={schema}
|
|
|
|
>
|
|
|
|
{
|
|
|
|
({ isSubmitting, resetForm }) => (
|
|
|
|
<Modal
|
|
|
|
visible={visible}
|
|
|
|
dismissable={!isSubmitting}
|
|
|
|
showSpinnerOverlay={isSubmitting}
|
|
|
|
onDismissed={() => {
|
|
|
|
resetForm();
|
|
|
|
setVisible(false);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<FlashMessageRender byKey={'role:create'} css={tw`mb-6`}/>
|
2021-02-16 20:04:10 +00:00
|
|
|
<h2 css={tw`mb-6 text-2xl text-neutral-100`}>New Role</h2>
|
2020-12-28 17:08:08 +00:00
|
|
|
<Form css={tw`m-0`}>
|
|
|
|
<Field
|
|
|
|
type={'string'}
|
|
|
|
id={'name'}
|
|
|
|
name={'name'}
|
|
|
|
label={'Name'}
|
|
|
|
description={'A short name used to identify this role.'}
|
2021-02-16 20:04:10 +00:00
|
|
|
autoFocus
|
2020-12-28 17:08:08 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
<div css={tw`mt-6`}>
|
|
|
|
<Field
|
|
|
|
type={'string'}
|
|
|
|
id={'description'}
|
|
|
|
name={'description'}
|
|
|
|
label={'Description'}
|
2020-12-28 17:09:55 +00:00
|
|
|
description={'A description for this role.'}
|
2020-12-28 17:08:08 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<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 Role
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</Formik>
|
|
|
|
|
2021-01-10 18:34:14 +00:00
|
|
|
<Button type={'button'} size={'large'} css={tw`h-10 px-4 py-0 whitespace-nowrap`} onClick={() => setVisible(true)}>
|
2020-12-28 17:08:08 +00:00
|
|
|
New Role
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|