diff --git a/awx/ui_next/src/screens/NotificationTemplate/NotificationTemplateEdit/NotificationTemplateEdit.jsx b/awx/ui_next/src/screens/NotificationTemplate/NotificationTemplateEdit/NotificationTemplateEdit.jsx index 9445f9c7be..91844a477c 100644 --- a/awx/ui_next/src/screens/NotificationTemplate/NotificationTemplateEdit/NotificationTemplateEdit.jsx +++ b/awx/ui_next/src/screens/NotificationTemplate/NotificationTemplateEdit/NotificationTemplateEdit.jsx @@ -2,7 +2,7 @@ import React, { useState } from 'react'; import PropTypes from 'prop-types'; import { useHistory } from 'react-router-dom'; import { CardBody } from '../../../components/Card'; -import { OrganizationsAPI } from '../../../api'; +import { NotificationTemplatesAPI } from '../../../api'; import NotificationTemplateForm from '../shared/NotificationTemplateForm'; @@ -11,23 +11,9 @@ function NotificationTemplateEdit({ template, defaultMessages }) { const history = useHistory(); const [formError, setFormError] = useState(null); - const handleSubmit = async ( - values, - groupsToAssociate, - groupsToDisassociate - ) => { + const handleSubmit = async values => { try { - await OrganizationsAPI.update(template.id, values); - await Promise.all( - groupsToAssociate.map(id => - OrganizationsAPI.associateInstanceGroup(template.id, id) - ) - ); - await Promise.all( - groupsToDisassociate.map(id => - OrganizationsAPI.disassociateInstanceGroup(template.id, id) - ) - ); + await NotificationTemplatesAPI.update(template.id, values); history.push(detailsUrl); } catch (error) { setFormError(error); diff --git a/awx/ui_next/src/screens/NotificationTemplate/shared/NotificationTemplateForm.jsx b/awx/ui_next/src/screens/NotificationTemplate/shared/NotificationTemplateForm.jsx index f1ab928ca6..edbb591c4d 100644 --- a/awx/ui_next/src/screens/NotificationTemplate/shared/NotificationTemplateForm.jsx +++ b/awx/ui_next/src/screens/NotificationTemplate/shared/NotificationTemplateForm.jsx @@ -98,7 +98,8 @@ function NotificationTemplateForm({ i18n, }) { const handleSubmit = values => { - onSubmit(normalizeTypeFields(values)); + // TODO: convert list values to arrays (do it in the field itself?) + onSubmit(normalizeFields(values, defaultMessages)); }; let emailOptions = ''; @@ -225,24 +226,28 @@ function isCustomized(message, defaultMessage) { if (!message) { return false; } - if (!message.message || message.message !== defaultMessage.message) { + if (message.message && message.message !== defaultMessage.message) { return true; } - if (!message.body || message.body !== defaultMessage.body) { + if (message.body && message.body !== defaultMessage.body) { return true; } return false; } +function normalizeFields(values, defaultMessages) { + return normalizeTypeFields(normalizeMessageFields(values, defaultMessages)); +} + /* 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]; + fields.forEach(fieldName => { + if (typeof values.notification_configuration[fieldName] !== 'undefined') { + stripped[fieldName] = values.notification_configuration[fieldName]; } }); if (values.notification_type === 'email') { @@ -255,3 +260,46 @@ function normalizeTypeFields(values) { notification_configuration: stripped, }; } + +function normalizeMessageFields(values, defaults) { + if (!values.useCustomMessages) { + return values; + } + const { messages } = values; + const defs = defaults[values.notification_type]; + + const nullIfDefault = (m, d) => { + return { + message: m.message === d.message ? null : m.message, + body: m.body === d.body ? null : m.body, + }; + }; + + const nonDefaultMessages = { + started: nullIfDefault(messages.started, defs.started), + success: nullIfDefault(messages.success, defs.success), + error: nullIfDefault(messages.error, defs.error), + workflow_approval: { + approved: nullIfDefault( + messages.workflow_approval.approved, + defs.workflow_approval.approved + ), + denied: nullIfDefault( + messages.workflow_approval.denied, + defs.workflow_approval.denied + ), + running: nullIfDefault( + messages.workflow_approval.running, + defs.workflow_approval.running + ), + timed_out: nullIfDefault( + messages.workflow_approval.timed_out, + defs.workflow_approval.timed_out + ), + }, + }; + return { + ...values, + messages: nonDefaultMessages, + }; +}