Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions ui-rs/src/settings/templates/TemplateForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,6 @@ const TemplateForm = ({ initialValues, onSubmit, onClose, title, submitLabelId,
return errors;
};

// Swapping patron for staff is an ordinary update; only clearing one is impossible,
// since PUT can set an audience but never restore the "matches both" null. So the
// choice is withheld rather than offered and then refused.
const audiences = editing && initialValues?.audience ? AUDIENCES.filter(Boolean) : AUDIENCES;

// Whether the body currently holds markup, which is not the same question as
// which content type is selected: switching to text leaves the markup alone.
const bodyIsMarkup = useRef(initialValues?.contentType === 'html');
Expand Down Expand Up @@ -335,7 +330,7 @@ const TemplateForm = ({ initialValues, onSubmit, onClose, title, submitLabelId,
id="template-audience"
name="audience"
component={Select}
dataOptions={opts('audience', audiences)}
dataOptions={opts('audience', AUDIENCES)}
label={<FormattedMessage id="ui-rs.settings.templates.field.audience" />}
/>
</Col>
Expand Down
23 changes: 5 additions & 18 deletions ui-rs/src/settings/templates/TemplateForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,26 +254,13 @@ describe('TemplateForm', () => {
expect(byId('template-purpose')).toBeDisabled();
});

describe('audience', () => {
const optionValues = () => [...byId('template-audience').options].map(opt => opt.value);

it('cannot be cleared once set, which the broker cannot restore to "both"', () => {
renderForm(jest.fn(), {
editing: true,
initialValues: { ...baseInitial, title: 'T', body: 'B', subject: 'S', labels: ['l'], audience: 'patron' },
});

expect(optionValues()).toEqual(['patron', 'staff']);
it('offers "both" when editing a template that has an audience, which PUT can now restore', () => {
renderForm(jest.fn(), {
editing: true,
initialValues: { ...baseInitial, title: 'T', body: 'B', subject: 'S', labels: ['l'], audience: 'patron' },
});

it('can still be narrowed from "both", which needs no restoring', () => {
renderForm(jest.fn(), {
editing: true,
initialValues: { ...baseInitial, title: 'T', body: 'B', subject: 'S', labels: ['l'] },
});

expect(optionValues()).toEqual(['', 'patron', 'staff']);
});
expect([...byId('template-audience').options].map(opt => opt.value)).toEqual(['', 'patron', 'staff']);
});

describe('labels', () => {
Expand Down
6 changes: 3 additions & 3 deletions ui-rs/src/settings/templates/mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export function buildUpdateTemplateBody(values = {}) {
body: values.body,
contentType: values.contentType,
labels: cleanLabels(values.labels),
subject: (values.subject ?? '').trim(),
};
// Omitted rather than sent empty: "" matches neither an audience nor the IS NULL
// "both" case, making the template unreachable. The form rejects clearing it.
// Omitted optional fields are cleared by PUT.
const subject = (values.subject ?? '').trim();
if (subject && values.purpose !== 'pullslip') updated.subject = subject;
if (values.audience) updated.audience = values.audience;
Comment thread
Copilot marked this conversation as resolved.
return updated;
}
Expand Down
11 changes: 6 additions & 5 deletions ui-rs/src/settings/templates/mapping.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ describe('buildUpdateTemplateBody', () => {
expect(buildUpdateTemplateBody(values)).not.toHaveProperty('purpose');
});

it('sends an empty subject so clearing one takes effect', () => {
// PUT leaves omitted fields untouched, so omission would silently keep the old value.
expect(buildUpdateTemplateBody({ ...values, subject: '' }).subject).toBe('');
it('omits blank optional fields', () => {
const body = buildUpdateTemplateBody({ ...values, subject: ' ', audience: '' });
expect(body).not.toHaveProperty('subject');
expect(body).not.toHaveProperty('audience');
});

it('omits a cleared audience, which would otherwise be stored as an unmatchable empty string', () => {
expect(buildUpdateTemplateBody({ ...values, audience: '' })).not.toHaveProperty('audience');
it('omits the subject for pull slips, clearing one that was stored', () => {
expect(buildUpdateTemplateBody({ ...values, purpose: 'pullslip' })).not.toHaveProperty('subject');
});
});

Expand Down
Loading