From c74b5030c67eae26d6e443f71db4294d5ba54e74 Mon Sep 17 00:00:00 2001 From: Vincent Tam Date: Wed, 31 Jul 2019 04:24:50 +0200 Subject: [PATCH] Staticman - try to avoid spam by using javascript to write the form action (#521) --- _includes/staticman-comments.html | 15 +++++++-- _includes/staticman-script.html | 47 --------------------------- css/staticman.css | 7 ---- js/staticman.js | 54 +++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 57 deletions(-) delete mode 100644 _includes/staticman-script.html create mode 100644 js/staticman.js diff --git a/_includes/staticman-comments.html b/_includes/staticman-comments.html index 83f5c30..d83d1c5 100644 --- a/_includes/staticman-comments.html +++ b/_includes/staticman-comments.html @@ -20,7 +20,7 @@

{{ site.data.ui-text[site.locale].comments_label | default: "Leave a Comment" }}

{{ site.data.ui-text[site.locale].comment_form_info | default: "Your email address will not be published. Required fields are marked" }} *

-
+

@@ -48,7 +48,8 @@
{% if site.staticman.reCaptcha.siteKey %} @@ -58,6 +59,7 @@ {% endif %}
+
@@ -67,6 +69,13 @@ {% endif %} + - {% include staticman-script.html %} + + + {% endif %} diff --git a/_includes/staticman-script.html b/_includes/staticman-script.html deleted file mode 100644 index 4beea11..0000000 --- a/_includes/staticman-script.html +++ /dev/null @@ -1,47 +0,0 @@ -{% if site.staticman.repository and site.staticman.branch %} - - - -{% endif %} diff --git a/css/staticman.css b/css/staticman.css index 3771595..c0429ec 100644 --- a/css/staticman.css +++ b/css/staticman.css @@ -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 ========================================================================== */ diff --git a/js/staticman.js b/js/staticman.js new file mode 100644 index 0000000..7472722 --- /dev/null +++ b/js/staticman.js @@ -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);