Skip to content

Commit

Permalink
feat(restapi): add emailSend action (#2042)
Browse files Browse the repository at this point in the history
* feat(restapi): add emailSend action

* added emailSend reducer

Co-authored-by: Giulia Ghisini <giulia.ghisini@redturtle.it>
  • Loading branch information
nzambello and giuliaghisini authored Dec 4, 2020
1 parent ac97e22 commit 4985607
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
### Feature

- Generate language file of added missing German translations by @tisto. @ksuess
- Add emailSend action @nzambello

### Bugfix

Expand Down
33 changes: 33 additions & 0 deletions src/actions/emailSend/emailSend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* emailNotification actions.
* @module actions/emailNotification/emailNotification
*/

import { EMAIL_SEND } from '@plone/volto/constants/ActionTypes';

/**
* Email send function
* @function emailSend
* @param {string} name New password.
* @param {string} from Sender mail address.
* @param {string} to Receiver mail address.
* @param {string} subject Email subject.
* @param {string} message Email message.
* @returns {Object} Edit emailSend action.
*/
export function emailSend(name, from, to, subject, message) {
return {
type: EMAIL_SEND,
request: {
op: 'post',
path: '/@email-send',
data: {
name,
from,
to,
subject,
message,
},
},
};
}
27 changes: 27 additions & 0 deletions src/actions/emailSend/emailSend.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { emailSend } from './emailSend';
import { EMAIL_SEND } from '@plone/volto/constants/ActionTypes';

describe('Send email message', () => {
describe('emailSend', () => {
it('should create an action to send a mail notification', () => {
const action = emailSend(
'John Doe',
'john@doe.com',
'jane@doe.com',
'Hello!',
'Just want to say hi.',
);

expect(action.type).toEqual(EMAIL_SEND);
expect(action.request.op).toEqual('post');
expect(action.request.path).toEqual('/@email-send');
expect(action.request.data).toEqual({
name: 'John Doe',
from: 'john@doe.com',
to: 'jane@doe.com',
subject: 'Hello!',
message: 'Just want to say hi.',
});
});
});
});
1 change: 1 addition & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export {
} from '@plone/volto/actions/controlpanels/controlpanels';
export { getDiff } from '@plone/volto/actions/diff/diff';
export { emailNotification } from '@plone/volto/actions/emailNotification/emailNotification';
export { emailSend } from '@plone/volto/actions/emailSend/emailSend';
export {
createGroup,
deleteGroup,
Expand Down
1 change: 1 addition & 0 deletions src/constants/ActionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const DELETE_CONTENT = 'DELETE_CONTENT';
export const DELETE_GROUP = 'DELETE_GROUP';
export const DELETE_USER = 'DELETE_USER';
export const EMAIL_NOTIFICATION = 'EMAIL_NOTIFICATION';
export const EMAIL_SEND = 'EMAIL_SEND';
export const EXPAND_TOOLBAR = 'EXPAND_TOOLBAR';
export const INITIAL_PASSWORD = 'INITIAL_PASSWORD';
export const GET_BREADCRUMBS = 'GET_BREADCRUMBS';
Expand Down
47 changes: 47 additions & 0 deletions src/reducers/emailSend/emailSend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* emailSend reducer.
* @module reducers/emailSend/emailSend
*/

import { EMAIL_SEND } from '@plone/volto/constants/ActionTypes';

const initialState = {
error: null,
loaded: false,
loading: false,
};

/**
* emailSend reducer.
* @function emailSend
* @param {Object} state Current state.
* @param {Object} action Action to be handled.
* @returns {Object} New state.
*/
export default function emailSend(state = initialState, action = {}) {
switch (action.type) {
case `${EMAIL_SEND}_PENDING`:
return {
...state,
error: null,
loaded: false,
loading: true,
};
case `${EMAIL_SEND}_SUCCESS`:
return {
...state,
error: null,
loaded: true,
loading: false,
};
case `${EMAIL_SEND}_FAIL`:
return {
...state,
error: action.error,
loaded: false,
loading: false,
};
default:
return state;
}
}
49 changes: 49 additions & 0 deletions src/reducers/emailSend/emailSend.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import emailSend from './emailSend';
import { EMAIL_SEND } from '@plone/volto/constants/ActionTypes';

describe('emailSend reducer', () => {
it('should return the initial state', () => {
expect(emailSend()).toEqual({
error: null,
loaded: false,
loading: false,
});
});

it('should handle EMAIL_SEND_PENDING', () => {
expect(
emailSend(undefined, {
type: `${EMAIL_SEND}_PENDING`,
}),
).toEqual({
error: null,
loaded: false,
loading: true,
});
});

it('should handle EMAIL_SEND_SUCCESS', () => {
expect(
emailSend(undefined, {
type: `${EMAIL_SEND}_SUCCESS`,
}),
).toEqual({
error: null,
loaded: true,
loading: false,
});
});

it('should handle EMAIL_SEND_FAIL', () => {
expect(
emailSend(undefined, {
type: `${EMAIL_SEND}_FAIL`,
error: 'failed',
}),
).toEqual({
error: 'failed',
loaded: false,
loading: false,
});
});
});
2 changes: 2 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import controlpanels from '@plone/volto/reducers/controlpanels/controlpanels';
import clipboard from '@plone/volto/reducers/clipboard/clipboard';
import diff from '@plone/volto/reducers/diff/diff';
import emailNotification from '@plone/volto/reducers/emailNotification/emailNotification';
import emailSend from '@plone/volto/reducers/emailSend/emailSend';
import form from '@plone/volto/reducers/form/form';
import history from '@plone/volto/reducers/history/history';
import groups from '@plone/volto/reducers/groups/groups';
Expand Down Expand Up @@ -59,6 +60,7 @@ const reducers = {
clipboard,
diff,
emailNotification,
emailSend,
form,
groups,
history,
Expand Down

0 comments on commit 4985607

Please sign in to comment.