Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1686 from SumOfUs/development
Browse files Browse the repository at this point in the history
New Pension Tool workflow, Email Tool & Editor bug fixes
  • Loading branch information
venkateshf22 committed Jul 7, 2020
2 parents 4f11e41 + 5be4ca4 commit c524dc5
Show file tree
Hide file tree
Showing 15 changed files with 495 additions and 157 deletions.
49 changes: 28 additions & 21 deletions app/controllers/api/emails_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,12 @@ def create_unsafe
end

def create_pension_email
reg_endpoint = plugin.try(:registered_target_endpoint)

target_data = if reg_endpoint
targets =
EmailTool::TargetsFinder.new(
postcode: params[:postcode],
endpoint: reg_endpoint.url
).find

constituency_targets_email_params(targets)
else
pension_email_params
end

PensionEmailSender.run(params[:page_id], target_data)
@action = ManageAction.create(action_params)
@action = ManageAction.create(action_params.merge(target_name: params[:target_name]))
write_member_cookie(@action.member_id)

respond_to do |format|
format.html do
page
render template: 'api/emails/create_pension_email.js.erb', content_type: 'text/javascript'
end
format.js
Expand All @@ -58,7 +44,7 @@ def create_pension_email
def unsafe_email_params
params
.require(:email)
.permit(:body, :subject, :from_name, :from_email, :country, :consented, :email_service)
.permit(:body, :subject, :from_name, :from_email, :country, :consented, :email_service, :clicked_copy_body_button)
end

def recipient_params
Expand All @@ -68,12 +54,13 @@ def recipient_params
def email_params
params
.require(:email)
.permit(:body, :subject, :target_id, :from_email, :from_name, :country, :email_service)
.permit(:body, :subject, :target_id, :from_email, :from_name, :country, :email_service,
:clicked_copy_body_button, :consented)
end

def tracking_params
(params.to_unsafe_hash[:tracking_params] || {})
.slice(:source, :akid, :referring_akid, :referrer_id, :rid, :clicked_copy_body_button)
.slice(:source, :akid, :referring_akid, :referrer_id, :rid)
.merge(mobile_value)
end

Expand Down Expand Up @@ -118,16 +105,36 @@ def action_params
name: params[:from_name],
email: params[:from_email],
country: params[:country],
action_target: params[:target_name],
action_target: params[:to_name],
action_target_email: params[:to_email],
akid: params[:akid],
referring_akid: params[:referring_akid],
referrer_id: params[:referrer_id],
rid: params[:rid],
source: params[:source]
clicked_copy_body_button: params[:clicked_copy_body_button],
email_service: params[:email_service],
source: params[:source],
consented: params[:consented]
}
end

# def send_pension_email
#
# reg_endpoint = plugin.try(:registered_target_endpoint)
# target_data = if reg_endpoint
# targets =
# EmailTool::TargetsFinder.new(
# postcode: params[:postcode],
# endpoint: reg_endpoint.url
# ).find

# constituency_targets_email_params(targets)
# else
# pension_email_params
# end
# PensionEmailSender.run(params[:page_id], target_data)
# end

def page
@page = Page.find(params[:page_id])
end
Expand Down
20 changes: 15 additions & 5 deletions app/javascript/components/EmailEditor/EmailEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Input from '../SweetInput/SweetInput';
import FormGroup from '../Form/FormGroup';
import ErrorMessages from '../ErrorMessages';
import { FormattedMessage } from 'react-intl';
import { compact, debounce, template, isEqual } from 'lodash';
import { compact, debounce, template, isEqual, merge } from 'lodash';
import classnames from 'classnames';
import { Editor, EditorState } from 'draft-js';
import { stateFromHTML } from 'draft-js-import-html';
Expand All @@ -28,25 +28,35 @@ export default class EmailEditor extends Component {
}

static getDerivedStateFromProps(props, state) {
const fn = props.templateInterpolate;
const fn = props.templateInterpolate || interpolateVars;
let body = props.body;

if (typeof fn == 'function') {
body = fn(props.body, props.templateVars);
}

return {
let stateParams = {
header: interpolateVars(props.header, props.templateVars),
body,
footer: interpolateVars(props.footer, props.templateVars),
};

if (state.templateVars != props.templateVars) {
stateParams = merge(stateParams, {
editorState: EditorState.createWithContent(
stateFromHTML(interpolateVars(props.body, props.templateVars))
),
});
}

return stateParams;
}

shouldComponentUpdate(nextProps, nextState) {
return (
!isEqual(nextProps.templateVars, this.props.templateVars) ||
this.state.subject !== nextState.subject ||
this.state.editorState !== nextState.editorState ||
stateToHTML(this.state.editorState.getCurrentContent()) !==
stateToHTML(nextState.editorState.getCurrentContent()) ||
!isEqual(this.props.errors, nextProps.errors)
);
}
Expand Down
Loading

0 comments on commit c524dc5

Please sign in to comment.