Skip to content

Commit

Permalink
Bulk delete for credential types (#2268)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalis committed May 13, 2024
1 parent 8d6ef22 commit e33a1c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export function useCredentialTypeToolbarActions(
selection: PageActionSelection.Multiple,
icon: TrashIcon,
label: t('Delete selected credential types'),
isDisabled: () => 'Not implemented',
onClick: deleteCredentialTypes,
isDanger: true,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useMemo } from 'react';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { compareStrings } from '../../../../../framework';
import { useNameColumn } from '../../../../common/columns';
Expand All @@ -16,27 +16,50 @@ export function useDeleteCredentialTypes(
const confirmationColumns = useCredentialTypesColumns();
const deleteActionNameColumn = useNameColumn({ disableLinks: true, disableSort: true });
const actionColumns = useMemo(() => [deleteActionNameColumn], [deleteActionNameColumn]);

const cannotDeleteManagedCredentialType = (credentialType: EdaCredentialType) =>
credentialType.managed
? t(`The credential type is provided by the system and is read-only.`)
: '';
const bulkAction = useEdaBulkConfirmation<EdaCredentialType>();
return useCallback(
(credentialTypes: EdaCredentialType[]) => {
bulkAction({
title: t('Permanently delete credential types', { count: credentialTypes.length }),
confirmText: t('Yes, I confirm that I want to delete these {{count}} credential types.', {
count: credentialTypes.length,
}),
actionButtonText: t('Delete credential types', { count: credentialTypes.length }),
items: credentialTypes.sort((l, r) => compareStrings(l.name, r.name)),
keyFn: idKeyFn,
isDanger: true,
confirmationColumns,
actionColumns,
onComplete,
actionFn: (credentialType: EdaCredentialType, signal) => {
const url = edaAPI`/credential-types/${credentialType.id.toString()}/`;
return requestDelete(url, signal);
},
});
},
[actionColumns, bulkAction, confirmationColumns, onComplete, t]
);
const deleteCredentialTypes = (credentialTypes: EdaCredentialType[]) => {
const undeletableManagedCredentialTypes: EdaCredentialType[] = credentialTypes.filter(
(credentialType) => credentialType.managed
);

bulkAction({
title: t('Permanently delete credential types', { count: credentialTypes.length }),
confirmText: t('Yes, I confirm that I want to delete these {{count}} credential types.', {
count: credentialTypes.length,
}),
actionButtonText: t('Delete credential types', { count: credentialTypes.length }),
items: credentialTypes.sort((l, r) => compareStrings(l.name, r.name)),
alertPrompts: undeletableManagedCredentialTypes.length
? [
...(undeletableManagedCredentialTypes.length
? [
t(
'{{count}} of the selected credential types cannot be deleted because they are read-only.',
{
count: undeletableManagedCredentialTypes.length,
}
),
]
: []),
]
: undefined,
isItemNonActionable: (credentialType: EdaCredentialType) =>
cannotDeleteManagedCredentialType(credentialType),
keyFn: idKeyFn,
isDanger: true,
confirmationColumns,
actionColumns,
onComplete,
actionFn: (credentialType: EdaCredentialType, signal) => {
const url = edaAPI`/credential-types/${credentialType.id.toString()}/`;
return requestDelete(url, signal);
},
});
};
return deleteCredentialTypes;
}

0 comments on commit e33a1c2

Please sign in to comment.