Skip to content

Commit 555094e

Browse files
authored
State level action fixes & improvements (#941)
* Refactor state level email actions * Clean up state level refactor + fix its styles * Some small cleanup around state-level actions * Update activism director -> Jason
1 parent c3ee555 commit 555094e

File tree

20 files changed

+205
-124
lines changed

20 files changed

+205
-124
lines changed

app/assets/javascripts/application/tools/email.js.erb renamed to app/assets/javascripts/application/tools/email.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ $(document).on('ready', function() {
1414
$('#email-tool').hide();
1515
});
1616

17-
$(".state-rep-email").hide();
17+
$("#state-email-tool").on("ajax:beforeSend", function() {
18+
show_progress_bars();
19+
});
1820

19-
$("form.state-rep-lookup").on("ajax:complete", function(e, xhr, status) {
21+
$(".state-rep-lookup").on("ajax:complete", function(xhr, data, status) {
2022
var $form = $(this);
21-
var data = xhr.responseJSON;
22-
$('.state-rep-lookup').hide();
23-
$('.state-reps').replaceWith(data.content);
2423
if (status == "success") {
25-
$form.remove();
26-
$(".state-reps").html(data);
27-
2824
if ($("#action-content").length) {
2925
$(window).scrollTop( $("#action-content").offset().top ); // go to top of page if on action center site
3026
}
3127
update_tabs(1, 2);
28+
$(".progress-striped").hide();
29+
$(".address-lookup").remove();
30+
} else if (data.responseText) {
31+
show_error(data.responseText, $form);
3232
} else {
3333
show_error("Something went wrong. Please try again later.", $form);
3434
}

app/assets/javascripts/application/tools/shared.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ function show_error(error, form) {
1515
function update_tabs(from, to) {
1616
$(".page-indicator div.page" + from).removeClass('active');
1717
$(".page-indicator div.page" + to).addClass('active');
18-
}
18+
}

app/assets/stylesheets/application/action_page.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,3 +1316,9 @@ html.js #affiliations {
13161316
}
13171317
}
13181318

1319+
.state-email-body {
1320+
padding: 0.5rem 1.5rem;
1321+
form {
1322+
padding: 0;
1323+
}
1324+
}

app/controllers/tools_controller.rb

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,13 @@ def state_reps
131131
@actionPage = @email_campaign.action_page
132132
# TODO: strong params this
133133
address = "#{params[:street_address]} #{params[:zipcode]}"
134-
civic_api_response = CivicApi.state_rep_search(address, @email_campaign.leg_level)
135-
@state_reps = JSON.parse(civic_api_response.body)["officials"]
136-
state_rep_emails = []
137-
@state_reps.each do |sr|
138-
state_rep_emails << sr["emails"] unless sr["emails"].nil?
139-
end
140-
# single-rep lookup only
141-
@state_rep_email = state_rep_emails.flatten.first
142-
if @state_reps.present?
143-
render json: { content: render_to_string(partial: "action_page/state_reps") }, status: 200
144-
else
145-
render json: { error: "No representatives found" }, status: 200
134+
@state_reps = CivicApi.state_rep_search(address, @email_campaign.leg_level)
135+
136+
# Get first non-null email for a state rep
137+
@state_rep_email = @state_reps.map { |sr| sr["emails"] }.flatten.compact.first
138+
139+
unless @state_reps.present?
140+
render plain: "No representatives found", status: 200
146141
end
147142
end
148143

app/lib/civic_api.rb

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,52 @@
66
# do not allow holdbacks, A/B testing, or similar experiments."
77

88
module CivicApi
9-
VALID_ROLES = %w[legislatorLowerBody legislatorUpperBody headOfGovernment].freeze
9+
VALID_ROLES = %w[legislatorLowerBody legislatorUpperBody
10+
headOfGovernment].freeze
1011

12+
# 4/22 note - address and roles are both strings for now, as we
13+
# don't yet support multiple targets for state campaigns
1114
def self.state_rep_search(address, roles)
12-
raise ArgumentError, "required argument is nil" unless [address, roles].all?
13-
14-
raise ArgumentError, "Invalid role for Civic API #{roles}" unless VALID_ROLES.include?(roles)
15+
unless [address, roles].all?(&:present?)
16+
missing_fields = []
17+
missing_fields << :address unless address.present?
18+
missing_fields << :roles unless roles.present?
19+
raise ArgumentError, "required argument(s) `#{missing_fields.join(", ")}` is nil"
20+
end
21+
unless VALID_ROLES.include?(roles)
22+
raise ArgumentError, "Invalid role for Civic API `#{roles}`"
23+
end
1524

1625
# `includeOffices` param is needed in order to get officials list
17-
# `administrativeArea1` param restricts the search to state-level legislators (and governors)
18-
params = { address: address, includeOffices: true, levels: "administrativeArea1", roles: roles, key: civic_api_key }
26+
# `administrativeArea1` param restricts the search to state-level
27+
# legislators (and governors)
28+
params = {
29+
address: address,
30+
includeOffices: true,
31+
levels: "administrativeArea1",
32+
roles: roles,
33+
key: civic_api_key
34+
}
1935

20-
get params
36+
response = get params
37+
JSON.parse(response.body)["officials"]
2138
end
2239

2340
def self.all_state_reps_for_role(state, roles)
2441
raise ArgumentError, "required argument is nil" unless [state, roles].all?
2542

2643
# need to append division information to API route
27-
path_params = { ocdId: "ocd-division%2Fcountry%3Aus%2Fstate%3A#{state.downcase}" }
28-
# `administrativeArea1` param restricts the search to state-level legislators (and governors)
29-
query_params = { levels: "administrativeArea1", recursive: true, roles: roles, key: civic_api_key }
44+
path_params = {
45+
ocdId: "ocd-division%2Fcountry%3Aus%2Fstate%3A#{state.downcase}"
46+
}
47+
# `administrativeArea1` param restricts the search to state-level
48+
# legislators (and governors)
49+
query_params = {
50+
levels: "administrativeArea1",
51+
recursive: true,
52+
roles: roles,
53+
key: civic_api_key
54+
}
3055

3156
params = { path_params: path_params, query_params: query_params }
3257

app/views/layouts/application.html.erb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
<title><%= page_title -%></title>
1616
<%= stylesheet_link_tag "application", media: "all" %>
17+
<%= javascript_include_tag "application" %>
18+
1719
<%= yield(:head) if content_for?(:head) -%>
1820
<%= csrf_meta_tags %>
1921

@@ -97,9 +99,6 @@
9799
</div>
98100
</div>
99101

100-
101-
<%= javascript_include_tag "application" %>
102-
103102
<%= matomo_tracking_embed %>
104103

105104
<%= yield(:body) if content_for?(:body) -%>

app/views/tools/_container.html.erb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
<% else -%>
77
<%= render "tools/petition" if @actionPage.enable_petition? && @actionPage.petition %>
88
<%= render "tools/call" if @actionPage.enable_call? && @actionPage.call_campaign %>
9-
<%= render "tools/email" if @actionPage.enable_email? && @actionPage.email_campaign %>
9+
<% if @actionPage.enable_email? && @actionPage.email_campaign %>
10+
<% if @actionPage.email_campaign.state? %>
11+
<%= render "tools/state_leg_email" %>
12+
<% else %>
13+
<%= render "tools/email" %>
14+
<% end %>
15+
<% end %>
1016
<%= render "tools/congress_message" if @actionPage.enable_congress_message? && @actionPage.congress_message_campaign %>
1117
<%= render "tools/tweet" if @actionPage.enable_tweet? && @actionPage.tweet %>
1218
<%= render "tools/mailing_list" if @no_tools == true %>

app/views/tools/_email.html.erb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
<% if @email_campaign.state? %>
2-
<div class="page-indicator">
3-
<div class="page1 active">1</div>
4-
<div class="page2">2</div>
5-
<div class="page3">3</div>
6-
</div>
7-
<% end %>
81
<div id='email-tool' class="tool" data-action-id="<%= @actionPage.id %>">
92
<div class="tool-container">
103
<div class="tool-heading">
@@ -15,11 +8,7 @@
158
</div>
169

1710
<div class="email-body">
18-
<% if @email_campaign.state? %>
19-
<%= render 'tools/lookup_state_rep' %>
20-
<% else %>
21-
<%= render 'tools/send_email' %>
22-
<% end %>
11+
<%= render 'tools/send_email' %>
2312
</div>
2413
</div>
2514
</div>
Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
1-
<div class="state-rep-lookup">
1+
<div class="address-lookup">
22
<p>Enter your address below and we'll help you email your representatives (US addresses only).</p>
3-
<div class="address-lookup">
4-
<%= form_tag(tools_state_reps_path, method: :get, remote: true, class: "form state-rep-lookup") do %>
5-
<%= hidden_field_tag :email_campaign_id, @actionPage.email_campaign.id %>
3+
<%= form_tag(tools_state_reps_path, method: :post, remote: true, class: "form state-rep-lookup") do %>
4+
<%= hidden_field_tag :email_campaign_id, @actionPage.email_campaign.id %>
65

7-
<fieldset id="lookup-address">
8-
<h3>
9-
<span class="customize-message-popover" data-toggle="tooltip" data-trigger="hover">?
10-
<span class="tooltiptext">We'll use your address to find your state representatives.</span>
11-
</span>
12-
Look up your state representatives
13-
</h3>
14-
<div id="errors"></div>
6+
<fieldset id="lookup-address">
7+
<h3>
8+
<span class="customize-message-popover" data-toggle="tooltip" data-trigger="hover">?
9+
<span class="tooltiptext">We'll use your address to find your state representatives.</span>
10+
</span>
11+
Look up your state representatives
12+
</h3>
13+
<div id="errors"></div>
1514

16-
<div class="form-group">
17-
<%= text_field_tag :street_address, (current_user && current_user.street_address),
18-
placeholder: "Street Address", "aria-label": "Street Address",
19-
required: true, class: "form-control" %>
20-
</div>
15+
<div class="form-group">
16+
<%= text_field_tag :street_address, (current_user && current_user.street_address),
17+
placeholder: "Street Address", "aria-label": "Street Address",
18+
required: true, class: "form-control" %>
19+
</div>
2120

22-
<div class="form-group">
23-
<%= text_field_tag :zipcode, (current_user && current_user.zipcode),
24-
placeholder: "Zip Code", "aria-label": "Zip Code", required: true,
25-
class: "form-control", title: "Must be 5 numeric numbers",
26-
pattern: "\\d{5}", maxlength: 5 %>
27-
</div>
21+
<div class="form-group">
22+
<%= text_field_tag :zipcode, (current_user && current_user.zipcode),
23+
placeholder: "Zip Code", "aria-label": "Zip Code", required: true,
24+
class: "form-control", title: "Must be 5 numeric numbers",
25+
pattern: "\\d{5}", maxlength: 5 %>
26+
</div>
2827

29-
<div class="form-errors info-circle error hidden"></div>
28+
<div class="form-errors info-circle error hidden"></div>
3029

31-
<p class="privacy-notice">Uses <a href="https://developers.google.com/terms/api-services-user-data-policy" target="_blank">Google</a>'s APIs (
30+
<p class="privacy-notice">Uses <a href="https://developers.google.com/terms/api-services-user-data-policy" target="_blank">Google</a>'s APIs (
3231
<span class="privacy-notice-popover" data-toggle="tooltip" data-trigger="hover">why?
3332
<!-- Attribution Guidelines: https://developers.google.com/civic-information/docs/data_guidelines?hl=en#data-availability -->
3433
<span class="tooltiptext">Representative information powered by the Civic Information API.</span>
3534
</span>).
36-
</p>
37-
</fieldset>
38-
<button type="submit" class="eff-button" id="button-state-rep-lookup"role="button" aria-pressed="false">See Your Representatives</button>
39-
<%= render "tools/loading" -%>
40-
</div>
41-
<% end %>
35+
</p>
36+
</fieldset>
37+
<input type="submit" class="eff-button" value="Find your reps">
38+
<%= render "tools/loading" -%>
39+
<% end %>
4240
</div>
43-
<div class="state-reps"></div>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<div class="page-indicator">
2+
<div class="page1 active">1</div>
3+
<div class="page2">2</div>
4+
<div class="page3">3</div>
5+
</div>
6+
<div id='state-email-tool' class="tool" data-action-id="<%= @actionPage.id %>">
7+
<div class="tool-container">
8+
<div class="tool-heading">
9+
<h2 class="tool-title">Take action</h2>
10+
</div>
11+
12+
<div class="tool-body state-email-body">
13+
<%= render 'tools/lookup_state_rep' %>
14+
<div class="state-reps"></div>
15+
</div>
16+
</div>
17+
</div>
18+
19+
<div class="update-user-data-container" style="display: none;">
20+
<% if current_user %>
21+
<input checked="checked" id="update_user_data" name="update_user_data" type="checkbox" value="yes">
22+
<label for="update_user_data" value="Save my name and address for next time."></label>
23+
<% end %>
24+
</div>
25+
</div>

0 commit comments

Comments
 (0)