generated from Code-4-Community/frontend-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,9 @@ module.exports = { | |
], | ||
], | ||
}, | ||
jest: { | ||
configure: { | ||
setupFiles: ['<rootDir>/setup.js'], | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}} | ||
> | ||
<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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
{showPreview && ( | ||
<EmailPreview | ||
dangerouslySetInnerHTML={{ __html: sanitizedBodyContent }} | ||
/> | ||
)} | ||
<SubmitButton type="primary" htmlType="submit"> | ||
Send Email | ||
{t('send')} | ||
</SubmitButton> | ||
</Form> | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, sincechangedValues
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.