From bb12e0a3a9e5831dde176141f502bde43f56687a Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Tue, 18 Aug 2020 12:36:28 -0700 Subject: [PATCH] fix initial values for notification type fields --- .../shared/NotificationTemplateForm.jsx | 38 ++++++++++++- .../shared/TypeInputsSubForm.jsx | 13 ++--- .../shared/typeFieldNames.js | 55 +++++++++++++++++++ 3 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 awx/ui_next/src/screens/NotificationTemplate/shared/typeFieldNames.js diff --git a/awx/ui_next/src/screens/NotificationTemplate/shared/NotificationTemplateForm.jsx b/awx/ui_next/src/screens/NotificationTemplate/shared/NotificationTemplateForm.jsx index 078c2aac6b..300fea5a59 100644 --- a/awx/ui_next/src/screens/NotificationTemplate/shared/NotificationTemplateForm.jsx +++ b/awx/ui_next/src/screens/NotificationTemplate/shared/NotificationTemplateForm.jsx @@ -17,11 +17,12 @@ import { getAddedAndRemoved } from '../../../util/lists'; import { required, minMaxValue } from '../../../util/validators'; import { FormColumnLayout } from '../../../components/FormLayout'; import TypeInputsSubForm from './TypeInputsSubForm'; +import typeFieldNames, { initialConfigValues } from './typeFieldNames'; import { NotificationTemplate } from '../../../types'; function NotificationTemplateFormFields({ i18n, defaultMessages }) { const [orgField, orgMeta, orgHelpers] = useField('organization'); - const [typeField, typeMeta, typeHelpers] = useField({ + const [typeField, typeMeta] = useField({ name: 'notification_type', validate: required(i18n._(t`Select a value for this field`), i18n), }); @@ -97,16 +98,25 @@ function NotificationTemplateForm({ i18n, }) { const handleSubmit = values => { - console.log(values); - // onSubmit(values); + onSubmit(normalizeTypeFields(values)); }; + let emailOptions = ''; + if (template.notification_type === 'email') { + emailOptions = template.notification_configuration.use_ssl ? 'ssl' : 'tls'; + } + return ( @@ -144,3 +154,25 @@ NotificationTemplateForm.defaultProps = { }; export default withI18n()(NotificationTemplateForm); + +/* If the user filled in some of the Type Details fields, then switched + * to a different notification type, unecessary fields may be set in the + * notification_configuration — this function strips them off */ +function normalizeTypeFields(values) { + const stripped = {}; + const fields = typeFieldNames[values.notification_type]; + fields.foreach(fieldName => { + if (typeof values[fieldName] !== 'undefined') { + stripped[fieldName] = values[fieldName]; + } + }); + if (values.notification_type === 'email') { + stripped.use_ssl = values.emailOptions === 'ssl'; + stripped.use_tls = !stripped.use_ssl; + } + + return { + ...values, + notification_configuration: stripped, + }; +} diff --git a/awx/ui_next/src/screens/NotificationTemplate/shared/TypeInputsSubForm.jsx b/awx/ui_next/src/screens/NotificationTemplate/shared/TypeInputsSubForm.jsx index 1ed9eb2724..253430da8d 100644 --- a/awx/ui_next/src/screens/NotificationTemplate/shared/TypeInputsSubForm.jsx +++ b/awx/ui_next/src/screens/NotificationTemplate/shared/TypeInputsSubForm.jsx @@ -1,10 +1,9 @@ import React from 'react'; import { withI18n } from '@lingui/react'; import { t } from '@lingui/macro'; -import { useField } from 'formik'; +import { useField, useFormikContext } from 'formik'; import { FormGroup, Title } from '@patternfly/react-core'; import { - FormCheckboxLayout, FormColumnLayout, FormFullWidthLayout, SubFormLayout, @@ -418,7 +417,7 @@ function TwilioFields({ i18n }) { { + typeFieldNames[key].forEach(fieldName => { + initialConfigValues[fieldName] = ''; + }); +}); + +export { initialConfigValues };