2019-07-10 05:41:09 +00:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import Modal from '@/components/elements/Modal';
|
2020-03-19 04:32:07 +00:00
|
|
|
import { Form, Formik, FormikHelpers } from 'formik';
|
2019-07-17 04:43:11 +00:00
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import { object, string } from 'yup';
|
2020-11-09 02:09:22 +00:00
|
|
|
import createServerDatabase from '@/api/server/databases/createServerDatabase';
|
2019-07-17 04:43:11 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
2020-04-10 17:56:25 +00:00
|
|
|
import useFlash from '@/plugins/useFlash';
|
2020-07-05 00:15:49 +00:00
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import tw from 'twin.macro';
|
2019-07-17 04:43:11 +00:00
|
|
|
|
|
|
|
interface Values {
|
|
|
|
databaseName: string;
|
|
|
|
connectionsFrom: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const schema = object().shape({
|
|
|
|
databaseName: string()
|
|
|
|
.required('A database name must be provided.')
|
2020-09-25 02:31:42 +00:00
|
|
|
.min(3, 'Database name must be at least 3 characters.')
|
|
|
|
.max(48, 'Database name must not exceed 48 characters.')
|
2022-06-26 19:13:52 +00:00
|
|
|
.matches(
|
|
|
|
/^[\w\-.]{3,48}$/,
|
|
|
|
'Database name should only contain alphanumeric characters, underscores, dashes, and/or periods.'
|
|
|
|
),
|
2022-05-07 18:17:10 +00:00
|
|
|
connectionsFrom: string().matches(/^[\w\-/.%:]+$/, 'A valid host address must be provided.'),
|
2019-07-17 04:43:11 +00:00
|
|
|
});
|
2019-07-10 05:41:09 +00:00
|
|
|
|
2020-04-10 17:56:25 +00:00
|
|
|
export default () => {
|
2022-06-26 19:13:52 +00:00
|
|
|
const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid);
|
2020-04-10 17:56:25 +00:00
|
|
|
const { addError, clearFlashes } = useFlash();
|
2022-06-26 19:13:52 +00:00
|
|
|
const [visible, setVisible] = useState(false);
|
2020-04-10 17:56:25 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const appendDatabase = ServerContext.useStoreActions((actions) => actions.databases.appendDatabase);
|
2019-07-17 04:43:11 +00:00
|
|
|
|
2020-03-19 04:32:07 +00:00
|
|
|
const submit = (values: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
2020-04-10 17:56:25 +00:00
|
|
|
clearFlashes('database:create');
|
2022-05-07 18:17:10 +00:00
|
|
|
createServerDatabase(uuid, {
|
|
|
|
databaseName: values.databaseName,
|
|
|
|
connectionsFrom: values.connectionsFrom || '%',
|
|
|
|
})
|
2022-06-26 19:13:52 +00:00
|
|
|
.then((database) => {
|
2020-04-10 17:56:25 +00:00
|
|
|
appendDatabase(database);
|
2019-07-17 04:43:11 +00:00
|
|
|
setVisible(false);
|
|
|
|
})
|
2022-06-26 19:13:52 +00:00
|
|
|
.catch((error) => {
|
2020-04-10 17:56:25 +00:00
|
|
|
addError({ key: 'database:create', message: httpErrorToHuman(error) });
|
|
|
|
setSubmitting(false);
|
|
|
|
});
|
2019-07-17 04:43:11 +00:00
|
|
|
};
|
2019-07-10 05:41:09 +00:00
|
|
|
|
|
|
|
return (
|
2020-07-05 00:15:49 +00:00
|
|
|
<>
|
2019-07-17 04:43:11 +00:00
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
2022-05-07 18:17:10 +00:00
|
|
|
initialValues={{ databaseName: '', connectionsFrom: '' }}
|
2019-07-17 04:43:11 +00:00
|
|
|
validationSchema={schema}
|
|
|
|
>
|
2022-06-26 19:13:52 +00:00
|
|
|
{({ isSubmitting, resetForm }) => (
|
|
|
|
<Modal
|
|
|
|
visible={visible}
|
|
|
|
dismissable={!isSubmitting}
|
|
|
|
showSpinnerOverlay={isSubmitting}
|
|
|
|
onDismissed={() => {
|
|
|
|
resetForm();
|
|
|
|
setVisible(false);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<FlashMessageRender byKey={'database:create'} css={tw`mb-6`} />
|
|
|
|
<h2 css={tw`text-2xl mb-6`}>Create new database</h2>
|
|
|
|
<Form css={tw`m-0`}>
|
|
|
|
<Field
|
|
|
|
type={'string'}
|
|
|
|
id={'database_name'}
|
|
|
|
name={'databaseName'}
|
|
|
|
label={'Database Name'}
|
|
|
|
description={'A descriptive name for your database instance.'}
|
|
|
|
/>
|
|
|
|
<div css={tw`mt-6`}>
|
2019-07-17 04:43:11 +00:00
|
|
|
<Field
|
|
|
|
type={'string'}
|
2022-06-26 19:13:52 +00:00
|
|
|
id={'connections_from'}
|
|
|
|
name={'connectionsFrom'}
|
|
|
|
label={'Connections From'}
|
|
|
|
description={
|
|
|
|
'Where connections should be allowed from. Leave blank to allow connections from anywhere.'
|
|
|
|
}
|
2019-07-17 04:43:11 +00:00
|
|
|
/>
|
2022-06-26 19:13:52 +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 Database
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
)}
|
2019-07-17 04:43:11 +00:00
|
|
|
</Formik>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Button onClick={() => setVisible(true)}>New Database</Button>
|
2020-07-05 00:15:49 +00:00
|
|
|
</>
|
2019-07-10 05:41:09 +00:00
|
|
|
);
|
|
|
|
};
|