mirror of
https://github.com/ansible/awx.git
synced 2026-01-11 10:00:01 -03:30
move label requests to function
This commit is contained in:
parent
d05c1bdd6e
commit
e3d6ee6f9e
@ -19,6 +19,7 @@ class JobTemplateEdit extends Component {
|
||||
|
||||
this.handleCancel = this.handleCancel.bind(this);
|
||||
this.handleSubmit = this.handleSubmit.bind(this);
|
||||
this.submitLabels = this.submitLabels.bind(this);
|
||||
}
|
||||
|
||||
async handleSubmit(values, newLabels = [], removedLabels = []) {
|
||||
@ -27,28 +28,37 @@ class JobTemplateEdit extends Component {
|
||||
history,
|
||||
} = this.props;
|
||||
|
||||
const disassociatedLabels = removedLabels.forEach(removedLabel =>
|
||||
JobTemplatesAPI.disassociateLabel(id, removedLabel)
|
||||
);
|
||||
const associatedLabels = newLabels
|
||||
.filter(newLabel => !newLabel.organization)
|
||||
.forEach(newLabel => JobTemplatesAPI.associateLabel(id, newLabel));
|
||||
const generatedLabels = newLabels
|
||||
.filter(newLabel => newLabel.organization)
|
||||
.forEach(newLabel => JobTemplatesAPI.generateLabel(id, newLabel));
|
||||
try {
|
||||
await Promise.all([
|
||||
JobTemplatesAPI.update(id, { ...values }),
|
||||
disassociatedLabels,
|
||||
associatedLabels,
|
||||
generatedLabels,
|
||||
]);
|
||||
await JobTemplatesAPI.update(id, { ...values });
|
||||
await Promise.all([this.submitLabels(newLabels, removedLabels)]);
|
||||
history.push(`/templates/${type}/${id}/details`);
|
||||
} catch (error) {
|
||||
this.setState({ error });
|
||||
}
|
||||
}
|
||||
|
||||
async submitLabels(newLabels, removedLabels) {
|
||||
const {
|
||||
template: { id },
|
||||
} = this.props;
|
||||
const disassociationPromises = removedLabels.map(label =>
|
||||
JobTemplatesAPI.disassociateLabel(id, label)
|
||||
);
|
||||
const associationPromises = newLabels
|
||||
.filter(label => !label.organization)
|
||||
.map(label => JobTemplatesAPI.associateLabel(id, label));
|
||||
const creationPromises = newLabels
|
||||
.filter(label => label.organization)
|
||||
.map(label => JobTemplatesAPI.generateLabel(id, label));
|
||||
|
||||
const results = await Promise.all([
|
||||
...disassociationPromises,
|
||||
...associationPromises,
|
||||
...creationPromises,
|
||||
]);
|
||||
return results;
|
||||
}
|
||||
|
||||
handleCancel() {
|
||||
const {
|
||||
template: { id, type },
|
||||
|
||||
@ -29,7 +29,7 @@ describe('<JobTemplateEdit />', () => {
|
||||
mountWithContexts(<JobTemplateEdit template={mockData} />);
|
||||
});
|
||||
|
||||
test('handleSubmit should call api update', () => {
|
||||
test('handleSubmit should call api update', async (done) => {
|
||||
const wrapper = mountWithContexts(<JobTemplateEdit template={mockData} />);
|
||||
const updatedTemplateData = {
|
||||
name: 'new name',
|
||||
@ -47,7 +47,7 @@ describe('<JobTemplateEdit />', () => {
|
||||
{ disassociate: true, id: 2 },
|
||||
];
|
||||
|
||||
wrapper.find('JobTemplateForm').prop('handleSubmit')(
|
||||
await wrapper.find('JobTemplateForm').prop('handleSubmit')(
|
||||
updatedTemplateData,
|
||||
newLabels,
|
||||
removedLabels
|
||||
@ -56,6 +56,7 @@ describe('<JobTemplateEdit />', () => {
|
||||
expect(JobTemplatesAPI.disassociateLabel).toHaveBeenCalledTimes(2);
|
||||
expect(JobTemplatesAPI.associateLabel).toHaveBeenCalledTimes(2);
|
||||
expect(JobTemplatesAPI.generateLabel).toHaveBeenCalledTimes(2);
|
||||
done();
|
||||
});
|
||||
|
||||
test('should navigate to job template detail when cancel is clicked', () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user