diff --git a/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.test.tsx b/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.test.tsx index 1762fc0b39f8e..7626606a0bf60 100644 --- a/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.test.tsx +++ b/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.test.tsx @@ -128,4 +128,62 @@ describe('EmailParamsFields renders', () => { expect(editAction).not.toHaveBeenCalledWith('message', 'Some different default message', 0); }); + + test('when useDefaultMessage is set to true and the default message changes, the underlying message is replaced with the default message', () => { + const actionParams = { + cc: [], + bcc: [], + to: ['test@test.com'], + subject: 'test', + }; + + const editAction = jest.fn(); + const wrapper = mountWithIntl( + + ); + const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text(); + expect(text).toEqual('not the default message'); + + wrapper.setProps({ + useDefaultMessage: true, + defaultMessage: 'Some different default message', + }); + + expect(editAction).toHaveBeenCalledWith('message', 'Some different default message', 0); + }); + + test('when useDefaultMessage is set to false and the default message changes, the underlying message is not changed', () => { + const actionParams = { + cc: [], + bcc: [], + to: ['test@test.com'], + subject: 'test', + }; + + const editAction = jest.fn(); + const wrapper = mountWithIntl( + + ); + const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text(); + expect(text).toEqual('not the default message'); + + wrapper.setProps({ + useDefaultMessage: false, + defaultMessage: 'Some different default message', + }); + + expect(editAction).not.toHaveBeenCalled(); + }); }); diff --git a/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.tsx b/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.tsx index 29e2a8525ae04..a8df45ba0e33f 100644 --- a/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.tsx +++ b/x-pack/plugins/stack_connectors/public/connector_types/email/email_params.tsx @@ -29,6 +29,7 @@ export const EmailParamsFields = ({ isDisabled, onBlur = noop, showEmailSubjectAndMessage = true, + useDefaultMessage, }: ActionParamsProps) => { const { to, cc, bcc, subject, message } = actionParams; const toOptions = to ? to.map((label: string) => ({ label })) : []; @@ -42,6 +43,7 @@ export const EmailParamsFields = ({ >([false, defaultMessage]); useEffect(() => { if ( + useDefaultMessage || !actionParams?.message || (isUsingDefault && actionParams?.message === defaultMessageUsed && diff --git a/x-pack/plugins/stack_connectors/public/connector_types/server_log/server_log_params.test.tsx b/x-pack/plugins/stack_connectors/public/connector_types/server_log/server_log_params.test.tsx index 90bed8f28701a..6a5426d952897 100644 --- a/x-pack/plugins/stack_connectors/public/connector_types/server_log/server_log_params.test.tsx +++ b/x-pack/plugins/stack_connectors/public/connector_types/server_log/server_log_params.test.tsx @@ -136,4 +136,56 @@ describe('ServerLogParamsFields renders', () => { expect(editAction).not.toHaveBeenCalledWith('message', 'Some different default message', 0); }); + + test('when useDefaultMessage is set to true and the default message changes, the underlying message is replaced with the default message', () => { + const actionParams = { + level: ServerLogLevelOptions.TRACE, + }; + + const editAction = jest.fn(); + const wrapper = mountWithIntl( + + ); + const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text(); + expect(text).toEqual('not the default message'); + + wrapper.setProps({ + useDefaultMessage: true, + defaultMessage: 'Some different default message', + }); + + expect(editAction).toHaveBeenCalledWith('message', 'Some different default message', 0); + }); + + test('when useDefaultMessage is set to false and the default message changes, the underlying message is not changed', () => { + const actionParams = { + level: ServerLogLevelOptions.TRACE, + }; + + const editAction = jest.fn(); + const wrapper = mountWithIntl( + + ); + const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text(); + expect(text).toEqual('not the default message'); + + wrapper.setProps({ + useDefaultMessage: false, + defaultMessage: 'Some different default message', + }); + + expect(editAction).not.toHaveBeenCalled(); + }); }); diff --git a/x-pack/plugins/stack_connectors/public/connector_types/server_log/server_log_params.tsx b/x-pack/plugins/stack_connectors/public/connector_types/server_log/server_log_params.tsx index f9882f1a67c76..7c74245289833 100644 --- a/x-pack/plugins/stack_connectors/public/connector_types/server_log/server_log_params.tsx +++ b/x-pack/plugins/stack_connectors/public/connector_types/server_log/server_log_params.tsx @@ -14,7 +14,15 @@ import { ServerLogActionParams } from '../types'; export const ServerLogParamsFields: React.FunctionComponent< ActionParamsProps -> = ({ actionParams, editAction, index, errors, messageVariables, defaultMessage }) => { +> = ({ + actionParams, + editAction, + index, + errors, + messageVariables, + defaultMessage, + useDefaultMessage, +}) => { const { message, level } = actionParams; const levelOptions = [ { value: 'trace', text: 'Trace' }, @@ -36,6 +44,7 @@ export const ServerLogParamsFields: React.FunctionComponent< >([false, defaultMessage]); useEffect(() => { if ( + useDefaultMessage || !actionParams?.message || (isUsingDefault && actionParams?.message === defaultMessageUsed && diff --git a/x-pack/plugins/stack_connectors/public/connector_types/slack/slack_params.test.tsx b/x-pack/plugins/stack_connectors/public/connector_types/slack/slack_params.test.tsx index 6611b33e094b2..faf6dc208a116 100644 --- a/x-pack/plugins/stack_connectors/public/connector_types/slack/slack_params.test.tsx +++ b/x-pack/plugins/stack_connectors/public/connector_types/slack/slack_params.test.tsx @@ -28,4 +28,56 @@ describe('SlackParamsFields renders', () => { 'test message' ); }); + + test('when useDefaultMessage is set to true and the default message changes, the underlying message is replaced with the default message', () => { + const actionParams = { + message: 'not the default message', + }; + + const editAction = jest.fn(); + const wrapper = mountWithIntl( + + ); + const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text(); + expect(text).toEqual('not the default message'); + + wrapper.setProps({ + useDefaultMessage: true, + defaultMessage: 'Some different default message', + }); + + expect(editAction).toHaveBeenCalledWith('message', 'Some different default message', 0); + }); + + test('when useDefaultMessage is set to false and the default message changes, the underlying message is not changed', () => { + const actionParams = { + message: 'not the default message', + }; + + const editAction = jest.fn(); + const wrapper = mountWithIntl( + + ); + const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text(); + expect(text).toEqual('not the default message'); + + wrapper.setProps({ + useDefaultMessage: false, + defaultMessage: 'Some different default message', + }); + + expect(editAction).not.toHaveBeenCalled(); + }); }); diff --git a/x-pack/plugins/stack_connectors/public/connector_types/slack/slack_params.tsx b/x-pack/plugins/stack_connectors/public/connector_types/slack/slack_params.tsx index ec62b64b47961..4d219aebfe5bd 100644 --- a/x-pack/plugins/stack_connectors/public/connector_types/slack/slack_params.tsx +++ b/x-pack/plugins/stack_connectors/public/connector_types/slack/slack_params.tsx @@ -18,6 +18,7 @@ const SlackParamsFields: React.FunctionComponent { const { message } = actionParams; const [[isUsingDefault, defaultMessageUsed], setDefaultMessageUsage] = useState< @@ -25,6 +26,7 @@ const SlackParamsFields: React.FunctionComponent([false, defaultMessage]); useEffect(() => { if ( + useDefaultMessage || !actionParams?.message || (isUsingDefault && actionParams?.message === defaultMessageUsed && diff --git a/x-pack/plugins/stack_connectors/public/connector_types/teams/teams_params.test.tsx b/x-pack/plugins/stack_connectors/public/connector_types/teams/teams_params.test.tsx index ac1228ac5fda4..b730e21bb350a 100644 --- a/x-pack/plugins/stack_connectors/public/connector_types/teams/teams_params.test.tsx +++ b/x-pack/plugins/stack_connectors/public/connector_types/teams/teams_params.test.tsx @@ -29,4 +29,56 @@ describe('TeamsParamsFields renders', () => { 'test message' ); }); + + test('when useDefaultMessage is set to true and the default message changes, the underlying message is replaced with the default message', () => { + const actionParams = { + message: 'not the default message', + }; + + const editAction = jest.fn(); + const wrapper = mountWithIntl( + + ); + const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text(); + expect(text).toEqual('not the default message'); + + wrapper.setProps({ + useDefaultMessage: true, + defaultMessage: 'Some different default message', + }); + + expect(editAction).toHaveBeenCalledWith('message', 'Some different default message', 0); + }); + + test('when useDefaultMessage is set to false and the default message changes, the underlying message is not changed', () => { + const actionParams = { + message: 'not the default message', + }; + + const editAction = jest.fn(); + const wrapper = mountWithIntl( + + ); + const text = wrapper.find('[data-test-subj="messageTextArea"]').first().text(); + expect(text).toEqual('not the default message'); + + wrapper.setProps({ + useDefaultMessage: false, + defaultMessage: 'Some different default message', + }); + + expect(editAction).not.toHaveBeenCalled(); + }); }); diff --git a/x-pack/plugins/stack_connectors/public/connector_types/teams/teams_params.tsx b/x-pack/plugins/stack_connectors/public/connector_types/teams/teams_params.tsx index d2ef099bee82b..93f8d0e0fe02e 100644 --- a/x-pack/plugins/stack_connectors/public/connector_types/teams/teams_params.tsx +++ b/x-pack/plugins/stack_connectors/public/connector_types/teams/teams_params.tsx @@ -18,14 +18,15 @@ const TeamsParamsFields: React.FunctionComponent { const { message } = actionParams; useEffect(() => { - if (!message && defaultMessage && defaultMessage.length > 0) { + if (useDefaultMessage || (!message && defaultMessage && defaultMessage.length > 0)) { editAction('message', defaultMessage, index); } // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); + }, [defaultMessage]); return ( { @@ -264,6 +265,8 @@ export const ActionTypeForm = ({ )} onSummaryChange={useCallback( (summary: boolean) => { + // use the default message when a user toggles between action frequencies + setUseDefaultMessage(true); setActionFrequencyProperty('summary', summary, index); }, [setActionFrequencyProperty, index] @@ -387,6 +390,7 @@ export const ActionTypeForm = ({ ? defaultSummaryMessage : selectedActionGroup?.defaultActionMessage ?? defaultActionMessage } + useDefaultMessage={useDefaultMessage} actionConnector={actionConnector} executionMode={ActionConnectorMode.ActionForm} /> diff --git a/x-pack/plugins/triggers_actions_ui/public/types.ts b/x-pack/plugins/triggers_actions_ui/public/types.ts index 6622b35fa8920..f99573150faaf 100644 --- a/x-pack/plugins/triggers_actions_ui/public/types.ts +++ b/x-pack/plugins/triggers_actions_ui/public/types.ts @@ -207,6 +207,7 @@ export interface ActionParamsProps { errors: IErrorObject; messageVariables?: ActionVariable[]; defaultMessage?: string; + useDefaultMessage?: boolean; actionConnector?: ActionConnector; isLoading?: boolean; isDisabled?: boolean;