From 9ec6068f457d0240c9acfe7a55df378c93d858ca Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sat, 9 Jan 2021 10:32:34 -0700 Subject: [PATCH] admin(ui): add ability to delete locations --- .../admin/databases/DatabaseDeleteButton.tsx | 1 + .../admin/locations/LocationDeleteButton.tsx | 58 +++++++++++++++++++ .../admin/locations/LocationEditContainer.tsx | 24 ++++++-- 3 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 resources/scripts/components/admin/locations/LocationDeleteButton.tsx diff --git a/resources/scripts/components/admin/databases/DatabaseDeleteButton.tsx b/resources/scripts/components/admin/databases/DatabaseDeleteButton.tsx index 53215f641..ea9cb1de6 100644 --- a/resources/scripts/components/admin/databases/DatabaseDeleteButton.tsx +++ b/resources/scripts/components/admin/databases/DatabaseDeleteButton.tsx @@ -14,6 +14,7 @@ interface Props { 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 = () => { diff --git a/resources/scripts/components/admin/locations/LocationDeleteButton.tsx b/resources/scripts/components/admin/locations/LocationDeleteButton.tsx new file mode 100644 index 000000000..f4d7134a2 --- /dev/null +++ b/resources/scripts/components/admin/locations/LocationDeleteButton.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 deleteLocation from '@/api/admin/locations/deleteLocation'; + +interface Props { + locationId: number; + onDeleted: () => void; +} + +export default ({ locationId, 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('location'); + + deleteLocation(locationId) + .then(() => { + setLoading(false); + onDeleted(); + }) + .catch(error => { + console.error(error); + clearAndAddHttpError({ key: 'location', error }); + + setLoading(false); + setVisible(false); + }); + }; + + return ( + <> + setVisible(false)} + > + Are you sure you want to delete this location? You may only delete a location if no nodes are assigned to it. + + + + + ); +}; diff --git a/resources/scripts/components/admin/locations/LocationEditContainer.tsx b/resources/scripts/components/admin/locations/LocationEditContainer.tsx index 465d9009d..83e3d53b3 100644 --- a/resources/scripts/components/admin/locations/LocationEditContainer.tsx +++ b/resources/scripts/components/admin/locations/LocationEditContainer.tsx @@ -1,6 +1,8 @@ +import DatabaseDeleteButton from '@/components/admin/databases/DatabaseDeleteButton'; +import LocationDeleteButton from '@/components/admin/locations/LocationDeleteButton'; 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 { Location } from '@/api/admin/locations/getLocations'; import getLocation from '@/api/admin/locations/getLocation'; @@ -35,7 +37,10 @@ interface Values { } const EditInformationContainer = () => { + const history = useHistory(); + const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes); + const location = Context.useStoreState(state => state.location); const setLocation = Context.useStoreActions(actions => actions.setLocation); @@ -94,10 +99,19 @@ const EditInformationContainer = () => { /> -
- +
+
+ history.push('/admin/locations')} + /> +
+ +
+ +