mirror of
https://github.com/ansible/awx.git
synced 2026-05-06 17:07:36 -02:30
move label requests to function
This commit is contained in:
@@ -19,6 +19,7 @@ class JobTemplateEdit extends Component {
|
|||||||
|
|
||||||
this.handleCancel = this.handleCancel.bind(this);
|
this.handleCancel = this.handleCancel.bind(this);
|
||||||
this.handleSubmit = this.handleSubmit.bind(this);
|
this.handleSubmit = this.handleSubmit.bind(this);
|
||||||
|
this.submitLabels = this.submitLabels.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleSubmit(values, newLabels = [], removedLabels = []) {
|
async handleSubmit(values, newLabels = [], removedLabels = []) {
|
||||||
@@ -27,28 +28,37 @@ class JobTemplateEdit extends Component {
|
|||||||
history,
|
history,
|
||||||
} = this.props;
|
} = 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 {
|
try {
|
||||||
await Promise.all([
|
await JobTemplatesAPI.update(id, { ...values });
|
||||||
JobTemplatesAPI.update(id, { ...values }),
|
await Promise.all([this.submitLabels(newLabels, removedLabels)]);
|
||||||
disassociatedLabels,
|
|
||||||
associatedLabels,
|
|
||||||
generatedLabels,
|
|
||||||
]);
|
|
||||||
history.push(`/templates/${type}/${id}/details`);
|
history.push(`/templates/${type}/${id}/details`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.setState({ 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() {
|
handleCancel() {
|
||||||
const {
|
const {
|
||||||
template: { id, type },
|
template: { id, type },
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ describe('<JobTemplateEdit />', () => {
|
|||||||
mountWithContexts(<JobTemplateEdit template={mockData} />);
|
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 wrapper = mountWithContexts(<JobTemplateEdit template={mockData} />);
|
||||||
const updatedTemplateData = {
|
const updatedTemplateData = {
|
||||||
name: 'new name',
|
name: 'new name',
|
||||||
@@ -47,7 +47,7 @@ describe('<JobTemplateEdit />', () => {
|
|||||||
{ disassociate: true, id: 2 },
|
{ disassociate: true, id: 2 },
|
||||||
];
|
];
|
||||||
|
|
||||||
wrapper.find('JobTemplateForm').prop('handleSubmit')(
|
await wrapper.find('JobTemplateForm').prop('handleSubmit')(
|
||||||
updatedTemplateData,
|
updatedTemplateData,
|
||||||
newLabels,
|
newLabels,
|
||||||
removedLabels
|
removedLabels
|
||||||
@@ -56,6 +56,7 @@ describe('<JobTemplateEdit />', () => {
|
|||||||
expect(JobTemplatesAPI.disassociateLabel).toHaveBeenCalledTimes(2);
|
expect(JobTemplatesAPI.disassociateLabel).toHaveBeenCalledTimes(2);
|
||||||
expect(JobTemplatesAPI.associateLabel).toHaveBeenCalledTimes(2);
|
expect(JobTemplatesAPI.associateLabel).toHaveBeenCalledTimes(2);
|
||||||
expect(JobTemplatesAPI.generateLabel).toHaveBeenCalledTimes(2);
|
expect(JobTemplatesAPI.generateLabel).toHaveBeenCalledTimes(2);
|
||||||
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should navigate to job template detail when cancel is clicked', () => {
|
test('should navigate to job template detail when cancel is clicked', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user