Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emailer: preview HTML content before sending #324

Merged
merged 4 commits into from
Feb 25, 2024
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
5 changes: 5 additions & 0 deletions craco.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ module.exports = {
],
],
},
jest: {
configure: {
setupFiles: ['<rootDir>/setup.js'],
},
},
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"axios": "^0.21.2",
"craco-antd": "^2.0.0",
"i18next": "^23.5.1",
"isomorphic-dompurify": "1.4.0",
"lodash": "^4.17.20",
"moment": "^2.29.4",
"react": "^16.13.1",
Expand Down
3 changes: 3 additions & 0 deletions setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { TextEncoder, TextDecoder } = require('util');
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
83 changes: 71 additions & 12 deletions src/components/forms/sendEmailForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,112 @@
import React from 'react';
import { Form, Input, message } from 'antd';
import React, { useState } from 'react';
import styled from 'styled-components';
import { Form, Input, Switch, message } from 'antd';
import { SubmitButton } from '../../../components/themedComponents';
import ProtectedApiClient from '../../../api/protectedApiClient';
import {
SendEmailFormValues,
SendEmailRequest,
} from '../../../components/forms/ducks/types';
import { requiredRule } from '../../../utils/formRules';
import { site } from '../../../constants';
import { useTranslation } from 'react-i18next';
import { n } from '../../../utils/stringFormat';
import DOMPurify from 'isomorphic-dompurify';

const PreviewSwitch = styled(Switch)`
display: flex;
align-items: center;
margin-bottom: 10px;
padding: 10px 5px;
`;

const EmailPreview = styled.div`
min-height: 250px;
margin-bottom: 24px;
max-height: 800px;
border: 1px solid #d9d9d9;
border-radius: 4px;
padding: 3px 11px;
resize: vertical;
overflow-y: scroll; // required for resizing
`;

interface SendEmailFormProps {
readonly emails: string[];
}

const SendEmailForm: React.FC<SendEmailFormProps> = ({ emails }) => {
const { t } = useTranslation(n(site, ['forms']), {
keyPrefix: 'volunteer_emailer',
nsMode: 'fallback',
});

const [sendEmailForm] = Form.useForm();

const [showPreview, setShowPreview] = useState<boolean>(false);
const [bodyContent, setBodyContent] = useState<string>('');
const [sanitizedBodyContent, setSanitizedBodyContent] = useState<string>('');

const togglePreview = (isShowPreview: boolean) => {
setShowPreview(isShowPreview);
if (isShowPreview) setSanitizedBodyContent(DOMPurify.sanitize(bodyContent));
};

const onFinishSendEmail = (values: SendEmailFormValues) => {
if (emails.length === 0) {
message.error(t('users_required'));
return;
}

const sendEmailRequest: SendEmailRequest = {
...values,
emails,
};

ProtectedApiClient.sendEmail(sendEmailRequest)
.then(() => {
message.success('Email sent!');
message.success(t('success'));
})
.catch((err) =>
message.error(`Email could not be sent: ${err.response.data}`),
message.error(t('response_error', { error: err.response.data })),
);
};

return (
<Form name="sendEmail" form={sendEmailForm} onFinish={onFinishSendEmail}>
<Form
name="sendEmail"
form={sendEmailForm}
onFinish={onFinishSendEmail}
onValuesChange={(changedValues, _) => {
if (changedValues.emailBody !== undefined)
setBodyContent(changedValues.emailBody);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the emailBody is undefined, would we want to prompt the user to do anything? or just ignore until they input something that's valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! In this case, emailBody would be undefined only when the user edits a field that's not the body, since changedValues only contains fields that were modified when the user inputs something. This check is just to make sure we don't accidentally clear the body when the user modifies the subject.

}}
>
<Form.Item
name="emailSubject"
rules={requiredRule('The email subject is required')}
rules={requiredRule(t('subject_required'))}
>
<Input placeholder="Email Subject" />
<Input placeholder={t('subject_placeholder')} />
</Form.Item>

<PreviewSwitch
onChange={togglePreview}
checkedChildren={t('preview.preview')}
unCheckedChildren={t('preview.raw')}
/>
<Form.Item
name="emailBody"
rules={requiredRule('The email body is required')}
rules={requiredRule(t('body_required'))}
hidden={showPreview}
>
<Input.TextArea rows={6} placeholder="Email Body" />
<Input.TextArea rows={8} placeholder={t('body_placeholder')} />
</Form.Item>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason to set the row to 8? if the user has less than 8 lines, would it just show a bunch of white spaces?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it's just making the textbox for the the email body a little bigger (see pic). No real important reason behind this, I just thought it'd be nice to have a little more space to view the email HTML (before/after):

image

image


{showPreview && (
<EmailPreview
dangerouslySetInnerHTML={{ __html: sanitizedBodyContent }}
/>
)}
<SubmitButton type="primary" htmlType="submit">
Send Email
{t('send')}
</SubmitButton>
</Form>
);
Expand Down
14 changes: 14 additions & 0 deletions src/i18n/en/forms.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,19 @@
},
"date_label": "Activity Date",
"activity_label": "Stewardship Activities"
},
"volunteer_emailer": {
"success": "Email sent!",
"users_required": "Please select users to email",
"response_error": "Email could not be sent: {{error}}",
"subject_required": "The email subject is required",
"subject_placeholder": "Email Subject",
"preview": {
"preview": "Preview",
"raw": "Raw"
},
"body_required": "The email body is required",
"body_placeholder": "Email Body",
"send": "Send Email"
}
}
Loading
Loading