also make labels work for add view

This commit is contained in:
Jake McDermott
2019-08-27 17:41:54 -04:00
parent fbe6abfb53
commit 04f7218b4a

View File

@@ -14,10 +14,14 @@ import JobTemplateForm from '../shared/JobTemplateForm';
import { JobTemplatesAPI } from '@api'; import { JobTemplatesAPI } from '@api';
function JobTemplateAdd({ history, i18n }) { function JobTemplateAdd({ history, i18n }) {
const [error, setError] = useState(null); const [formSubmitError, setFormSubmitError] = useState(null);
const handleSubmit = async values => { async function handleSubmit(values) {
setError(null); const { newLabels, removedLabels } = values;
delete values.newLabels;
delete values.removedLabels;
setFormSubmitError(null);
try { try {
const { const {
data: { id, type }, data: { id, type },
@@ -27,11 +31,30 @@ function JobTemplateAdd({ history, i18n }) {
} catch (error) { } catch (error) {
setFormSubmitError(error); setFormSubmitError(error);
} }
}; }
const handleCancel = () => { async function submitLabels(id, newLabels = [], removedLabels = []) {
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;
}
function handleCancel() {
history.push(`/templates`); history.push(`/templates`);
}; }
return ( return (
<PageSection> <PageSection>
@@ -47,7 +70,7 @@ function JobTemplateAdd({ history, i18n }) {
handleSubmit={handleSubmit} handleSubmit={handleSubmit}
/> />
</CardBody> </CardBody>
{error ? <div>error</div> : ''} {formSubmitError ? <div>formSubmitError</div> : ''}
</Card> </Card>
</PageSection> </PageSection>
); );