diff --git a/resources/scripts/components/admin/locations/LocationDeleteButton.tsx b/resources/scripts/components/admin/locations/LocationDeleteButton.tsx index f4d7134a2..cd712daba 100644 --- a/resources/scripts/components/admin/locations/LocationDeleteButton.tsx +++ b/resources/scripts/components/admin/locations/LocationDeleteButton.tsx @@ -1,10 +1,10 @@ import React, { useState } from 'react'; import { Actions, useStoreActions } from 'easy-peasy'; -import { ApplicationStore } from '@/state'; import tw from 'twin.macro'; +import deleteLocation from '@/api/admin/locations/deleteLocation'; import Button from '@/components/elements/Button'; import ConfirmationModal from '@/components/elements/ConfirmationModal'; -import deleteLocation from '@/api/admin/locations/deleteLocation'; +import { ApplicationStore } from '@/state'; interface Props { locationId: number; diff --git a/resources/scripts/components/admin/roles/RoleDeleteButton.tsx b/resources/scripts/components/admin/roles/RoleDeleteButton.tsx new file mode 100644 index 000000000..964b0052c --- /dev/null +++ b/resources/scripts/components/admin/roles/RoleDeleteButton.tsx @@ -0,0 +1,58 @@ +import { Actions, useStoreActions } from 'easy-peasy'; +import React, { useState } from 'react'; +import tw from 'twin.macro'; +import deleteRole from '@/api/admin/roles/deleteRole'; +import Button from '@/components/elements/Button'; +import ConfirmationModal from '@/components/elements/ConfirmationModal'; +import { ApplicationStore } from '@/state'; + +interface Props { + roleId: number; + onDeleted: () => void; +} + +export default ({ roleId, 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('role'); + + deleteRole(roleId) + .then(() => { + setLoading(false); + onDeleted(); + }) + .catch(error => { + console.error(error); + clearAndAddHttpError({ key: 'role', error }); + + setLoading(false); + setVisible(false); + }); + }; + + return ( + <> + setVisible(false)} + > + Are you sure you want to delete this role? + + + + + ); +}; diff --git a/resources/scripts/components/admin/roles/RoleEditContainer.tsx b/resources/scripts/components/admin/roles/RoleEditContainer.tsx index 2616b6ecb..912da287c 100644 --- a/resources/scripts/components/admin/roles/RoleEditContainer.tsx +++ b/resources/scripts/components/admin/roles/RoleEditContainer.tsx @@ -1,21 +1,22 @@ import { action, Action, Actions, createContextStore, useStoreActions } from 'easy-peasy'; +import { Form, Formik, FormikHelpers } from 'formik'; import React, { useEffect, useState } from 'react'; -// import { useHistory } from 'react-router'; +import { useHistory } from 'react-router'; import { useRouteMatch } from 'react-router-dom'; import tw from 'twin.macro'; import { object, string } from 'yup'; import { Role } from '@/api/admin/roles/getRoles'; import getRole from '@/api/admin/roles/getRole'; -import AdminContentBlock from '@/components/admin/AdminContentBlock'; -import Spinner from '@/components/elements/Spinner'; -import FlashMessageRender from '@/components/FlashMessageRender'; -import { ApplicationStore } from '@/state'; -import { Form, Formik, FormikHelpers } from 'formik'; import updateRole from '@/api/admin/roles/updateRole'; +import FlashMessageRender from '@/components/FlashMessageRender'; import AdminBox from '@/components/admin/AdminBox'; -import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; -import Field from '@/components/elements/Field'; +import AdminContentBlock from '@/components/admin/AdminContentBlock'; +import RoleDeleteButton from '@/components/admin/roles/RoleDeleteButton'; import Button from '@/components/elements/Button'; +import Field from '@/components/elements/Field'; +import Spinner from '@/components/elements/Spinner'; +import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; +import { ApplicationStore } from '@/state'; interface ctx { role: Role | undefined; @@ -36,7 +37,7 @@ interface Values { } const EditInformationContainer = () => { - // const history = useHistory(); + const history = useHistory(); const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions) => actions.flashes); @@ -99,12 +100,12 @@ const EditInformationContainer = () => {
- {/*
*/} - {/* history.push('/admin/roles')} */} - {/* /> */} - {/*
*/} +
+ history.push('/admin/roles')} + /> +