2020-03-27 22:32:33 +00:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import ConfirmationModal from '@/components/elements/ConfirmationModal';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2020-07-05 01:46:09 +00:00
|
|
|
import { faTrashAlt } from '@fortawesome/free-solid-svg-icons';
|
2020-03-27 22:32:33 +00:00
|
|
|
import { Subuser } from '@/state/server/subusers';
|
2022-02-27 17:25:31 +00:00
|
|
|
import { deleteSubuser } from '@/api/server/users';
|
2020-03-27 22:32:33 +00:00
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2020-07-04 23:26:07 +00:00
|
|
|
import tw from 'twin.macro';
|
2020-03-27 22:32:33 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2020-07-05 01:30:50 +00:00
|
|
|
};
|
2020-03-27 22:32:33 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ConfirmationModal
|
|
|
|
title={'Delete this subuser?'}
|
|
|
|
buttonText={'Yes, remove subuser'}
|
2020-08-19 03:16:13 +00:00
|
|
|
visible={showConfirmation}
|
2020-03-27 22:32:33 +00:00
|
|
|
showSpinnerOverlay={loading}
|
|
|
|
onConfirmed={() => doDeletion()}
|
2020-08-18 04:35:11 +00:00
|
|
|
onModalDismissed={() => setShowConfirmation(false)}
|
2020-03-27 22:32:33 +00:00
|
|
|
>
|
|
|
|
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'}
|
2020-07-04 23:26:07 +00:00
|
|
|
css={tw`block text-sm p-2 text-neutral-500 hover:text-red-600 transition-colors duration-150`}
|
2020-03-27 22:32:33 +00:00
|
|
|
onClick={() => setShowConfirmation(true)}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon={faTrashAlt}/>
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|