From 04f7218b4aa7cdc4facc4725ad080a7fc38f9163 Mon Sep 17 00:00:00 2001 From: Jake McDermott Date: Tue, 27 Aug 2019 17:41:54 -0400 Subject: [PATCH] also make labels work for add view --- .../JobTemplateAdd/JobTemplateAdd.jsx | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/awx/ui_next/src/screens/Template/JobTemplateAdd/JobTemplateAdd.jsx b/awx/ui_next/src/screens/Template/JobTemplateAdd/JobTemplateAdd.jsx index 5baad8382a..b3b4d12f92 100644 --- a/awx/ui_next/src/screens/Template/JobTemplateAdd/JobTemplateAdd.jsx +++ b/awx/ui_next/src/screens/Template/JobTemplateAdd/JobTemplateAdd.jsx @@ -14,10 +14,14 @@ import JobTemplateForm from '../shared/JobTemplateForm'; import { JobTemplatesAPI } from '@api'; function JobTemplateAdd({ history, i18n }) { - const [error, setError] = useState(null); + const [formSubmitError, setFormSubmitError] = useState(null); - const handleSubmit = async values => { - setError(null); + async function handleSubmit(values) { + const { newLabels, removedLabels } = values; + delete values.newLabels; + delete values.removedLabels; + + setFormSubmitError(null); try { const { data: { id, type }, @@ -27,11 +31,30 @@ function JobTemplateAdd({ history, i18n }) { } catch (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`); - }; + } return ( @@ -47,7 +70,7 @@ function JobTemplateAdd({ history, i18n }) { handleSubmit={handleSubmit} /> - {error ?
error
: ''} + {formSubmitError ?
formSubmitError
: ''}
);