docbrown/app/assets/javascripts/initializers/initializeArticleReactions.js
Ali Spittel 52c60ce37e Feature/refactored onboarding (#3333)
* set up refactored onboarding

* create onboarding page

* add in first slide and change slide functionality

* fix test suite

* profile refactor

* profile refactor

* refactor to api

* add checkbox fields

* add checkbox fields

* remove puts

* add basic css

* add styling

* add redirect

* hide back and next at first and last slides

* test refactored onboarding

* test refactored onboarding

* remove article edits

* Fix schema

* Add deleted file back in

* Add default value for checked_t&c column

* Adjust HTML structure to keep nav buttons in place

* Fix ESLint issues on Onboarding.jsx file

* Handling for undefined or empty followedTags on getUserTags

* Fix codeclimate issues

* Fix codeclimate issues

* Fix more codeclimate issues

* Fix more codeclimate issues

* Update Onboarding snapshots

* Uncheck the CoC and T&C checkboxes on render

* Update snapshots

* Return false instead of raising error

* Update spec to use new onboarding

* Redirect to onboarding if haven't seen it yet

* Prevent redirect to onboarding from /signout_confirm

* Use assign_attributes instead of saving twice

* Move COC and T&C checkbox page to second slide

* Add 'go back to original page' functionality

* Reuse ready prototype logic

* Keep track of the last visited onboarding page

* Fix email subscription bug

* Fix overflow issue for tags page

* Remove height to prevent page container scrolling

* Check for CoC and T&C for displaying onboarding

* Add InstantClick redirect and preserve referrer in client

* Fix async update + check by using localStorage

* Turn off onboarding for tests

* Finalize design for onboarding

* Finalize design for onboarding

* Make bulk follows during onboarding

* Fix bulk follow test
2019-07-26 15:53:32 -04:00

148 lines
4.5 KiB
JavaScript

// Set reaction count to correct number
function setReactionCount(reactionName, newCount) {
var reactionClassList = document.getElementById(
'reaction-butt-' + reactionName,
).classList;
var reactionNumber = document.getElementById(
'reaction-number-' + reactionName,
);
if (newCount > 0) {
reactionClassList.add('activated');
reactionNumber.textContent = newCount;
} else {
reactionClassList.remove('activated');
reactionNumber.textContent = '0';
}
}
function showUserReaction(reactionName, animatedClass) {
document
.getElementById('reaction-butt-' + reactionName)
.classList.add('user-activated', animatedClass);
}
function hideUserReaction(reactionName) {
document
.getElementById('reaction-butt-' + reactionName)
.classList.remove('user-activated', 'user-animated');
}
function hasUserReacted(reactionName) {
return document
.getElementById('reaction-butt-' + reactionName)
.classList.contains('user-activated');
}
function getNumReactions(reactionName) {
var num = document.getElementById('reaction-number-' + reactionName)
.textContent;
if (num == '') {
return 0;
}
return parseInt(num);
}
function initializeArticleReactions() {
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);
}
function reactToArticle(articleId, reaction) {
// Visually toggle the reaction
function toggleReaction() {
var currentNum = getNumReactions(reaction);
if (hasUserReacted(reaction)) {
hideUserReaction(reaction);
setReactionCount(reaction, currentNum - 1);
} else {
showUserReaction(reaction, 'user-animated');
setReactionCount(reaction, currentNum + 1);
}
}
var userStatus = document
.getElementsByTagName('body')[0]
.getAttribute('data-user-status');
sendHapticMessage('medium');
if (userStatus == 'logged-out') {
showModal('react-to-article');
return;
} else {
toggleReaction();
document.getElementById('reaction-butt-' + reaction).disabled = true;
}
function createFormdata() {
/*
* What's not shown here is that "authenticity_token" is included in this formData.
* The logic can be seen in sendFetch.js.
*/
var formData = new FormData();
formData.append('reactable_type', 'Article');
formData.append('reactable_id', articleId);
formData.append('category', reaction);
return formData;
}
getCsrfToken()
.then(sendFetch('reaction-creation', createFormdata()))
.then(function(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;
}
})
.catch(function(error) {
toggleReaction();
document.getElementById('reaction-butt-' + reaction).disabled = false;
});
}