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';
|
|
|
|
import { faDatabase } from '@fortawesome/free-solid-svg-icons/faDatabase';
|
|
|
|
import { faTrashAlt } from '@fortawesome/free-solid-svg-icons/faTrashAlt';
|
|
|
|
import { faEye } from '@fortawesome/free-solid-svg-icons/faEye';
|
2019-07-17 04:43:11 +00:00
|
|
|
import classNames from 'classnames';
|
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 { Actions, useStoreActions } from 'easy-peasy';
|
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import deleteServerDatabase from '@/api/server/deleteServerDatabase';
|
|
|
|
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';
|
2019-07-17 05:15:14 +00:00
|
|
|
|
|
|
|
interface Props {
|
2019-07-27 22:17:50 +00:00
|
|
|
databaseId: string | number;
|
2019-07-17 05:15:14 +00:00
|
|
|
className?: string;
|
|
|
|
onDelete: () => void;
|
|
|
|
}
|
|
|
|
|
2019-07-27 22:17:50 +00:00
|
|
|
export default ({ databaseId, className, onDelete }: Props) => {
|
2020-03-30 05:20:27 +00:00
|
|
|
const [ visible, setVisible ] = useState(false);
|
2019-07-27 22:17:50 +00:00
|
|
|
const database = ServerContext.useStoreState(state => state.databases.items.find(item => item.id === databaseId));
|
|
|
|
const appendDatabase = ServerContext.useStoreActions(actions => actions.databases.appendDatabase);
|
2020-03-30 05:20:27 +00:00
|
|
|
const [ connectionVisible, setConnectionVisible ] = useState(false);
|
2019-07-17 05:15:14 +00:00
|
|
|
const { addFlash, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
|
|
const server = ServerContext.useStoreState(state => state.server.data!);
|
|
|
|
|
2019-07-27 22:17:50 +00:00
|
|
|
if (!database) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
deleteServerDatabase(server.uuid, database.id)
|
|
|
|
.then(() => {
|
|
|
|
setVisible(false);
|
|
|
|
setTimeout(() => onDelete(), 150);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
setSubmitting(false);
|
|
|
|
addFlash({
|
|
|
|
key: 'delete-database-modal',
|
|
|
|
type: 'error',
|
|
|
|
title: 'Error',
|
|
|
|
message: httpErrorToHuman(error),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2019-07-10 05:00:29 +00:00
|
|
|
|
|
|
|
return (
|
2019-07-17 05:15:14 +00:00
|
|
|
<React.Fragment>
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{ confirm: '' }}
|
|
|
|
validationSchema={schema}
|
|
|
|
>
|
|
|
|
{
|
|
|
|
({ 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
|
|
|
>
|
|
|
|
<FlashMessageRender byKey={'delete-database-modal'} className={'mb-6'}/>
|
|
|
|
<h3 className={'mb-6'}>Confirm database deletion</h3>
|
|
|
|
<p className={'text-sm'}>
|
|
|
|
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>
|
|
|
|
<Form className={'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 className={'mt-6 text-right'}>
|
|
|
|
<button
|
|
|
|
type={'button'}
|
|
|
|
className={'btn btn-sm btn-secondary mr-2'}
|
|
|
|
onClick={() => setVisible(false)}
|
|
|
|
>
|
|
|
|
Cancel
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type={'submit'}
|
|
|
|
className={'btn btn-sm btn-red'}
|
|
|
|
disabled={!isValid}
|
|
|
|
>
|
|
|
|
Delete Database
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</Formik>
|
2019-07-17 05:29:00 +00:00
|
|
|
<Modal visible={connectionVisible} onDismissed={() => setConnectionVisible(false)}>
|
2019-07-27 22:17:50 +00:00
|
|
|
<FlashMessageRender byKey={'database-connection-modal'} className={'mb-6'}/>
|
2019-07-17 05:29:00 +00:00
|
|
|
<h3 className={'mb-6'}>Database connection details</h3>
|
2020-03-30 05:20:27 +00:00
|
|
|
<Can action={'database.view_password'}>
|
|
|
|
<div>
|
|
|
|
<label className={'input-dark-label'}>Password</label>
|
|
|
|
<input type={'text'} className={'input-dark'} readOnly={true} value={database.password}/>
|
|
|
|
</div>
|
|
|
|
</Can>
|
2019-07-17 05:29:00 +00:00
|
|
|
<div className={'mt-6'}>
|
|
|
|
<label className={'input-dark-label'}>JBDC Connection String</label>
|
|
|
|
<input
|
|
|
|
type={'text'}
|
|
|
|
className={'input-dark'}
|
|
|
|
readOnly={true}
|
|
|
|
value={`jdbc:mysql://${database.username}:${database.password}@${database.connectionString}/${database.name}`}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className={'mt-6 text-right'}>
|
2020-03-30 05:20:27 +00:00
|
|
|
<Can action={'database.update'}>
|
|
|
|
<RotatePasswordButton databaseId={database.id} onUpdate={appendDatabase}/>
|
|
|
|
</Can>
|
2019-07-17 05:29:00 +00:00
|
|
|
<button className={'btn btn-sm btn-secondary'} onClick={() => setConnectionVisible(false)}>
|
|
|
|
Close
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Modal>
|
2019-07-17 05:15:14 +00:00
|
|
|
<div className={classNames('grey-row-box no-hover', className)}>
|
|
|
|
<div className={'icon'}>
|
2020-02-08 23:23:08 +00:00
|
|
|
<FontAwesomeIcon icon={faDatabase} fixedWidth={true}/>
|
2019-07-17 05:15:14 +00:00
|
|
|
</div>
|
|
|
|
<div className={'flex-1 ml-4'}>
|
|
|
|
<p className={'text-lg'}>{database.name}</p>
|
|
|
|
</div>
|
2020-03-22 21:14:11 +00:00
|
|
|
<div className={'ml-8 text-center'}>
|
|
|
|
<p className={'text-sm'}>{database.connectionString}</p>
|
|
|
|
<p className={'mt-1 text-2xs text-neutral-500 uppercase select-none'}>Endpoint</p>
|
2019-07-17 05:15:14 +00:00
|
|
|
</div>
|
2020-03-22 21:14:11 +00:00
|
|
|
<div className={'ml-8 text-center'}>
|
|
|
|
<p className={'text-sm'}>{database.allowConnectionsFrom}</p>
|
|
|
|
<p className={'mt-1 text-2xs text-neutral-500 uppercase select-none'}>Connections from</p>
|
2019-07-17 05:15:14 +00:00
|
|
|
</div>
|
2020-03-22 21:14:11 +00:00
|
|
|
<div className={'ml-8 text-center'}>
|
|
|
|
<p className={'text-sm'}>{database.username}</p>
|
|
|
|
<p className={'mt-1 text-2xs text-neutral-500 uppercase select-none'}>Username</p>
|
2019-07-17 05:15:14 +00:00
|
|
|
</div>
|
2020-03-22 21:14:11 +00:00
|
|
|
<div className={'ml-8'}>
|
2019-07-17 05:29:00 +00:00
|
|
|
<button className={'btn btn-sm btn-secondary mr-2'} onClick={() => setConnectionVisible(true)}>
|
2019-07-17 05:15:14 +00:00
|
|
|
<FontAwesomeIcon icon={faEye} fixedWidth={true}/>
|
|
|
|
</button>
|
2020-03-30 05:20:27 +00:00
|
|
|
<Can action={'database.delete'}>
|
|
|
|
<button className={'btn btn-sm btn-secondary btn-red'} onClick={() => setVisible(true)}>
|
|
|
|
<FontAwesomeIcon icon={faTrashAlt} fixedWidth={true}/>
|
|
|
|
</button>
|
|
|
|
</Can>
|
2019-07-17 05:15:14 +00:00
|
|
|
</div>
|
2019-07-10 05:00:29 +00:00
|
|
|
</div>
|
2019-07-17 05:15:14 +00:00
|
|
|
</React.Fragment>
|
2019-07-10 05:00:29 +00:00
|
|
|
);
|
|
|
|
};
|