Skip to content

Commit

Permalink
Staticman - try to avoid spam by using javascript to write the form a…
Browse files Browse the repository at this point in the history
…ction (#521)
  • Loading branch information
VincentTam authored and daattali committed Jul 31, 2019
1 parent 35647fd commit c74b503
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 57 deletions.
15 changes: 12 additions & 3 deletions _includes/staticman-comments.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ <h3 class="page__comments-title">{{ site.data.ui-text[site.locale].comments_titl
<div class="page__comments-form">
<h3 class="page__comments-title">{{ site.data.ui-text[site.locale].comments_label | default: "Leave a Comment" }}</h3>
<p class="small">{{ site.data.ui-text[site.locale].comment_form_info | default: "Your email address will not be published. Required fields are marked" }} <span class="required">*</span></p>
<form id="new_comment" class="page__comments-form js-form form" method="post" action="{{ site.staticman.endpoint | default: 'https://staticman3.herokuapp.com/v3/entry/github/' }}{{ site.staticman.repository }}/{{ site.staticman.branch }}/comments">
<form id="new_comment" class="page__comments-form js-form form" method="post">
<div class="form-group">
<label for="comment-form-message">{{ site.data.ui-text[site.locale].comment_form_comment_label | default: "Comment" }} <small class="required">*</small></label><br>
<textarea type="text" rows="12" cols="36" id="comment-form-message" name="fields[message]" tabindex="1"></textarea>
Expand Down Expand Up @@ -48,7 +48,8 @@ <h3 class="page__comments-title">{{ site.data.ui-text[site.locale].comments_labe
</div>
<!-- Start comment form alert messaging -->
<p class="hidden js-notice">
<strong class="js-notice-text"></strong>
<strong class="js-notice-text-success hidden">{{ site.data.ui-text[site.locale].comment_success_msg | default: "Thanks for your comment! It will show on the site once it has been approved." }}</strong>
<strong class="js-notice-text-failure hidden">{{ site.data.ui-text[site.locale].comment_error_msg | default: "Sorry, there was an error with your submission. Please make sure all required fields have been completed and try again." }}</strong>
</p>
<!-- End comment form alert messaging -->
{% if site.staticman.reCaptcha.siteKey %}
Expand All @@ -58,6 +59,7 @@ <h3 class="page__comments-title">{{ site.data.ui-text[site.locale].comments_labe
{% endif %}
<div class="form-group">
<button type="submit" id="comment-form-submit" tabindex="5" class="btn btn--primary btn--large">{{ site.data.ui-text[site.locale].comment_btn_submit | default: "Submit Comment" }}</button>
<button type="submit" id="comment-form-submitted" tabindex="5" class="btn btn--primary btn--large hidden" disabled>{{ site.data.ui-text[site.locale].comment_btn_submitted | default: "Submitted" }}</button>
</div>
</form>
</div>
Expand All @@ -67,6 +69,13 @@ <h3 class="page__comments-title">{{ site.data.ui-text[site.locale].comments_labe
<script async src="https://www.google.com/recaptcha/api.js"></script>
{% endif %}
</div>

<!-- Load script to handle comment form submission -->
{% include staticman-script.html %}
<!-- doing something a bit funky here because I want to be careful not to include JQuery twice! -->
<script>
if (typeof jQuery == 'undefined') {
document.write('<script src="{{ "/js/jquery-1.11.2.min.js" | relative_url }}"></scr' + 'ipt>');
}
</script>
<script src="{{ "/js/staticman.js" | relative_url }}"></script>
{% endif %}
47 changes: 0 additions & 47 deletions _includes/staticman-script.html

This file was deleted.

7 changes: 0 additions & 7 deletions css/staticman.css
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@
.staticman-comments .form {
position: relative;
}
/*
Disabled state
========================================================================== */
.staticman-comments input[disabled][disabled], .staticman-comments textarea[disabled], .staticman-comments input[readonly][readonly], .staticman-comments textarea[readonly] {
opacity: 0.5;
cursor: not-allowed;
}
/*
Focus & active state
========================================================================== */
Expand Down
54 changes: 54 additions & 0 deletions js/staticman.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
layout: null
---

(function ($) {
var $comments = $('.js-comments');

$('#new_comment').submit(function () {
var form = this;

$(form).addClass('disabled');

{% assign sm = site.staticman -%}
var endpoint = '{{ sm.endpoint | default: "https://staticman3.herokuapp.com/v3/entry/github/" }}';
var repository = '{{ sm.repository }}';
var branch = '{{ sm.branch }}';

$.ajax({
type: $(this).attr('method'),
url: endpoint + repository + '/' + branch + '/comments',
data: $(this).serialize(),
contentType: 'application/x-www-form-urlencoded',
success: function (data) {
$('#comment-form-submit').addClass('hidden');
$('#comment-form-submitted').removeClass('hidden');
$('.page__comments-form .js-notice').removeClass('notice--danger');
$('.page__comments-form .js-notice').addClass('notice--success');
showAlert('success');
},
error: function (err) {
console.log(err);
$('#comment-form-submitted').addClass('hidden');
$('#comment-form-submit').removeClass('hidden');
$('.page__comments-form .js-notice').removeClass('notice--success');
$('.page__comments-form .js-notice').addClass('notice--danger');
showAlert('failure');
$(form).removeClass('disabled');
}
});

return false;
});

function showAlert(message) {
$('.page__comments-form .js-notice').removeClass('hidden');
if (message == 'success') {
$('.page__comments-form .js-notice-text-success').removeClass('hidden');
$('.page__comments-form .js-notice-text-failure').addClass('hidden');
} else {
$('.page__comments-form .js-notice-text-success').addClass('hidden');
$('.page__comments-form .js-notice-text-failure').removeClass('hidden');
}
}
})(jQuery);

0 comments on commit c74b503

Please sign in to comment.