mirror of
https://github.com/ansible/awx.git
synced 2026-02-25 06:56:00 -03:30
Adds delete button to job details and handle delete errors
After successful deletion of Job the user is nativated back to Jobs List. If the job is not successfully deleted a alert modal pops up and on close the user remains on Job Details page.
This commit is contained in:
@@ -1,18 +1,27 @@
|
|||||||
import React from 'react';
|
import React, { useState } from 'react';
|
||||||
import { Link, withRouter } from 'react-router-dom';
|
import { Link, withRouter } from 'react-router-dom';
|
||||||
import { withI18n } from '@lingui/react';
|
import { withI18n } from '@lingui/react';
|
||||||
import { t } from '@lingui/macro';
|
import { t } from '@lingui/macro';
|
||||||
import { CardBody, Button } from '@patternfly/react-core';
|
import { CardBody, Button } from '@patternfly/react-core';
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
|
|
||||||
|
import AlertModal from '@components/AlertModal';
|
||||||
import { DetailList, Detail } from '@components/DetailList';
|
import { DetailList, Detail } from '@components/DetailList';
|
||||||
import { ChipGroup, Chip, CredentialChip } from '@components/Chip';
|
import { ChipGroup, Chip, CredentialChip } from '@components/Chip';
|
||||||
import { VariablesInput as _VariablesInput } from '@components/CodeMirrorInput';
|
import { VariablesInput as _VariablesInput } from '@components/CodeMirrorInput';
|
||||||
|
import ErrorDetail from '@components/ErrorDetail';
|
||||||
import { toTitleCase } from '@util/strings';
|
import { toTitleCase } from '@util/strings';
|
||||||
import { Job } from '../../../types';
|
import { Job } from '../../../types';
|
||||||
|
import { JobsAPI, ProjectUpdatesAPI } from '@api';
|
||||||
|
import { JOB_TYPE_URL_SEGMENTS } from '../../../constants';
|
||||||
|
|
||||||
const ActionButtonWrapper = styled.div`
|
const ActionButtonWrapper = styled.div`
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: flex-end;
|
justify-content: flex-end;
|
||||||
|
margin-top: 20px;
|
||||||
|
& > :not(:first-child) {
|
||||||
|
margin-left: 20px;
|
||||||
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const VariablesInput = styled(_VariablesInput)`
|
const VariablesInput = styled(_VariablesInput)`
|
||||||
@@ -29,7 +38,7 @@ const VERBOSITY = {
|
|||||||
4: '4 (Connection Debug)',
|
4: '4 (Connection Debug)',
|
||||||
};
|
};
|
||||||
|
|
||||||
function JobDetail({ job, i18n }) {
|
function JobDetail({ job, i18n, history }) {
|
||||||
const {
|
const {
|
||||||
job_template: jobTemplate,
|
job_template: jobTemplate,
|
||||||
project,
|
project,
|
||||||
@@ -38,7 +47,22 @@ function JobDetail({ job, i18n }) {
|
|||||||
credentials,
|
credentials,
|
||||||
labels,
|
labels,
|
||||||
} = job.summary_fields;
|
} = job.summary_fields;
|
||||||
|
const [isDeleteModalOpen, setDeleteModal] = useState(false);
|
||||||
|
const [errorMsg, setErrorMsg] = useState();
|
||||||
|
|
||||||
|
const deleteJob = async () => {
|
||||||
|
try {
|
||||||
|
if (job.type === 'job') {
|
||||||
|
await JobsAPI.destroy(job.id);
|
||||||
|
} else {
|
||||||
|
await ProjectUpdatesAPI.destroy(job.id);
|
||||||
|
}
|
||||||
|
history.push('/jobs');
|
||||||
|
} catch (err) {
|
||||||
|
setErrorMsg(err);
|
||||||
|
setDeleteModal(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
return (
|
return (
|
||||||
<CardBody>
|
<CardBody>
|
||||||
<DetailList>
|
<DetailList>
|
||||||
@@ -145,6 +169,13 @@ function JobDetail({ job, i18n }) {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
<ActionButtonWrapper>
|
<ActionButtonWrapper>
|
||||||
|
<Button
|
||||||
|
variant="danger"
|
||||||
|
aria-label="delete"
|
||||||
|
onClick={() => setDeleteModal(true)}
|
||||||
|
>
|
||||||
|
{i18n._(t`Delete`)}
|
||||||
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
variant="secondary"
|
variant="secondary"
|
||||||
aria-label="close"
|
aria-label="close"
|
||||||
@@ -154,6 +185,41 @@ function JobDetail({ job, i18n }) {
|
|||||||
{i18n._(t`Close`)}
|
{i18n._(t`Close`)}
|
||||||
</Button>
|
</Button>
|
||||||
</ActionButtonWrapper>
|
</ActionButtonWrapper>
|
||||||
|
{isDeleteModalOpen && (
|
||||||
|
<AlertModal
|
||||||
|
isOpen={isDeleteModalOpen}
|
||||||
|
title={i18n._(t`Delete Job`)}
|
||||||
|
variant="danger"
|
||||||
|
onClose={() => setDeleteModal(false)}
|
||||||
|
>
|
||||||
|
{i18n._(t`Are you sure you want to delete:`)}
|
||||||
|
<br />
|
||||||
|
<strong>{job.name}</strong>
|
||||||
|
<ActionButtonWrapper>
|
||||||
|
<Button
|
||||||
|
variant="secondary"
|
||||||
|
aria-label="close"
|
||||||
|
component={Link}
|
||||||
|
to={`/jobs/${JOB_TYPE_URL_SEGMENTS[job.type]}/${job.id}`}
|
||||||
|
>
|
||||||
|
{i18n._(t`Cancel`)}
|
||||||
|
</Button>
|
||||||
|
<Button variant="danger" aria-label="delete" onClick={deleteJob}>
|
||||||
|
{i18n._(t`Delete`)}
|
||||||
|
</Button>
|
||||||
|
</ActionButtonWrapper>
|
||||||
|
</AlertModal>
|
||||||
|
)}
|
||||||
|
{errorMsg && (
|
||||||
|
<AlertModal
|
||||||
|
isOpen={errorMsg}
|
||||||
|
variant="danger"
|
||||||
|
onClose={() => setErrorMsg()}
|
||||||
|
title={i18n._(t`Job Delete Error`)}
|
||||||
|
>
|
||||||
|
<ErrorDetail error={errorMsg} />
|
||||||
|
</AlertModal>
|
||||||
|
)}
|
||||||
</CardBody>
|
</CardBody>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { mountWithContexts } from '@testUtils/enzymeHelpers';
|
import { mountWithContexts } from '@testUtils/enzymeHelpers';
|
||||||
|
import { sleep } from '@testUtils/testUtils';
|
||||||
import JobDetail from './JobDetail';
|
import JobDetail from './JobDetail';
|
||||||
|
import { JobsAPI, ProjectUpdatesAPI } from '@api';
|
||||||
|
|
||||||
|
jest.mock('@api');
|
||||||
|
|
||||||
describe('<JobDetail />', () => {
|
describe('<JobDetail />', () => {
|
||||||
let job;
|
let job;
|
||||||
@@ -57,4 +61,57 @@ describe('<JobDetail />', () => {
|
|||||||
job.summary_fields.credentials[0]
|
job.summary_fields.credentials[0]
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
test('should properly delete job', () => {
|
||||||
|
job = {
|
||||||
|
name: 'Rage',
|
||||||
|
id: 1,
|
||||||
|
type: 'job',
|
||||||
|
summary_fields: {
|
||||||
|
job_template: { name: 'Spud' },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const wrapper = mountWithContexts(<JobDetail job={job} />);
|
||||||
|
wrapper
|
||||||
|
.find('button')
|
||||||
|
.at(0)
|
||||||
|
.invoke('onClick')();
|
||||||
|
const modal = wrapper.find('Modal');
|
||||||
|
expect(modal.length).toBe(1);
|
||||||
|
modal.find('button[aria-label="delete"]').invoke('onClick')();
|
||||||
|
expect(JobsAPI.destroy).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should display error modal when a job does not delete properly', async () => {
|
||||||
|
job = {
|
||||||
|
name: 'Angry',
|
||||||
|
id: 'a',
|
||||||
|
type: 'project_updates',
|
||||||
|
summary_fields: {
|
||||||
|
job_template: { name: 'Peanut' },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const wrapper = mountWithContexts(<JobDetail job={job} />);
|
||||||
|
wrapper
|
||||||
|
.find('button')
|
||||||
|
.at(0)
|
||||||
|
.invoke('onClick')();
|
||||||
|
const modal = wrapper.find('Modal');
|
||||||
|
ProjectUpdatesAPI.destroy.mockRejectedValue(
|
||||||
|
new Error({
|
||||||
|
response: {
|
||||||
|
config: {
|
||||||
|
method: 'delete',
|
||||||
|
url: '/api/v2/project_updates/1',
|
||||||
|
},
|
||||||
|
data: 'An error occurred',
|
||||||
|
status: 404,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
modal.find('button[aria-label="delete"]').invoke('onClick')();
|
||||||
|
await sleep(1);
|
||||||
|
wrapper.update();
|
||||||
|
const errorModal = wrapper.find('ErrorDetail__Expandable');
|
||||||
|
expect(errorModal.length).toBe(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user