2021-01-06 22:39:23 +00:00
|
|
|
import React from 'react';
|
|
|
|
import tw from 'twin.macro';
|
|
|
|
import AdminContentBlock from '@/components/admin/AdminContentBlock';
|
2021-07-24 18:23:33 +00:00
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
2021-09-12 23:00:22 +00:00
|
|
|
import UserForm from '@/components/admin/users/UserForm';
|
2021-07-24 18:23:33 +00:00
|
|
|
import { useHistory } from 'react-router-dom';
|
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import { FormikHelpers } from 'formik';
|
2022-02-27 20:26:26 +00:00
|
|
|
import { createUser, UpdateUserValues } from '@/api/admin/users';
|
2021-01-06 22:39:23 +00:00
|
|
|
|
|
|
|
export default () => {
|
2021-07-24 18:23:33 +00:00
|
|
|
const history = useHistory();
|
|
|
|
|
|
|
|
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
|
|
|
2022-02-27 20:26:26 +00:00
|
|
|
const submit = (values: UpdateUserValues, { setSubmitting }: FormikHelpers<UpdateUserValues>) => {
|
2021-07-24 18:23:33 +00:00
|
|
|
clearFlashes('user:create');
|
|
|
|
|
|
|
|
createUser(values)
|
|
|
|
.then(user => history.push(`/admin/users/${user.id}`))
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
clearAndAddHttpError({ key: 'user:create', error });
|
|
|
|
})
|
|
|
|
.then(() => setSubmitting(false));
|
|
|
|
};
|
|
|
|
|
2021-01-06 22:39:23 +00:00
|
|
|
return (
|
2021-01-10 18:34:14 +00:00
|
|
|
<AdminContentBlock title={'New User'}>
|
2021-01-06 22:39:23 +00:00
|
|
|
<div css={tw`w-full flex flex-row items-center mb-8`}>
|
2021-01-10 18:34:14 +00:00
|
|
|
<div css={tw`flex flex-col flex-shrink`} style={{ minWidth: '0' }}>
|
2021-09-17 19:06:31 +00:00
|
|
|
<h2 css={tw`text-2xl text-neutral-50 font-header font-medium`}>New User</h2>
|
2021-01-10 18:34:14 +00:00
|
|
|
<p css={tw`text-base text-neutral-400 whitespace-nowrap overflow-ellipsis overflow-hidden`}>Add a new user to the panel.</p>
|
2021-01-06 22:39:23 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-07-24 18:23:33 +00:00
|
|
|
|
|
|
|
<FlashMessageRender byKey={'user:create'} css={tw`mb-4`}/>
|
|
|
|
|
2021-09-12 23:00:22 +00:00
|
|
|
<UserForm title={'Create User'} onSubmit={submit} role={null}/>
|
2021-01-06 22:39:23 +00:00
|
|
|
</AdminContentBlock>
|
|
|
|
);
|
|
|
|
};
|