2022-11-25 20:25:03 +00:00
|
|
|
import { useState } from 'react';
|
2019-07-10 05:00:29 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2020-08-26 05:09:54 +00:00
|
|
|
import { faDatabase, faEye, faTrashAlt } from '@fortawesome/free-solid-svg-icons';
|
2019-07-17 05:15:14 +00:00
|
|
|
import Modal from '@/components/elements/Modal';
|
2020-03-19 04:32:07 +00:00
|
|
|
import { Form, Formik, FormikHelpers } from 'formik';
|
2019-07-17 05:15:14 +00:00
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import { object, string } from 'yup';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import { ServerContext } from '@/state/server';
|
2020-11-09 02:09:22 +00:00
|
|
|
import deleteServerDatabase from '@/api/server/databases/deleteServerDatabase';
|
2019-07-17 05:15:14 +00:00
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2019-07-27 22:17:50 +00:00
|
|
|
import RotatePasswordButton from '@/components/server/databases/RotatePasswordButton';
|
2020-03-30 05:20:27 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
2020-11-09 02:09:22 +00:00
|
|
|
import { ServerDatabase } from '@/api/server/databases/getServerDatabases';
|
2020-04-10 17:56:25 +00:00
|
|
|
import useFlash from '@/plugins/useFlash';
|
2020-07-05 00:15:49 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import Label from '@/components/elements/Label';
|
|
|
|
import Input from '@/components/elements/Input';
|
|
|
|
import GreyRowBox from '@/components/elements/GreyRowBox';
|
2020-11-09 02:09:22 +00:00
|
|
|
import CopyOnClick from '@/components/elements/CopyOnClick';
|
2019-07-17 05:15:14 +00:00
|
|
|
|
|
|
|
interface Props {
|
2020-04-10 17:56:25 +00:00
|
|
|
database: ServerDatabase;
|
2019-07-17 05:15:14 +00:00
|
|
|
className?: string;
|
|
|
|
}
|
|
|
|
|
2020-04-10 17:56:25 +00:00
|
|
|
export default ({ database, className }: Props) => {
|
2022-11-25 20:25:03 +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);
|
|
|
|
const [connectionVisible, setConnectionVisible] = useState(false);
|
2019-07-17 05:15:14 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const appendDatabase = ServerContext.useStoreActions(actions => actions.databases.appendDatabase);
|
|
|
|
const removeDatabase = ServerContext.useStoreActions(actions => actions.databases.removeDatabase);
|
2019-07-27 22:17:50 +00:00
|
|
|
|
2022-09-17 18:50:40 +00:00
|
|
|
const jdbcConnectionString = `jdbc:mysql://${database.username}${
|
2022-11-06 22:24:33 +00:00
|
|
|
database.password ? `:${encodeURIComponent(database.password)}` : ''
|
2022-09-17 18:50:40 +00:00
|
|
|
}@${database.connectionString}/${database.name}`;
|
|
|
|
|
2019-07-17 05:15:14 +00:00
|
|
|
const schema = object().shape({
|
|
|
|
confirm: string()
|
|
|
|
.required('The database name must be provided.')
|
2022-06-26 19:13:52 +00:00
|
|
|
.oneOf([database.name.split('_', 2)[1], database.name], 'The database name must be provided.'),
|
2019-07-17 05:15:14 +00:00
|
|
|
});
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const submit = (_: { confirm: string }, { setSubmitting }: FormikHelpers<{ confirm: string }>) => {
|
2019-07-17 05:15:14 +00:00
|
|
|
clearFlashes();
|
2020-04-10 17:56:25 +00:00
|
|
|
deleteServerDatabase(uuid, database.id)
|
2019-07-17 05:15:14 +00:00
|
|
|
.then(() => {
|
|
|
|
setVisible(false);
|
2020-04-10 17:56:25 +00:00
|
|
|
setTimeout(() => removeDatabase(database.id), 150);
|
2019-07-17 05:15:14 +00:00
|
|
|
})
|
2022-11-25 20:25:03 +00:00
|
|
|
.catch(error => {
|
2019-07-17 05:15:14 +00:00
|
|
|
console.error(error);
|
|
|
|
setSubmitting(false);
|
2020-04-10 17:56:25 +00:00
|
|
|
addError({ key: 'database:delete', message: httpErrorToHuman(error) });
|
2019-07-17 05:15:14 +00:00
|
|
|
});
|
|
|
|
};
|
2020-07-05 00:15:49 +00:00
|
|
|
|
2019-07-10 05:00:29 +00:00
|
|
|
return (
|
2020-07-05 00:15:49 +00:00
|
|
|
<>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Formik onSubmit={submit} initialValues={{ confirm: '' }} validationSchema={schema} isInitialValid={false}>
|
|
|
|
{({ isSubmitting, isValid, resetForm }) => (
|
|
|
|
<Modal
|
|
|
|
visible={visible}
|
|
|
|
dismissable={!isSubmitting}
|
|
|
|
showSpinnerOverlay={isSubmitting}
|
|
|
|
onDismissed={() => {
|
|
|
|
setVisible(false);
|
|
|
|
resetForm();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<FlashMessageRender byKey={'database:delete'} css={tw`mb-6`} />
|
|
|
|
<h2 css={tw`text-2xl mb-6`}>Confirm database deletion</h2>
|
|
|
|
<p css={tw`text-sm`}>
|
|
|
|
Deleting a database is a permanent action, it cannot be undone. This will permanently delete
|
|
|
|
the <strong>{database.name}</strong> database and remove all associated data.
|
|
|
|
</p>
|
|
|
|
<Form css={tw`m-0 mt-6`}>
|
|
|
|
<Field
|
|
|
|
type={'text'}
|
|
|
|
id={'confirm_name'}
|
|
|
|
name={'confirm'}
|
|
|
|
label={'Confirm Database Name'}
|
|
|
|
description={'Enter the database name to confirm deletion.'}
|
|
|
|
/>
|
|
|
|
<div css={tw`mt-6 text-right`}>
|
|
|
|
<Button type={'button'} isSecondary css={tw`mr-2`} onClick={() => setVisible(false)}>
|
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
<Button type={'submit'} color={'red'} disabled={!isValid}>
|
|
|
|
Delete Database
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
)}
|
2019-07-17 05:15:14 +00:00
|
|
|
</Formik>
|
2019-07-17 05:29:00 +00:00
|
|
|
<Modal visible={connectionVisible} onDismissed={() => setConnectionVisible(false)}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FlashMessageRender byKey={'database-connection-modal'} css={tw`mb-6`} />
|
2020-12-06 20:01:42 +00:00
|
|
|
<h3 css={tw`mb-6 text-2xl`}>Database connection details</h3>
|
2020-09-13 17:02:25 +00:00
|
|
|
<div>
|
|
|
|
<Label>Endpoint</Label>
|
2022-06-26 19:13:52 +00:00
|
|
|
<CopyOnClick text={database.connectionString}>
|
|
|
|
<Input type={'text'} readOnly value={database.connectionString} />
|
|
|
|
</CopyOnClick>
|
2020-09-13 17:02:25 +00:00
|
|
|
</div>
|
|
|
|
<div css={tw`mt-6`}>
|
|
|
|
<Label>Connections from</Label>
|
|
|
|
<Input type={'text'} readOnly value={database.allowConnectionsFrom} />
|
|
|
|
</div>
|
|
|
|
<div css={tw`mt-6`}>
|
|
|
|
<Label>Username</Label>
|
2022-06-26 19:13:52 +00:00
|
|
|
<CopyOnClick text={database.username}>
|
|
|
|
<Input type={'text'} readOnly value={database.username} />
|
|
|
|
</CopyOnClick>
|
2020-09-13 17:02:25 +00:00
|
|
|
</div>
|
2020-03-30 05:20:27 +00:00
|
|
|
<Can action={'database.view_password'}>
|
2020-09-13 17:02:25 +00:00
|
|
|
<div css={tw`mt-6`}>
|
2020-07-05 00:15:49 +00:00
|
|
|
<Label>Password</Label>
|
2022-06-26 19:13:52 +00:00
|
|
|
<CopyOnClick text={database.password}>
|
|
|
|
<Input type={'text'} readOnly value={database.password} />
|
|
|
|
</CopyOnClick>
|
2020-03-30 05:20:27 +00:00
|
|
|
</div>
|
|
|
|
</Can>
|
2020-07-05 00:15:49 +00:00
|
|
|
<div css={tw`mt-6`}>
|
2020-12-06 18:17:47 +00:00
|
|
|
<Label>JDBC Connection String</Label>
|
2022-09-17 18:50:40 +00:00
|
|
|
<CopyOnClick text={jdbcConnectionString}>
|
|
|
|
<Input type={'text'} readOnly value={jdbcConnectionString} />
|
2020-11-09 02:09:22 +00:00
|
|
|
</CopyOnClick>
|
2019-07-17 05:29:00 +00:00
|
|
|
</div>
|
2020-07-05 00:15:49 +00:00
|
|
|
<div css={tw`mt-6 text-right`}>
|
2020-03-30 05:20:27 +00:00
|
|
|
<Can action={'database.update'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<RotatePasswordButton databaseId={database.id} onUpdate={appendDatabase} />
|
2020-03-30 05:20:27 +00:00
|
|
|
</Can>
|
2020-07-05 00:15:49 +00:00
|
|
|
<Button isSecondary onClick={() => setConnectionVisible(false)}>
|
2019-07-17 05:29:00 +00:00
|
|
|
Close
|
2020-07-05 00:15:49 +00:00
|
|
|
</Button>
|
2019-07-17 05:29:00 +00:00
|
|
|
</div>
|
|
|
|
</Modal>
|
2020-09-13 17:02:25 +00:00
|
|
|
<GreyRowBox $hoverable={false} className={className} css={tw`mb-2`}>
|
|
|
|
<div css={tw`hidden md:block`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FontAwesomeIcon icon={faDatabase} fixedWidth />
|
2019-07-17 05:15:14 +00:00
|
|
|
</div>
|
2020-07-05 00:15:49 +00:00
|
|
|
<div css={tw`flex-1 ml-4`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<CopyOnClick text={database.name}>
|
|
|
|
<p css={tw`text-lg`}>{database.name}</p>
|
|
|
|
</CopyOnClick>
|
2019-07-17 05:15:14 +00:00
|
|
|
</div>
|
2020-09-13 17:02:25 +00:00
|
|
|
<div css={tw`ml-8 text-center hidden md:block`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<CopyOnClick text={database.connectionString}>
|
|
|
|
<p css={tw`text-sm`}>{database.connectionString}</p>
|
|
|
|
</CopyOnClick>
|
2020-07-05 00:15:49 +00:00
|
|
|
<p css={tw`mt-1 text-2xs text-neutral-500 uppercase select-none`}>Endpoint</p>
|
2019-07-17 05:15:14 +00:00
|
|
|
</div>
|
2020-09-13 17:02:25 +00:00
|
|
|
<div css={tw`ml-8 text-center hidden md:block`}>
|
2020-07-05 00:15:49 +00:00
|
|
|
<p css={tw`text-sm`}>{database.allowConnectionsFrom}</p>
|
|
|
|
<p css={tw`mt-1 text-2xs text-neutral-500 uppercase select-none`}>Connections from</p>
|
2019-07-17 05:15:14 +00:00
|
|
|
</div>
|
2020-09-13 17:02:25 +00:00
|
|
|
<div css={tw`ml-8 text-center hidden md:block`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<CopyOnClick text={database.username}>
|
|
|
|
<p css={tw`text-sm`}>{database.username}</p>
|
|
|
|
</CopyOnClick>
|
2020-07-05 00:15:49 +00:00
|
|
|
<p css={tw`mt-1 text-2xs text-neutral-500 uppercase select-none`}>Username</p>
|
2019-07-17 05:15:14 +00:00
|
|
|
</div>
|
2020-07-05 00:15:49 +00:00
|
|
|
<div css={tw`ml-8`}>
|
|
|
|
<Button isSecondary css={tw`mr-2`} onClick={() => setConnectionVisible(true)}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FontAwesomeIcon icon={faEye} fixedWidth />
|
2020-07-05 00:15:49 +00:00
|
|
|
</Button>
|
2020-03-30 05:20:27 +00:00
|
|
|
<Can action={'database.delete'}>
|
2020-07-05 00:15:49 +00:00
|
|
|
<Button color={'red'} isSecondary onClick={() => setVisible(true)}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FontAwesomeIcon icon={faTrashAlt} fixedWidth />
|
2020-07-05 00:15:49 +00:00
|
|
|
</Button>
|
2020-03-30 05:20:27 +00:00
|
|
|
</Can>
|
2019-07-17 05:15:14 +00:00
|
|
|
</div>
|
2020-07-05 00:15:49 +00:00
|
|
|
</GreyRowBox>
|
|
|
|
</>
|
2019-07-10 05:00:29 +00:00
|
|
|
);
|
|
|
|
};
|