Skip to content

Commit

Permalink
Handle newsletter subscriptions on the server side
Browse files Browse the repository at this point in the history
- protect them using altcha captcha
  • Loading branch information
crutch committed Nov 16, 2024
1 parent c5626b1 commit 4c7ea68
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 54 deletions.
1 change: 0 additions & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
//= require activestorage
//= require turbolinks
//= require cookieconsent.min
//= require newsletter_sign_up
//= require jscookie

$(document).on("turbolinks:load", function () {
Expand Down
42 changes: 0 additions & 42 deletions app/assets/javascripts/newsletter_sign_up.js

This file was deleted.

22 changes: 22 additions & 0 deletions app/controllers/newsletter_subscriptions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class NewsletterSubscriptionsController < ApplicationController

def subscribe
email, altcha = subscription_params
if AltchaSolution.verify_and_save(altcha)
SubscribeToNewsletterJob.perform_later(email, 'NewsletterSubscription')
respond_to do |format|
format.js { render :success}
end
else
respond_to do |format|
format.js { render :altcha_failure, status: :unprocessable_entity}
end
end
end

private

def subscription_params
params.require([:email, :altcha])
end
end
13 changes: 2 additions & 11 deletions app/views/components/_footer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<span class="sdn-footer__newsletter-heading"><label for="email-input">Novinky emailom</label> <small class="sdn-footer__newsletter-heading-tooltip">(raz mesačne)</small></span>
<div id="newsletter-success" style="display: none">
<p style="color: white;">
<strong>Na Váš e-mail sme poslali potvrdzovaciu správu.</strong>
<strong>Na Váš e-mail sme poslali potvrdzovaciu správu. Ak nechodí, skúste to znova.</strong>
<br>
Ďakujeme za Váš záujem.
</p>
Expand All @@ -15,16 +15,7 @@
<strong></strong>
</p>
</div>
<form id="newsletter-form" role="form" action="https://my.sendinblue.com/users/subscribe/js_id/29wti/id/4" data-action="https://my.sendinblue.com/users/subscribeembed/js_id/29wti/id/4" method="post">
<div class="govuk-form-group sdn-footer__newsletter-form-group">
<input type="hidden" name="js_id" id="js_id" value="29wti">
<input type="hidden" name="listid" id="listid" value="45">
<input type="hidden" name="from_url" id="from_url" value="yes">
<input type="hidden" name="hdn_email_txt" id="hdn_email_txt" value="">
<input type="email" name="email" id="email-input" required class="govuk-input sdn-footer__newsletter-input" placeholder="Zadajte emailovú adresu"/>
<button type="submit" class="govuk-button sdn-footer__newsletter-button">Prihlásiť</button>
</div>
</form>
<%= render 'newsletter_subscriptions/form' %>
<span class="sdn-footer__newsletter-disclaimer">
Prihlásením dávam dobrovoľný súhlas OZ Slovensko.Digital na zasielanie bezplatného newslettera na mnou zadaný e-mail. Tento súhlas môžem kdykoľvek odvolať na <%= mail_to ENV['DEFAULT_EMAIL_FROM'], ENV['DEFAULT_EMAIL_FROM'], class: 'sdn-footer__link' %>. Oboznámil(a) som sa s informáciami v sekcii <%= link_to 'Ochrana osobných údajov', page_path('ochrana-osobnych-udajov'), class: 'sdn-footer__link' %>.</span>
</div>
Expand Down
6 changes: 6 additions & 0 deletions app/views/newsletter_subscriptions/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= form_tag newsletter_subscribe_path, method: :post, remote: true, id: "newsletter-form" do |f| %>
<%= email_field_tag :email, '', class: 'govuk-input sdn-footer__newsletter-input', placeholder: "Zadajte emailovú adresu", autocomplete: :email %>
<%= submit_tag 'Prihlásiť', class: 'govuk-button sdn-footer__newsletter-button' %>
<altcha-widget hidelogo hidefooter strings="<%= JSON.dump({ 'label': 'Nie som robot', 'verifying': 'Overujem...', 'verified': 'Overené'}) %>" challengeurl="<%= altcha_url() %>"></altcha-widget>
<div class="newsletter-altcha-error"></div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/newsletter_subscriptions/failure.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$('#newsletter-form').replaceWith('<%=j render partial: 'newsletter_subscriptions/form', object: @group %>');
$('.newsletter-altcha-error').html('Nie sme si isti, či nie ste robot... zaškrtli ste, že nie ste?');
4 changes: 4 additions & 0 deletions app/views/newsletter_subscriptions/success.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$('#newsletter-success').show();
if ($('#newsletter-warning').is(':visible')) {
$('#newsletter-warning').hide();
}
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@
get :confirm, on: :member, path: 'potvrdit'
end

post 'newsletter/subscribe', to: 'newsletter_subscriptions#subscribe', as: :newsletter_subscribe

resource :session, only: [:new, :create, :destroy]
get '/auth/magiclink/info', to: 'sessions#magic_link_info'
get '/auth/failure', to: 'sessions#failure'
Expand Down

0 comments on commit 4c7ea68

Please sign in to comment.