misc_pterodactyl-panel/resources/scripts/components/server/databases/CreateDatabaseButton.tsx

111 lines
4.8 KiB
TypeScript
Raw Normal View History

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';
import Field from '@/components/elements/Field';
import { object, string } from 'yup';
import { createServerDatabase } from '@/api/server/databases';
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';
interface Values {
databaseName: string;
connectionsFrom: string;
}
const schema = object().shape({
databaseName: string()
.required('A database name must be provided.')
.min(3, 'Database name must be at least 3 characters.')
.max(48, 'Database name must not exceed 48 characters.')
.matches(/^[A-Za-z0-9_\-.]{3,48}$/, 'Database name should only contain alphanumeric characters, underscores, dashes, and/or periods.'),
connectionsFrom: string()
.required('A connection value must be provided.')
.matches(/^([0-9]{1,3}|%)(\.([0-9]{1,3}|%))?(\.([0-9]{1,3}|%))?(\.([0-9]{1,3}|%))?$/, 'A valid connection address must be provided.'),
});
2019-07-10 05:41:09 +00:00
2020-04-10 17:56:25 +00:00
export default () => {
2020-08-26 05:09:54 +00:00
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
2020-04-10 17:56:25 +00:00
const { addError, clearFlashes } = useFlash();
2019-07-10 05:41:09 +00:00
const [ visible, setVisible ] = useState(false);
2020-04-10 17:56:25 +00:00
const appendDatabase = ServerContext.useStoreActions(actions => actions.databases.appendDatabase);
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');
createServerDatabase(uuid, { ...values })
.then(database => {
2020-04-10 17:56:25 +00:00
appendDatabase(database);
setVisible(false);
})
.catch(error => {
2020-04-10 17:56:25 +00:00
addError({ key: 'database:create', message: httpErrorToHuman(error) });
setSubmitting(false);
});
};
2019-07-10 05:41:09 +00:00
return (
2020-07-05 00:15:49 +00:00
<>
<Formik
onSubmit={submit}
initialValues={{ databaseName: '', connectionsFrom: '%' }}
validationSchema={schema}
>
{
({ isSubmitting, resetForm }) => (
<Modal
visible={visible}
dismissable={!isSubmitting}
showSpinnerOverlay={isSubmitting}
onDismissed={() => {
resetForm();
setVisible(false);
}}
>
2020-07-05 00:15:49 +00:00
<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
2021-09-15 17:41:02 +00:00
type={'text'}
id={'database_name'}
name={'databaseName'}
label={'Database Name'}
description={'A descriptive name for your database instance.'}
/>
2020-07-05 00:15:49 +00:00
<div css={tw`mt-6`}>
<Field
2021-09-15 17:41:02 +00:00
type={'text'}
id={'connections_from'}
name={'connectionsFrom'}
label={'Connections From'}
description={'Where connections should be allowed from. Use % for wildcards.'}
/>
</div>
2020-09-13 17:49:57 +00:00
<div css={tw`flex flex-wrap justify-end mt-6`}>
2020-07-05 00:15:49 +00:00
<Button
2019-07-17 05:15:14 +00:00
type={'button'}
2020-07-05 00:15:49 +00:00
isSecondary
2020-09-13 17:49:57 +00:00
css={tw`w-full sm:w-auto sm:mr-2`}
onClick={() => setVisible(false)}
>
Cancel
2020-07-05 00:15:49 +00:00
</Button>
2020-09-13 17:49:57 +00:00
<Button css={tw`w-full mt-4 sm:w-auto sm:mt-0`} type={'submit'}>
Create Database
2020-07-05 00:15:49 +00:00
</Button>
</div>
</Form>
</Modal>
)
}
</Formik>
2020-07-05 00:15:49 +00:00
<Button onClick={() => setVisible(true)}>
New Database
2020-07-05 00:15:49 +00:00
</Button>
</>
2019-07-10 05:41:09 +00:00
);
};