1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 15:59:41 +02:00

fix(metadata): add mutli endpoint delete api EE-6872 (#11550)
Some checks are pending
ci / build_images (map[arch:amd64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
ci / build_images (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
ci / build_images (map[arch:arm platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:arm64 platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:ppc64le platform:linux version:]) (push) Waiting to run
ci / build_images (map[arch:s390x platform:linux version:]) (push) Waiting to run
ci / build_manifests (push) Blocked by required conditions
/ triage (push) Waiting to run
Lint / Run linters (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Waiting to run
Test / test-client (push) Waiting to run
Test / test-server (map[arch:amd64 platform:linux]) (push) Waiting to run
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Waiting to run
Test / test-server (map[arch:arm64 platform:linux]) (push) Waiting to run

This commit is contained in:
Dakota Walsh 2024-04-30 21:32:20 -04:00 committed by GitHub
parent 439f13af19
commit 2fe213d864
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 123 additions and 29 deletions

View file

@ -1,8 +1,6 @@
import { useStore } from 'zustand';
import _ from 'lodash';
import { environmentStore } from '@/react/hooks/current-environment-store';
import { notifySuccess } from '@/portainer/services/notifications';
import { PageHeader } from '@@/PageHeader';
import { confirmDelete } from '@@/modals/confirm';
@ -44,15 +42,11 @@ export function ListView() {
}
deletionMutation.mutate(
environments.map((e) => e.Id),
{
onSuccess() {
notifySuccess(
'Environments successfully removed',
_.map(environments, 'Name').join(', ')
);
},
}
environments.map((e) => ({
id: e.Id,
deleteCluster: false,
name: e.Name,
}))
);
}
}

View file

@ -1,12 +1,9 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { promiseSequence } from '@/portainer/helpers/promise-utils';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import {
mutationOptions,
withError,
withInvalidate,
} from '@/react-tools/react-query';
import { withError } from '@/react-tools/react-query';
import { notifyError, notifySuccess } from '@/portainer/services/notifications';
import { pluralize } from '@/portainer/helpers/strings';
import { buildUrl } from '../environment.service/utils';
import { EnvironmentId } from '../types';
@ -14,22 +11,54 @@ import { EnvironmentId } from '../types';
export function useDeleteEnvironmentsMutation() {
const queryClient = useQueryClient();
return useMutation(
(environments: EnvironmentId[]) =>
promiseSequence(
environments.map(
(environmentId) => () => deleteEnvironment(environmentId)
)
),
mutationOptions(
withError('Unable to delete environment(s)'),
withInvalidate(queryClient, [['environments']])
)
async (
environments: {
id: EnvironmentId;
name: string;
deleteCluster?: boolean;
}[]
) => {
const resps = await deleteEnvironments(environments);
const successfulDeletions = resps.filter((r) => r.err === null);
const failedDeletions = resps.filter((r) => r.err !== null);
return { successfulDeletions, failedDeletions };
},
{
...withError('Unable to delete environment(s)'),
onSuccess: ({ successfulDeletions, failedDeletions }) => {
queryClient.invalidateQueries(['environments']);
// show an error message for each env that failed to delete
failedDeletions.forEach((deletion) => {
notifyError(
`Failed to remove environment`,
new Error(deletion.err ? deletion.err.Message : '') as Error
);
});
// show one summary message for all successful deletes
if (successfulDeletions.length) {
notifySuccess(
`${pluralize(
successfulDeletions.length,
'Environment'
)} successfully removed`,
successfulDeletions.map((deletion) => deletion.name).join(', ')
);
}
},
}
);
}
async function deleteEnvironment(id: EnvironmentId) {
async function deleteEnvironments(
environments: { id: EnvironmentId; deleteCluster?: boolean }[]
) {
try {
await axios.delete(buildUrl(id));
const { data } = await axios.post<
{ name: string; err: { Message: string } | null }[]
>(buildUrl(undefined, 'remove'), {
environments,
});
return data;
} catch (e) {
throw parseAxiosError(e as Error, 'Unable to delete environment');
}