diff --git a/app/assets/javascripts/initializers/initializeArticleReactions.js b/app/assets/javascripts/initializers/initializeArticleReactions.js index 2279bf7ba395..5ddc98423331 100644 --- a/app/assets/javascripts/initializers/initializeArticleReactions.js +++ b/app/assets/javascripts/initializers/initializeArticleReactions.js @@ -1,3 +1,7 @@ +/* global sendHapticMessage, showModal */ + +'use strict'; + // Set reaction count to correct number function setReactionCount(reactionName, newCount) { var reactionClassList = document.getElementById( @@ -36,62 +40,10 @@ function hasUserReacted(reactionName) { function getNumReactions(reactionName) { var num = document.getElementById('reaction-number-' + reactionName) .textContent; - if (num == '') { + if (num === '') { return 0; } - return parseInt(num); -} - -function initializeArticleReactions() { - setCollectionFunctionality(); - setTimeout(function() { - if (document.getElementById('article-body')) { - var articleId = document.getElementById('article-body').dataset.articleId; - if (document.getElementById('article-reaction-actions')) { - var ajaxReq; - var thisButt = this; - if (window.XMLHttpRequest) { - ajaxReq = new XMLHttpRequest(); - } else { - ajaxReq = new ActiveXObject('Microsoft.XMLHTTP'); - } - ajaxReq.onreadystatechange = function() { - if (ajaxReq.readyState == XMLHttpRequest.DONE) { - var json = JSON.parse(ajaxReq.response); - json.article_reaction_counts.forEach(function(reaction) { - setReactionCount(reaction.category, reaction.count); - }); - json.reactions.forEach(function(reaction) { - if ( - document.getElementById('reaction-butt-' + reaction.category) - ) { - showUserReaction(reaction.category, 'not-user-animated'); - } - }); - } - }; - ajaxReq.open('GET', '/reactions?article_id=' + articleId, true); - ajaxReq.send(); - } - } - var reactionButts = document.getElementsByClassName( - 'article-reaction-butt', - ); - for (var i = 0; i < reactionButts.length; i++) { - reactionButts[i].onclick = function(e) { - reactToArticle(articleId, this.dataset.category); - }; - } - if (document.getElementById('jump-to-comments')) { - document.getElementById('jump-to-comments').onclick = function(e) { - e.preventDefault(); - document.getElementById('comments').scrollIntoView({ - behavior: 'instant', - block: 'start', - }); - }; - } - }, 3); + return parseInt(num, 10); } function reactToArticle(articleId, reaction) { @@ -110,13 +62,12 @@ function reactToArticle(articleId, reaction) { .getElementsByTagName('body')[0] .getAttribute('data-user-status'); sendHapticMessage('medium'); - if (userStatus == 'logged-out') { + if (userStatus === 'logged-out') { showModal('react-to-article'); return; - } else { - toggleReaction(); - document.getElementById('reaction-butt-' + reaction).disabled = true; } + toggleReaction(); + document.getElementById('reaction-butt-' + reaction).disabled = true; function createFormdata() { /* @@ -132,39 +83,98 @@ function reactToArticle(articleId, reaction) { getCsrfToken() .then(sendFetch('reaction-creation', createFormdata())) - .then(function(response) { + .then(response => { if (response.status === 200) { return response.json().then(() => { document.getElementById('reaction-butt-' + reaction).disabled = false; }); - } else { - toggleReaction(); - document.getElementById('reaction-butt-' + reaction).disabled = false; } + toggleReaction(); + document.getElementById('reaction-butt-' + reaction).disabled = false; + return undefined; }) - .catch(function(error) { + .catch(error => { toggleReaction(); document.getElementById('reaction-butt-' + reaction).disabled = false; }); } - function setCollectionFunctionality() { if (document.getElementById('collection-link-inbetween')) { - var inbetweenLinks = document.getElementsByClassName('collection-link-inbetween'); - var inbetweenLinksLength = inbetweenLinks.length - for (var i = 0; i < inbetweenLinks.length; i++) { - inbetweenLinks[i].onclick = function(e) { + var inbetweenLinks = document.getElementsByClassName( + 'collection-link-inbetween', + ); + var inbetweenLinksLength = inbetweenLinks.length; + for (var i = 0; i < inbetweenLinks.length; i += 1) { + inbetweenLinks[i].onclick = e => { e.preventDefault(); var els = document.getElementsByClassName('collection-link-hidden'); - var elsLength = els.length - for (var i = 0; i < elsLength; i++) { + var elsLength = els.length; + for (var j = 0; j < elsLength; j += 1) { els[0].classList.remove('collection-link-hidden'); } - for (var i = 0; i < inbetweenLinksLength; i++) { - inbetweenLinks[0].className = 'collection-link-hidden' + for (var k = 0; k < inbetweenLinksLength; k += 1) { + inbetweenLinks[0].className = 'collection-link-hidden'; } - } - } + }; + } + } +} + +function requestReactionCounts(articleId) { + var ajaxReq; + if (window.XMLHttpRequest) { + ajaxReq = new XMLHttpRequest(); + } else { + ajaxReq = new ActiveXObject('Microsoft.XMLHTTP'); } -} \ No newline at end of file + ajaxReq.onreadystatechange = () => { + if (ajaxReq.readyState === XMLHttpRequest.DONE) { + var json = JSON.parse(ajaxReq.response); + json.article_reaction_counts.forEach(reaction => { + setReactionCount(reaction.category, reaction.count); + }); + json.reactions.forEach(reaction => { + if (document.getElementById('reaction-butt-' + reaction.category)) { + showUserReaction(reaction.category, 'not-user-animated'); + } + }); + } + }; + ajaxReq.open('GET', '/reactions?article_id=' + articleId, true); + ajaxReq.send(); +} + +function jumpToComments() { + document.getElementById('jump-to-comments').onclick = e => { + e.preventDefault(); + document.getElementById('comments').scrollIntoView({ + behavior: 'instant', + block: 'start', + }); + }; +} + +function initializeArticleReactions() { + setCollectionFunctionality(); + setTimeout(() => { + var articleId; + if (document.getElementById('article-body')) { + articleId = document.getElementById('article-body').dataset.articleId; + if (document.getElementById('article-reaction-actions')) { + requestReactionCounts(articleId); + } + } + var reactionButts = document.getElementsByClassName( + 'article-reaction-butt', + ); + for (var i = 0; i < reactionButts.length; i += 1) { + reactionButts[i].onclick = function addReactionOnClick(e) { + reactToArticle(articleId, this.dataset.category); + }; + } + if (document.getElementById('jump-to-comments')) { + jumpToComments(); + } + }, 3); +}