only submit customized notification messages if changed

This commit is contained in:
Keith Grant
2020-08-20 14:43:30 -07:00
parent 19fc0d9a96
commit 458d29a579
2 changed files with 57 additions and 23 deletions

View File

@@ -2,7 +2,7 @@ import React, { useState } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import { CardBody } from '../../../components/Card'; import { CardBody } from '../../../components/Card';
import { OrganizationsAPI } from '../../../api'; import { NotificationTemplatesAPI } from '../../../api';
import NotificationTemplateForm from '../shared/NotificationTemplateForm'; import NotificationTemplateForm from '../shared/NotificationTemplateForm';
@@ -11,23 +11,9 @@ function NotificationTemplateEdit({ template, defaultMessages }) {
const history = useHistory(); const history = useHistory();
const [formError, setFormError] = useState(null); const [formError, setFormError] = useState(null);
const handleSubmit = async ( const handleSubmit = async values => {
values,
groupsToAssociate,
groupsToDisassociate
) => {
try { try {
await OrganizationsAPI.update(template.id, values); await NotificationTemplatesAPI.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)
)
);
history.push(detailsUrl); history.push(detailsUrl);
} catch (error) { } catch (error) {
setFormError(error); setFormError(error);

View File

@@ -98,7 +98,8 @@ function NotificationTemplateForm({
i18n, i18n,
}) { }) {
const handleSubmit = values => { const handleSubmit = values => {
onSubmit(normalizeTypeFields(values)); // TODO: convert list values to arrays (do it in the field itself?)
onSubmit(normalizeFields(values, defaultMessages));
}; };
let emailOptions = ''; let emailOptions = '';
@@ -225,24 +226,28 @@ function isCustomized(message, defaultMessage) {
if (!message) { if (!message) {
return false; return false;
} }
if (!message.message || message.message !== defaultMessage.message) { if (message.message && message.message !== defaultMessage.message) {
return true; return true;
} }
if (!message.body || message.body !== defaultMessage.body) { if (message.body && message.body !== defaultMessage.body) {
return true; return true;
} }
return false; return false;
} }
function normalizeFields(values, defaultMessages) {
return normalizeTypeFields(normalizeMessageFields(values, defaultMessages));
}
/* If the user filled in some of the Type Details fields, then switched /* 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 * to a different notification type, unecessary fields may be set in the
* notification_configuration — this function strips them off */ * notification_configuration — this function strips them off */
function normalizeTypeFields(values) { function normalizeTypeFields(values) {
const stripped = {}; const stripped = {};
const fields = typeFieldNames[values.notification_type]; const fields = typeFieldNames[values.notification_type];
fields.foreach(fieldName => { fields.forEach(fieldName => {
if (typeof values[fieldName] !== 'undefined') { if (typeof values.notification_configuration[fieldName] !== 'undefined') {
stripped[fieldName] = values[fieldName]; stripped[fieldName] = values.notification_configuration[fieldName];
} }
}); });
if (values.notification_type === 'email') { if (values.notification_type === 'email') {
@@ -255,3 +260,46 @@ function normalizeTypeFields(values) {
notification_configuration: stripped, 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,
};
}