mirror of
https://github.com/portainer/portainer.git
synced 2025-07-25 08:19:40 +02:00
refactor(k8s): namespace core logic (#12142)
Co-authored-by: testA113 <aliharriss1995@gmail.com> Co-authored-by: Anthony Lapenna <anthony.lapenna@portainer.io> Co-authored-by: James Carppe <85850129+jamescarppe@users.noreply.github.com> Co-authored-by: Ali <83188384+testA113@users.noreply.github.com>
This commit is contained in:
parent
da010f3d08
commit
ea228c3d6d
276 changed files with 9241 additions and 3361 deletions
60
app/react/kubernetes/volumes/queries/useDeleteVolumes.ts
Normal file
60
app/react/kubernetes/volumes/queries/useDeleteVolumes.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
import { EnvironmentId } from '@/react/portainer/environments/types';
|
||||
import axios from '@/portainer/services/axios';
|
||||
import { getAllSettledItems } from '@/portainer/helpers/promise-utils';
|
||||
import { withGlobalError } from '@/react-tools/react-query';
|
||||
import { notifyError, notifySuccess } from '@/portainer/services/notifications';
|
||||
import { pluralize } from '@/portainer/helpers/strings';
|
||||
|
||||
import { parseKubernetesAxiosError } from '../../axiosError';
|
||||
import { VolumeViewModel } from '../ListView/types';
|
||||
|
||||
import { queryKeys } from './query-keys';
|
||||
|
||||
export function useDeleteVolumes(environmentId: EnvironmentId) {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (volumes: VolumeViewModel[]) =>
|
||||
deleteVolumes(volumes, environmentId),
|
||||
onSuccess: ({ fulfilledItems, rejectedItems }) => {
|
||||
// one error notification per rejected item
|
||||
rejectedItems.forEach(({ item, reason }) => {
|
||||
notifyError(
|
||||
`Failed to remove volume '${item.PersistentVolumeClaim.Name}'`,
|
||||
new Error(reason)
|
||||
);
|
||||
});
|
||||
|
||||
// one success notification for all fulfilled items
|
||||
if (fulfilledItems.length) {
|
||||
notifySuccess(
|
||||
`${pluralize(fulfilledItems.length, 'Volume')} successfully removed`,
|
||||
fulfilledItems
|
||||
.map((item) => item.PersistentVolumeClaim.Name)
|
||||
.join(', ')
|
||||
);
|
||||
}
|
||||
queryClient.invalidateQueries(queryKeys.storages(environmentId));
|
||||
queryClient.invalidateQueries(queryKeys.volumes(environmentId));
|
||||
},
|
||||
...withGlobalError('Unable to remove volumes'),
|
||||
});
|
||||
}
|
||||
|
||||
function deleteVolumes(
|
||||
volumes: VolumeViewModel[],
|
||||
environmentId: EnvironmentId
|
||||
) {
|
||||
return getAllSettledItems(volumes, deleteVolume);
|
||||
|
||||
async function deleteVolume(volume: VolumeViewModel) {
|
||||
try {
|
||||
await axios.delete(
|
||||
`/endpoints/${environmentId}/kubernetes/api/v1/namespaces/${volume.ResourcePool.Namespace.Name}/persistentvolumeclaims/${volume.PersistentVolumeClaim.Name}`
|
||||
);
|
||||
} catch (error) {
|
||||
throw parseKubernetesAxiosError(error, 'Unable to remove volume');
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue