move label requests to function

This commit is contained in:
Jake McDermott
2019-08-13 09:23:14 -04:00
parent d05c1bdd6e
commit e3d6ee6f9e
2 changed files with 28 additions and 17 deletions

View File

@@ -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 },

View File

@@ -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', () => {