mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-21 01:04:44 +02:00

This allows entire components to be unmounted when the modal is hidden without affecting the fade in/out of the modal itself. This also makes it easier to programatically dismiss a modal without having to copy the visibility all over the place, and makes working with props much simpler in those modal components
60 lines
2.4 KiB
TypeScript
60 lines
2.4 KiB
TypeScript
import React, { useState } from 'react';
|
|
import ConfirmationModal from '@/components/elements/ConfirmationModal';
|
|
import { ServerContext } from '@/state/server';
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
import { faTrashAlt } from '@fortawesome/free-solid-svg-icons';
|
|
import { Subuser } from '@/state/server/subusers';
|
|
import deleteSubuser from '@/api/server/users/deleteSubuser';
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
|
import { ApplicationStore } from '@/state';
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
import tw from 'twin.macro';
|
|
|
|
export default ({ subuser }: { subuser: Subuser }) => {
|
|
const [ loading, setLoading ] = useState(false);
|
|
const [ showConfirmation, setShowConfirmation ] = useState(false);
|
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
const removeSubuser = ServerContext.useStoreActions(actions => actions.subusers.removeSubuser);
|
|
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
|
|
const doDeletion = () => {
|
|
setLoading(true);
|
|
clearFlashes('users');
|
|
deleteSubuser(uuid, subuser.uuid)
|
|
.then(() => {
|
|
setLoading(false);
|
|
removeSubuser(subuser.uuid);
|
|
})
|
|
.catch(error => {
|
|
console.error(error);
|
|
addError({ key: 'users', message: httpErrorToHuman(error) });
|
|
setShowConfirmation(false);
|
|
});
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<ConfirmationModal
|
|
title={'Delete this subuser?'}
|
|
buttonText={'Yes, remove subuser'}
|
|
visible
|
|
showSpinnerOverlay={loading}
|
|
onConfirmed={() => doDeletion()}
|
|
onModalDismissed={() => setShowConfirmation(false)}
|
|
>
|
|
Are you sure you wish to remove this subuser? They will have all access to this server revoked
|
|
immediately.
|
|
</ConfirmationModal>
|
|
<button
|
|
type={'button'}
|
|
aria-label={'Delete subuser'}
|
|
css={tw`block text-sm p-2 text-neutral-500 hover:text-red-600 transition-colors duration-150`}
|
|
onClick={() => setShowConfirmation(true)}
|
|
>
|
|
<FontAwesomeIcon icon={faTrashAlt}/>
|
|
</button>
|
|
</>
|
|
);
|
|
};
|