Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] fixed es-lint issues #4223

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 123 additions & 126 deletions app/assets/javascripts/initializers/initializeAllFollowButts.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,59 @@
function initializeAllFollowButts() {
var followButts = document.getElementsByClassName('follow-action-button');
for (var i = 0; i < followButts.length; i++) {
initializeFollowButt(followButts[i]);
}
}
/* eslint-disable no-param-reassign */

//private
'use strict';

function initializeFollowButt(butt) {
var user = userData();
var deviceWidth = window.innerWidth > 0 ? window.innerWidth : screen.width;
var buttInfo = JSON.parse(butt.dataset.info);
var userStatus = document
.getElementsByTagName('body')[0]
.getAttribute('data-user-status');
if (userStatus === 'logged-out') {
addModalEventListener(butt);
return;
}
if (buttInfo.className === 'Tag' && user) {
handleTagButtAssignment(user, butt, buttInfo);
return;
// private

function addFollowText(butt, style) {
if (style === 'small') {
butt.textContent = '+';
} else if (style === 'follow-back') {
butt.textContent = '+ FOLLOW BACK';
} else {
if (butt.dataset.fetched === 'fetched') {
return;
}
fetchButt(butt, buttInfo);
butt.textContent = '+ FOLLOW';
}
}

function addModalEventListener(butt) {
assignState(butt, 'login');
butt.onclick = function(e) {
e.preventDefault();
showModal('follow-button');
return;
};
function addFollowingText(butt, style) {
if (style === 'small') {
butt.textContent = '✓';
} else {
butt.textContent = '✓ FOLLOWING';
}
}

function fetchButt(butt, buttInfo) {
butt.dataset.fetched = 'fetched';
var dataRequester;
if (window.XMLHttpRequest) {
dataRequester = new XMLHttpRequest();
function assignState(butt, newState) {
let style = JSON.parse(butt.dataset.info).style;
butt.classList.add('showing');
if (newState === 'follow' || newState === 'follow-back') {
butt.dataset.verb = 'unfollow';
butt.classList.remove('following-butt');
if (newState === 'follow-back') {
addFollowText(butt, newState);
} else if (newState === 'follow') {
addFollowText(butt, style);
}
} else if (newState === 'login') {
addFollowText(butt, style);
} else if (newState === 'self') {
butt.dataset.verb = 'self';
butt.textContent = 'EDIT PROFILE';
} else {
dataRequester = new ActiveXObject('Microsoft.XMLHTTP');
butt.dataset.verb = 'follow';
addFollowingText(butt, style);
butt.classList.add('following-butt');
}
dataRequester.onreadystatechange = function() {
if (
dataRequester.readyState === XMLHttpRequest.DONE &&
dataRequester.status === 200
) {
addButtClickHandle(dataRequester.response, butt);
}
};
dataRequester.open(
'GET',
'/follows/' + buttInfo.id + '?followable_type=' + buttInfo.className,
true,
);
dataRequester.send();
}

function addButtClickHandle(response, butt) {
// currently lacking error handling
var buttInfo = JSON.parse(butt.dataset.info);
assignInitialButtResponse(response, butt);
butt.onclick = function(e) {
function addModalEventListener(butt) {
assignState(butt, 'login');
butt.onclick = e => {
e.preventDefault();
handleOptimisticButtRender(butt);
// eslint-disable-next-line no-undef
showModal('follow-button');
};
}

function handleTagButtAssignment(user, butt, buttInfo) {
var buttAssignmentBoolean =
JSON.parse(user.followed_tags)
.map(function(a) {
return a.id;
})
.indexOf(buttInfo.id) !== -1;
var buttAssignmentBoolText = buttAssignmentBoolean ? 'true' : 'false';
addButtClickHandle(buttAssignmentBoolText, butt);
shouldNotFetch = true;
}

function assignInitialButtResponse(response, butt) {
butt.classList.add('showing');
if (response === 'true' || response === 'mutual') {
Expand All @@ -99,88 +69,115 @@ function assignInitialButtResponse(response, butt) {
}
}

function handleFollowButtPress(butt) {
let buttonDataInfo = JSON.parse(butt.dataset.info);
let formData = new FormData();
formData.append('followable_type', buttonDataInfo.className);
formData.append('followable_id', buttonDataInfo.id);
formData.append('verb', butt.dataset.verb);
getCsrfToken().then(sendFetch('follow-creation', formData));
}

function handleOptimisticButtRender(butt) {
if (butt.dataset.verb === 'self') {
window.location.href = '/settings';
} else if (butt.dataset.verb === 'login') {
// eslint-disable-next-line no-undef
showModal('follow-button');
} else {
// Handles actual following of tags/users
try {
//lets try grab the event buttons info data attribute user id
var evFabUserId = JSON.parse(butt.dataset.info).id;
var requestVerb = butt.dataset.verb;
//now for all follow action buttons
document.querySelectorAll('.follow-action-button').forEach(function(fab) {
try {
//lets check they have info data attributes
if (fab.dataset.info) {
//and attempt to parse those, to grab that buttons info user id
var fabUserId = JSON.parse(fab.dataset.info).id;
//now does that user id match our event buttons user id?
if (fabUserId && fabUserId === evFabUserId) {
//yes - time to assign the same state!
assignState(fab, requestVerb);
}
// lets try grab the event buttons info data attribute user id
let evFabUserId = JSON.parse(butt.dataset.info).id;
let requestVerb = butt.dataset.verb;
// now for all follow action buttons
document.querySelectorAll('.follow-action-button').forEach(fab => {
// lets check they have info data attributes
if (fab.dataset.info) {
// and attempt to parse those, to grab that buttons info user id
let fabUserId = JSON.parse(fab.dataset.info).id;
// now does that user id match our event buttons user id?
if (fabUserId && fabUserId === evFabUserId) {
// yes - time to assign the same state!
assignState(fab, requestVerb);
}
} catch (err) {
return;
}
});
} catch (err) {
return;
}

handleFollowButtPress(butt);
}
}

function handleFollowButtPress(butt) {
var buttonDataInfo = JSON.parse(butt.dataset.info);
var formData = new FormData();
formData.append('followable_type', buttonDataInfo.className);
formData.append('followable_id', buttonDataInfo.id);
formData.append('verb', butt.dataset.verb);
getCsrfToken().then(sendFetch('follow-creation', formData));
function addButtClickHandle(response, butt) {
// currently lacking error handling
let buttInfo = JSON.parse(butt.dataset.info);
assignInitialButtResponse(response, butt);
butt.onclick = e => {
e.preventDefault();
handleOptimisticButtRender(butt);
};
}

function assignState(butt, newState) {
var style = JSON.parse(butt.dataset.info).style;
butt.classList.add('showing');
if (newState === 'follow' || newState === 'follow-back') {
butt.dataset.verb = 'unfollow';
butt.classList.remove('following-butt');
if (newState === 'follow-back') {
addFollowText(butt, newState);
} else if (newState === 'follow') {
addFollowText(butt, style);
}
} else if (newState === 'login') {
addFollowText(butt, style);
} else if (newState === 'self') {
butt.dataset.verb = 'self';
butt.textContent = 'EDIT PROFILE';
} else {
butt.dataset.verb = 'follow';
addFollowingText(butt, style);
butt.classList.add('following-butt');
}
function handleTagButtAssignment(user, butt, buttInfo) {
let buttAssignmentBoolean =
JSON.parse(user.followed_tags)
.map(followedTag => followedTag.id)
.indexOf(buttInfo.id) !== -1;
let buttAssignmentBoolText = buttAssignmentBoolean ? 'true' : 'false';
addButtClickHandle(buttAssignmentBoolText, butt);
// eslint-disable-next-line no-undef
shouldNotFetch = true;
}

function addFollowText(butt, style) {
if (style === 'small') {
butt.textContent = '+';
} else if (style === 'follow-back') {
butt.textContent = '+ FOLLOW BACK';
function fetchButt(butt, buttInfo) {
butt.dataset.fetched = 'fetched';
let dataRequester;
if (window.XMLHttpRequest) {
dataRequester = new XMLHttpRequest();
} else {
butt.textContent = '+ FOLLOW';
dataRequester = new ActiveXObject('Microsoft.XMLHTTP');
}
dataRequester.onreadystatechange = () => {
if (
dataRequester.readyState === XMLHttpRequest.DONE &&
dataRequester.status === 200
) {
addButtClickHandle(dataRequester.response, butt);
}
};
dataRequester.open(
'GET',
'/follows/' + buttInfo.id + '?followable_type=' + buttInfo.className,
true,
);
dataRequester.send();
}

function addFollowingText(butt, style) {
if (style === 'small') {
butt.textContent = '✓';
function initializeFollowButt(butt) {
let user = userData();
// eslint-disable-next-line no-restricted-globals
let deviceWidth = window.innerWidth > 0 ? window.innerWidth : screen.width;
let buttInfo = JSON.parse(butt.dataset.info);
let userStatus = document
.getElementsByTagName('body')[0]
.getAttribute('data-user-status');
if (userStatus === 'logged-out') {
addModalEventListener(butt);
return;
}
if (buttInfo.className === 'Tag' && user) {
handleTagButtAssignment(user, butt, buttInfo);
} else {
butt.textContent = '✓ FOLLOWING';
if (butt.dataset.fetched === 'fetched') {
return;
}
fetchButt(butt, buttInfo);
}
}

function initializeAllFollowButts() {
let followButts = document.getElementsByClassName('follow-action-button');
followButts.forEach(initializeFollowButt);
}