admin(ui): add ability to delete database hosts

This commit is contained in:
Matthew Penner 2021-01-09 10:26:30 -07:00
parent d323662ad5
commit a91cb578d7
2 changed files with 76 additions and 5 deletions

View file

@ -0,0 +1,58 @@
import React, { useState } from 'react';
import { Actions, useStoreActions } from 'easy-peasy';
import { ApplicationStore } from '@/state';
import tw from 'twin.macro';
import Button from '@/components/elements/Button';
import ConfirmationModal from '@/components/elements/ConfirmationModal';
import deleteDatabase from '@/api/admin/databases/deleteDatabase';
interface Props {
databaseId: number;
onDeleted: () => void;
}
export default ({ databaseId, onDeleted }: Props) => {
const [ visible, setVisible ] = useState(false);
const [ loading, setLoading ] = useState(false);
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
const onDelete = () => {
setLoading(true);
clearFlashes('database');
deleteDatabase(databaseId)
.then(() => {
setLoading(false);
onDeleted();
})
.catch(error => {
console.error(error);
clearAndAddHttpError({ key: 'database', error });
setLoading(false);
setVisible(false);
});
};
return (
<>
<ConfirmationModal
visible={visible}
title={'Delete database host?'}
buttonText={'Yes, delete database host'}
onConfirmed={onDelete}
showSpinnerOverlay={loading}
onModalDismissed={() => setVisible(false)}
>
Are you sure you want to delete this database host? This action will delete all knowledge of
databases created on this host but not the databases themselves.
</ConfirmationModal>
<Button type={'button'} size={'xsmall'} color={'red'} onClick={() => setVisible(true)}>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" css={tw`h-5 w-5`}>
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
</Button>
</>
);
};

View file

@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import tw from 'twin.macro'; import tw from 'twin.macro';
import { useRouteMatch } from 'react-router-dom'; import { useHistory, useRouteMatch } from 'react-router-dom';
import { action, Action, Actions, createContextStore, useStoreActions } from 'easy-peasy'; import { action, Action, Actions, createContextStore, useStoreActions } from 'easy-peasy';
import { Database } from '@/api/admin/databases/getDatabases'; import { Database } from '@/api/admin/databases/getDatabases';
import getDatabase from '@/api/admin/databases/getDatabase'; import getDatabase from '@/api/admin/databases/getDatabase';
@ -15,6 +15,7 @@ import Field from '@/components/elements/Field';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import { Form, Formik, FormikHelpers } from 'formik'; import { Form, Formik, FormikHelpers } from 'formik';
import updateDatabase from '@/api/admin/databases/updateDatabase'; import updateDatabase from '@/api/admin/databases/updateDatabase';
import DatabaseDeleteButton from '@/components/admin/databases/DatabaseDeleteButton';
interface ctx { interface ctx {
database: Database | undefined; database: Database | undefined;
@ -38,7 +39,10 @@ interface Values {
} }
const EditInformationContainer = () => { const EditInformationContainer = () => {
const history = useHistory();
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes); const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
const database = Context.useStoreState(state => state.database); const database = Context.useStoreState(state => state.database);
const setDatabase = Context.useStoreActions(actions => actions.setDatabase); const setDatabase = Context.useStoreActions(actions => actions.setDatabase);
@ -134,10 +138,19 @@ const EditInformationContainer = () => {
</div> </div>
</div> </div>
<div css={tw`mt-6 text-right`}> <div css={tw`w-full flex flex-row items-center mt-6`}>
<Button type={'submit'} disabled={isSubmitting || !isValid}> <div css={tw`flex`}>
Save <DatabaseDeleteButton
</Button> databaseId={database.id}
onDeleted={() => history.push('/admin/databases')}
/>
</div>
<div css={tw`flex ml-auto`}>
<Button type={'submit'} disabled={isSubmitting || !isValid}>
Save
</Button>
</div>
</div> </div>
</Form> </Form>
</AdminBox> </AdminBox>