From a91cb578d7d4719fb57255744d6bfbf7f13a535c Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sat, 9 Jan 2021 10:26:30 -0700 Subject: [PATCH] admin(ui): add ability to delete database hosts --- .../admin/databases/DatabaseDeleteButton.tsx | 58 +++++++++++++++++++ .../admin/databases/DatabaseEditContainer.tsx | 23 ++++++-- 2 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 resources/scripts/components/admin/databases/DatabaseDeleteButton.tsx diff --git a/resources/scripts/components/admin/databases/DatabaseDeleteButton.tsx b/resources/scripts/components/admin/databases/DatabaseDeleteButton.tsx new file mode 100644 index 000000000..53215f641 --- /dev/null +++ b/resources/scripts/components/admin/databases/DatabaseDeleteButton.tsx @@ -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) => 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 ( + <> + 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. + + + + + ); +}; diff --git a/resources/scripts/components/admin/databases/DatabaseEditContainer.tsx b/resources/scripts/components/admin/databases/DatabaseEditContainer.tsx index 6c44c04b0..7450bc6b7 100644 --- a/resources/scripts/components/admin/databases/DatabaseEditContainer.tsx +++ b/resources/scripts/components/admin/databases/DatabaseEditContainer.tsx @@ -1,6 +1,6 @@ import React, { useEffect, useState } from 'react'; 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 { Database } from '@/api/admin/databases/getDatabases'; import getDatabase from '@/api/admin/databases/getDatabase'; @@ -15,6 +15,7 @@ import Field from '@/components/elements/Field'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import { Form, Formik, FormikHelpers } from 'formik'; import updateDatabase from '@/api/admin/databases/updateDatabase'; +import DatabaseDeleteButton from '@/components/admin/databases/DatabaseDeleteButton'; interface ctx { database: Database | undefined; @@ -38,7 +39,10 @@ interface Values { } const EditInformationContainer = () => { + const history = useHistory(); + const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes); + const database = Context.useStoreState(state => state.database); const setDatabase = Context.useStoreActions(actions => actions.setDatabase); @@ -134,10 +138,19 @@ const EditInformationContainer = () => { -
- +
+
+ history.push('/admin/databases')} + /> +
+ +
+ +