Update usage of useDeleteItems

* Update usage of useDeleteItems.
* Remove async when not necessary
* Update return of promises
This commit is contained in:
nixocio
2020-11-04 14:21:48 -05:00
parent 1deb4ff5e4
commit faec21ed08
27 changed files with 65 additions and 52 deletions

View File

@@ -122,7 +122,7 @@ function JobList({ i18n, defaultParams, showTypeColumn = false }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all( return Promise.all(
selected.map(({ type, id }) => { selected.map(({ type, id }) => {
switch (type) { switch (type) {

View File

@@ -66,7 +66,7 @@ function ResourceAccessList({ i18n, apiModel, resource }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
if (typeof deletionRole.team_id !== 'undefined') { if (typeof deletionRole.team_id !== 'undefined') {
return TeamsAPI.disassociateRole(deletionRole.team_id, deletionRole.id); return TeamsAPI.disassociateRole(deletionRole.team_id, deletionRole.id);
} }

View File

@@ -4,7 +4,7 @@ import { t } from '@lingui/macro';
import { Link, useHistory } from 'react-router-dom'; import { Link, useHistory } from 'react-router-dom';
import { Button } from '@patternfly/react-core'; import { Button } from '@patternfly/react-core';
import { useDeleteItems } from '../../../util/useRequest'; import useRequest, { useDismissableError } from '../../../util/useRequest';
import AlertModal from '../../../components/AlertModal'; import AlertModal from '../../../components/AlertModal';
import { CardBody, CardActionsRow } from '../../../components/Card'; import { CardBody, CardActionsRow } from '../../../components/Card';
import { Detail, DetailList } from '../../../components/DetailList'; import { Detail, DetailList } from '../../../components/DetailList';
@@ -21,16 +21,17 @@ function ApplicationDetails({
const history = useHistory(); const history = useHistory();
const { const {
isLoading: deleteLoading, isLoading: deleteLoading,
deletionError, error: deletionError,
deleteItems: handleDeleteApplications, request: deleteApplications,
clearDeletionError, } = useRequest(
} = useDeleteItems(
useCallback(async () => { useCallback(async () => {
await ApplicationsAPI.destroy(application.id); await ApplicationsAPI.destroy(application.id);
history.push('/applications'); history.push('/applications');
}, [application.id, history]) }, [application.id, history])
); );
const { error, dismissError } = useDismissableError(deletionError);
const getAuthorizationGrantType = type => { const getAuthorizationGrantType = type => {
let value; let value;
authorizationOptions.filter(option => { authorizationOptions.filter(option => {
@@ -104,22 +105,22 @@ function ApplicationDetails({
<DeleteButton <DeleteButton
name={application.name} name={application.name}
modalTitle={i18n._(t`Delete application`)} modalTitle={i18n._(t`Delete application`)}
onConfirm={handleDeleteApplications} onConfirm={deleteApplications}
isDisabled={deleteLoading} isDisabled={deleteLoading}
> >
{i18n._(t`Delete`)} {i18n._(t`Delete`)}
</DeleteButton> </DeleteButton>
)} )}
</CardActionsRow> </CardActionsRow>
{deletionError && ( {error && (
<AlertModal <AlertModal
isOpen={deletionError} isOpen={error}
variant="error" variant="error"
title={i18n._(t`Error!`)} title={i18n._(t`Error!`)}
onClose={clearDeletionError} onClose={dismissError}
> >
{i18n._(t`Failed to delete application.`)} {i18n._(t`Failed to delete application.`)}
<ErrorDetail error={deletionError} /> <ErrorDetail error={error} />
</AlertModal> </AlertModal>
)} )}
</CardBody> </CardBody>

View File

@@ -1,5 +1,6 @@
import React from 'react'; import React from 'react';
import { act } from 'react-dom/test-utils'; import { act } from 'react-dom/test-utils';
import { createMemoryHistory } from 'history';
import { mountWithContexts } from '../../../../testUtils/enzymeHelpers'; import { mountWithContexts } from '../../../../testUtils/enzymeHelpers';
import { ApplicationsAPI } from '../../../api'; import { ApplicationsAPI } from '../../../api';
@@ -126,21 +127,27 @@ describe('<ApplicationDetails/>', () => {
}); });
test('should delete properly', async () => { test('should delete properly', async () => {
ApplicationsAPI.destroy.mockResolvedValue({ data: {} }); const history = createMemoryHistory({
initialEntries: ['/applications/1/details'],
});
await act(async () => { await act(async () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<ApplicationDetails <ApplicationDetails
application={application} application={application}
authorizationOptions={authorizationOptions} authorizationOptions={authorizationOptions}
clientTypeOptions={clientTypeOptions} clientTypeOptions={clientTypeOptions}
/> />,
{
context: { router: { history } },
}
); );
}); });
await act(async () => expect(history.location.pathname).toEqual('/applications/1/details');
wrapper.find('Button[aria-label="Delete"]').prop('onClick')() await act(async () => {
); wrapper.find('DeleteButton').invoke('onConfirm')();
wrapper.update(); });
await act(async () => wrapper.find('DeleteButton').prop('onConfirm')()); expect(ApplicationsAPI.destroy).toHaveBeenCalledTimes(1);
expect(history.location.pathname).toBe('/applications');
expect(ApplicationsAPI.destroy).toBeCalledWith(10); expect(ApplicationsAPI.destroy).toBeCalledWith(10);
}); });

View File

@@ -76,8 +76,8 @@ function ApplicationTokenList({ i18n }) {
deleteItems: handleDeleteApplications, deleteItems: handleDeleteApplications,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
await Promise.all( return Promise.all(
selected.map(({ id: tokenId }) => TokensAPI.destroy(tokenId)) selected.map(({ id: tokenId }) => TokensAPI.destroy(tokenId))
); );
}, [selected]), }, [selected]),

View File

@@ -80,11 +80,11 @@ function ApplicationsList({ i18n }) {
const { const {
isLoading: deleteLoading, isLoading: deleteLoading,
deletionError, deletionError,
deleteItems: handleDeleteApplications, deleteItems: deleteApplications,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
await Promise.all(selected.map(({ id }) => ApplicationsAPI.destroy(id))); return Promise.all(selected.map(({ id }) => ApplicationsAPI.destroy(id)));
}, [selected]), }, [selected]),
{ {
qsConfig: QS_CONFIG, qsConfig: QS_CONFIG,
@@ -93,8 +93,8 @@ function ApplicationsList({ i18n }) {
} }
); );
const handleDelete = async () => { const handleDeleteApplications = async () => {
await handleDeleteApplications(); await deleteApplications();
setSelected([]); setSelected([]);
}; };
@@ -163,7 +163,7 @@ function ApplicationsList({ i18n }) {
: []), : []),
<ToolbarDeleteButton <ToolbarDeleteButton
key="delete" key="delete"
onDelete={handleDelete} onDelete={handleDeleteApplications}
itemsToDelete={selected} itemsToDelete={selected}
pluralizedItemName={i18n._(t`Applications`)} pluralizedItemName={i18n._(t`Applications`)}
/>, />,

View File

@@ -62,7 +62,7 @@ function CredentialList({ i18n }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all(selected.map(({ id }) => CredentialsAPI.destroy(id))); return Promise.all(selected.map(({ id }) => CredentialsAPI.destroy(id)));
}, [selected]), }, [selected]),
{ {

View File

@@ -83,8 +83,8 @@ function CredentialTypeList({ i18n }) {
deleteItems: deleteCredentialTypes, deleteItems: deleteCredentialTypes,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
await Promise.all( return Promise.all(
selected.map(({ id }) => CredentialTypesAPI.destroy(id)) selected.map(({ id }) => CredentialTypesAPI.destroy(id))
); );
}, [selected]), }, [selected]),

View File

@@ -100,7 +100,7 @@ function DashboardTemplateList({ i18n }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all( return Promise.all(
selected.map(({ type, id }) => { selected.map(({ type, id }) => {
if (type === 'job_template') { if (type === 'job_template') {

View File

@@ -91,7 +91,7 @@ function HostGroupsList({ i18n, host }) {
deleteItems: disassociateHosts, deleteItems: disassociateHosts,
deletionError: disassociateError, deletionError: disassociateError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all( return Promise.all(
selected.map(group => HostsAPI.disassociateGroup(hostId, group)) selected.map(group => HostsAPI.disassociateGroup(hostId, group))
); );

View File

@@ -72,7 +72,7 @@ function HostList({ i18n }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all(selected.map(host => HostsAPI.destroy(host.id))); return Promise.all(selected.map(host => HostsAPI.destroy(host.id)));
}, [selected]), }, [selected]),
{ {

View File

@@ -103,8 +103,8 @@ function InstanceGroupList({ i18n }) {
deleteItems: deleteInstanceGroups, deleteItems: deleteInstanceGroups,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
await Promise.all( return Promise.all(
selected.map(({ id }) => InstanceGroupsAPI.destroy(id)) selected.map(({ id }) => InstanceGroupsAPI.destroy(id))
); );
}, [selected]), }, [selected]),

View File

@@ -86,7 +86,7 @@ function InstanceList({ i18n }) {
deleteItems: disassociateInstances, deleteItems: disassociateInstances,
deletionError: disassociateError, deletionError: disassociateError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all( return Promise.all(
selected.map(instance => selected.map(instance =>
InstanceGroupsAPI.disassociateInstance(instanceGroupId, instance.id) InstanceGroupsAPI.disassociateInstance(instanceGroupId, instance.id)

View File

@@ -86,7 +86,7 @@ function InventoryGroupHostList({ i18n }) {
deleteItems: disassociateHosts, deleteItems: disassociateHosts,
deletionError: disassociateErr, deletionError: disassociateErr,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all( return Promise.all(
selected.map(host => GroupsAPI.disassociateHost(groupId, host)) selected.map(host => GroupsAPI.disassociateHost(groupId, host))
); );

View File

@@ -90,7 +90,7 @@ function InventoryHostGroupsList({ i18n }) {
deleteItems: disassociateHosts, deleteItems: disassociateHosts,
deletionError: disassociateError, deletionError: disassociateError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all( return Promise.all(
selected.map(group => HostsAPI.disassociateGroup(hostId, group)) selected.map(group => HostsAPI.disassociateGroup(hostId, group))
); );

View File

@@ -87,12 +87,17 @@ function InventoryHostList({ i18n }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
await Promise.all(selected.map(host => HostsAPI.destroy(host.id))); return Promise.all(selected.map(host => HostsAPI.destroy(host.id)));
}, [selected]), }, [selected]),
{ qsConfig: QS_CONFIG, fetchItems: fetchData } { qsConfig: QS_CONFIG, fetchItems: fetchData }
); );
const handleDeleteHosts = async () => {
await deleteHosts();
setSelected([]);
};
const canAdd = const canAdd =
actions && Object.prototype.hasOwnProperty.call(actions, 'POST'); actions && Object.prototype.hasOwnProperty.call(actions, 'POST');
const isAllSelected = selected.length > 0 && selected.length === hosts.length; const isAllSelected = selected.length > 0 && selected.length === hosts.length;
@@ -150,7 +155,7 @@ function InventoryHostList({ i18n }) {
/>, />,
<ToolbarDeleteButton <ToolbarDeleteButton
key="delete" key="delete"
onDelete={deleteHosts} onDelete={handleDeleteHosts}
itemsToDelete={selected} itemsToDelete={selected}
pluralizedItemName={i18n._(t`Hosts`)} pluralizedItemName={i18n._(t`Hosts`)}
/>, />,

View File

@@ -96,7 +96,7 @@ function InventoryList({ i18n }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all(selected.map(team => InventoriesAPI.destroy(team.id))); return Promise.all(selected.map(team => InventoriesAPI.destroy(team.id)));
}, [selected]), }, [selected]),
{ {

View File

@@ -89,7 +89,7 @@ function InventorySourceList({ i18n }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all( return Promise.all(
selected.map(({ id: sourceId }) => selected.map(({ id: sourceId }) =>
InventorySourcesAPI.destroy(sourceId) InventorySourcesAPI.destroy(sourceId)

View File

@@ -67,7 +67,7 @@ function NotificationTemplatesList({ i18n }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all( return Promise.all(
selected.map(({ id }) => NotificationTemplatesAPI.destroy(id)) selected.map(({ id }) => NotificationTemplatesAPI.destroy(id))
); );

View File

@@ -81,7 +81,7 @@ function OrganizationsList({ i18n }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all( return Promise.all(
selected.map(({ id }) => OrganizationsAPI.destroy(id)) selected.map(({ id }) => OrganizationsAPI.destroy(id))
); );

View File

@@ -80,7 +80,7 @@ function ProjectJobTemplatesList({ i18n }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all( return Promise.all(
selected.map(template => JobTemplatesAPI.destroy(template.id)) selected.map(template => JobTemplatesAPI.destroy(template.id))
); );

View File

@@ -82,7 +82,7 @@ function ProjectList({ i18n }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all(selected.map(({ id }) => ProjectsAPI.destroy(id))); return Promise.all(selected.map(({ id }) => ProjectsAPI.destroy(id)));
}, [selected]), }, [selected]),
{ {

View File

@@ -78,7 +78,7 @@ function TeamList({ i18n }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all(selected.map(team => TeamsAPI.destroy(team.id))); return Promise.all(selected.map(team => TeamsAPI.destroy(team.id)));
}, [selected]), }, [selected]),
{ {

View File

@@ -92,7 +92,7 @@ function TemplateList({ i18n }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all( return Promise.all(
selected.map(({ type, id }) => { selected.map(({ type, id }) => {
if (type === 'job_template') { if (type === 'job_template') {

View File

@@ -79,7 +79,7 @@ function UserList({ i18n }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all(selected.map(user => UsersAPI.destroy(user.id))); return Promise.all(selected.map(user => UsersAPI.destroy(user.id)));
}, [selected]), }, [selected]),
{ {

View File

@@ -110,7 +110,7 @@ function UserTeamList({ i18n }) {
deleteItems: disassociateTeams, deleteItems: disassociateTeams,
deletionError: disassociateError, deletionError: disassociateError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all(selected.flatMap(team => disassociateUserRoles(team))); return Promise.all(selected.flatMap(team => disassociateUserRoles(team)));
/* eslint-disable-next-line react-hooks/exhaustive-deps */ /* eslint-disable-next-line react-hooks/exhaustive-deps */
}, [selected]), }, [selected]),

View File

@@ -78,7 +78,7 @@ function UserTokenList({ i18n }) {
deletionError, deletionError,
clearDeletionError, clearDeletionError,
} = useDeleteItems( } = useDeleteItems(
useCallback(async () => { useCallback(() => {
return Promise.all( return Promise.all(
selected.map(({ id: tokenId }) => TokensAPI.destroy(tokenId)) selected.map(({ id: tokenId }) => TokensAPI.destroy(tokenId))
); );