2021-09-13 04:15:45 +00:00
|
|
|
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 deleteServer from '@/api/admin/servers/deleteServer';
|
2021-10-10 19:03:28 +00:00
|
|
|
import { TrashIcon } from '@heroicons/react/outline';
|
|
|
|
import { useServerFromRoute } from '@/api/admin/server';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
2021-09-13 04:15:45 +00:00
|
|
|
|
2021-10-10 19:03:28 +00:00
|
|
|
export default () => {
|
|
|
|
const history = useHistory();
|
2021-09-13 04:15:45 +00:00
|
|
|
const [ visible, setVisible ] = useState(false);
|
|
|
|
const [ loading, setLoading ] = useState(false);
|
2021-10-10 19:03:28 +00:00
|
|
|
const { data: server } = useServerFromRoute();
|
2021-09-13 04:15:45 +00:00
|
|
|
|
2021-10-10 19:03:28 +00:00
|
|
|
const {
|
|
|
|
clearFlashes,
|
|
|
|
clearAndAddHttpError,
|
|
|
|
} = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2021-09-13 04:15:45 +00:00
|
|
|
|
|
|
|
const onDelete = () => {
|
2021-10-10 19:03:28 +00:00
|
|
|
if (!server) return;
|
|
|
|
|
2021-09-13 04:15:45 +00:00
|
|
|
setLoading(true);
|
|
|
|
clearFlashes('server');
|
|
|
|
|
2021-10-10 19:03:28 +00:00
|
|
|
deleteServer(server.id)
|
|
|
|
.then(() => history.push('/admin/servers'))
|
2021-09-13 04:15:45 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
clearAndAddHttpError({ key: 'server', error });
|
|
|
|
|
|
|
|
setLoading(false);
|
|
|
|
setVisible(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-10-10 19:03:28 +00:00
|
|
|
if (!server) return null;
|
|
|
|
|
2021-09-13 04:15:45 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ConfirmationModal
|
|
|
|
visible={visible}
|
|
|
|
title={'Delete server?'}
|
|
|
|
buttonText={'Yes, delete server'}
|
|
|
|
onConfirmed={onDelete}
|
|
|
|
showSpinnerOverlay={loading}
|
|
|
|
onModalDismissed={() => setVisible(false)}
|
|
|
|
>
|
|
|
|
Are you sure you want to delete this server?
|
|
|
|
</ConfirmationModal>
|
2021-10-10 19:03:28 +00:00
|
|
|
<Button
|
|
|
|
type={'button'}
|
|
|
|
size={'small'}
|
|
|
|
color={'red'}
|
|
|
|
onClick={() => setVisible(true)}
|
|
|
|
css={tw`flex items-center justify-center`}
|
|
|
|
>
|
|
|
|
<TrashIcon css={tw`w-5 h-5 mr-2`}/> Delete Server
|
2021-09-13 04:15:45 +00:00
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|