2019-07-17 05:15:14 +00:00
|
|
|
import React, { 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) => {
|
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();
|
2020-03-30 05:20:27 +00:00
|
|
|
const [ visible, setVisible ] = useState(false);
|
|
|
|
const [ connectionVisible, setConnectionVisible ] = useState(false);
|
2019-07-17 05:15:14 +00:00
|
|
|
|
2020-04-10 17:56:25 +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
|
|
|
|
2019-07-17 05:15:14 +00:00
|
|
|
const schema = object().shape({
|
|
|
|
confirm: string()
|
|
|
|
.required('The database name must be provided.')
|
2020-03-30 05:20:27 +00:00
|
|
|
.oneOf([ database.name.split('_', 2)[1], database.name ], 'The database name must be provided.'),
|
2019-07-17 05:15:14 +00:00
|
|
|
});
|
|
|
|
|
2020-03-19 04:32:07 +00:00
|
|
|
const submit = (values: { 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
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
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
|
|
|
<>
|
2019-07-17 05:15:14 +00:00
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{ confirm: '' }}
|
|
|
|
validationSchema={schema}
|
2020-07-05 00:15:49 +00:00
|
|
|
isInitialValid={false}
|
2019-07-17 05:15:14 +00:00
|
|
|
>
|
|
|
|
{
|
|
|
|
({ isSubmitting, isValid, resetForm }) => (
|
|
|
|
<Modal
|
|
|
|
visible={visible}
|
|
|
|
dismissable={!isSubmitting}
|
|
|
|
showSpinnerOverlay={isSubmitting}
|
2020-03-30 05:20:27 +00:00
|
|
|
onDismissed={() => {
|
|
|
|
setVisible(false);
|
|
|
|
resetForm();
|
|
|
|
}}
|
2019-07-17 05:15:14 +00:00
|
|
|
>
|
2020-07-05 00:15:49 +00:00
|
|
|
<FlashMessageRender byKey={'database:delete'} css={tw`mb-6`}/>
|
|
|
|
<h2 css={tw`text-2xl mb-6`}>Confirm database deletion</h2>
|
|
|
|
<p css={tw`text-sm`}>
|
2019-07-17 05:15:14 +00:00
|
|
|
Deleting a database is a permanent action, it cannot be undone. This will permanetly
|
|
|
|
delete the <strong>{database.name}</strong> database and remove all associated data.
|
|
|
|
</p>
|
2020-07-05 00:15:49 +00:00
|
|
|
<Form css={tw`m-0 mt-6`}>
|
2019-07-17 05:15:14 +00:00
|
|
|
<Field
|
|
|
|
type={'text'}
|
|
|
|
id={'confirm_name'}
|
|
|
|
name={'confirm'}
|
|
|
|
label={'Confirm Database Name'}
|
|
|
|
description={'Enter the database name to confirm deletion.'}
|
|
|
|
/>
|
2020-07-05 00:15:49 +00:00
|
|
|
<div css={tw`mt-6 text-right`}>
|
|
|
|
<Button
|
2019-07-17 05:15:14 +00:00
|
|
|
type={'button'}
|
2020-07-05 00:15:49 +00:00
|
|
|
isSecondary
|
|
|
|
css={tw`mr-2`}
|
2019-07-17 05:15:14 +00:00
|
|
|
onClick={() => setVisible(false)}
|
|
|
|
>
|
|
|
|
Cancel
|
2020-07-05 00:15:49 +00:00
|
|
|
</Button>
|
|
|
|
<Button
|
2019-07-17 05:15:14 +00:00
|
|
|
type={'submit'}
|
2020-07-05 00:15:49 +00:00
|
|
|
color={'red'}
|
2019-07-17 05:15:14 +00:00
|
|
|
disabled={!isValid}
|
|
|
|
>
|
|
|
|
Delete Database
|
2020-07-05 00:15:49 +00:00
|
|
|
</Button>
|
2019-07-17 05:15:14 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</Formik>
|
2019-07-17 05:29:00 +00:00
|
|
|
<Modal visible={connectionVisible} onDismissed={() => setConnectionVisible(false)}>
|
2020-07-05 00:15:49 +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>
|
2020-11-09 02:09:22 +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>
|
2020-11-09 02:09:22 +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>
|
2020-12-09 05:42:56 +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>
|
2020-11-09 02:09:22 +00:00
|
|
|
<CopyOnClick text={`jdbc:mysql://${database.username}:${database.password}@${database.connectionString}/${database.name}`}>
|
|
|
|
<Input
|
|
|
|
type={'text'}
|
|
|
|
readOnly
|
|
|
|
value={`jdbc:mysql://${database.username}:${database.password}@${database.connectionString}/${database.name}`}
|
|
|
|
/>
|
|
|
|
</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'}>
|
|
|
|
<RotatePasswordButton databaseId={database.id} onUpdate={appendDatabase}/>
|
|
|
|
</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`}>
|
2020-07-05 00:15:49 +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`}>
|
2020-11-09 02:09:22 +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`}>
|
2020-11-09 02:09:22 +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`}>
|
2020-11-09 02:09:22 +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)}>
|
|
|
|
<FontAwesomeIcon icon={faEye} fixedWidth/>
|
|
|
|
</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)}>
|
|
|
|
<FontAwesomeIcon icon={faTrashAlt} fixedWidth/>
|
|
|
|
</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
|
|
|
);
|
|
|
|
};
|