2023-09-07 15:14:03 +01:00
|
|
|
import { CellContext } from '@tanstack/react-table';
|
|
|
|
import { useRouter } from '@uirouter/react';
|
|
|
|
|
|
|
|
import { Authorized } from '@/react/hooks/useUser';
|
2024-06-10 20:54:31 +02:00
|
|
|
import { useDisconnectContainer } from '@/react/docker/networks/queries/useDisconnectContainerMutation';
|
2023-09-07 15:14:03 +01:00
|
|
|
import { useEnvironmentId } from '@/react/hooks/useEnvironmentId';
|
2024-06-10 20:54:31 +02:00
|
|
|
import { notifySuccess } from '@/portainer/services/notifications';
|
2023-09-07 15:14:03 +01:00
|
|
|
|
|
|
|
import { LoadingButton } from '@@/buttons';
|
|
|
|
|
|
|
|
import { TableNetwork, isContainerNetworkTableMeta } from './types';
|
|
|
|
import { columnHelper } from './helper';
|
|
|
|
|
|
|
|
export const actions = columnHelper.display({
|
|
|
|
header: 'Actions',
|
|
|
|
cell: Cell,
|
|
|
|
});
|
|
|
|
|
|
|
|
function Cell({
|
2024-06-10 20:54:31 +02:00
|
|
|
row: {
|
|
|
|
original: { id: networkId },
|
|
|
|
},
|
2023-09-07 15:14:03 +01:00
|
|
|
table: {
|
|
|
|
options: { meta },
|
|
|
|
},
|
|
|
|
}: CellContext<TableNetwork, unknown>) {
|
|
|
|
const router = useRouter();
|
|
|
|
const environmentId = useEnvironmentId();
|
2024-06-10 20:54:31 +02:00
|
|
|
const disconnectMutation = useDisconnectContainer({
|
|
|
|
environmentId,
|
|
|
|
networkId,
|
|
|
|
});
|
2023-09-07 15:14:03 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Authorized authorizations="DockerNetworkDisconnect">
|
|
|
|
<LoadingButton
|
|
|
|
color="dangerlight"
|
2024-04-11 12:11:38 +12:00
|
|
|
data-cy="disconnect-network-button"
|
2023-09-07 15:14:03 +01:00
|
|
|
isLoading={disconnectMutation.isLoading}
|
|
|
|
loadingText="Leaving network..."
|
|
|
|
type="button"
|
|
|
|
onClick={handleSubmit}
|
|
|
|
>
|
|
|
|
Leave network
|
|
|
|
</LoadingButton>
|
|
|
|
</Authorized>
|
|
|
|
);
|
|
|
|
|
|
|
|
function handleSubmit() {
|
|
|
|
if (!isContainerNetworkTableMeta(meta)) {
|
|
|
|
throw new Error('Invalid row meta');
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectMutation.mutate(
|
|
|
|
{
|
|
|
|
containerId: meta.containerId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
onSuccess() {
|
2024-06-10 20:54:31 +02:00
|
|
|
notifySuccess('Container successfully disconnected', networkId);
|
2023-09-07 15:14:03 +01:00
|
|
|
router.stateService.reload();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|