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

Limit resource likes #87

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
52 changes: 51 additions & 1 deletion app/assets/javascripts/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,55 @@ function scrollToTop(event) {
$('html, body').animate({scrollTop: 0}, 'slow');
}

function getCookie(cname) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment here saying that this code is copied from W3 Schools, with the link: http://www.w3schools.com/js/js_cookies.asp

var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length,c.length);
}
}
return "";
}

function setCookie(cname, cvalue, exdays) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here - thanks!

var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}

function processLike(event, target){
event.preventDefault();
resourceID = target.parentNode.parentNode.action.split('/')[4]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way you rely on the absolute URL containing 5 forward slashes ('/'), which is not a good idea. That code would break if we change our URL paths. What you know (and what is independent of the rest of the path) is that the URL ends on '<resource_id>/like'. So instead of getting the 5th element of the array, I would use 'target.parentNode.parentNode.action.split('/').slice(-2)[0]'. That way you first reduce the array to the two last elements, and then choose the resource_id.


//create shescoding_likes cookie if it does not exist
if (getCookie("_shescoding_likes") === ""){
document.cookie = "_shescoding_likes = {}";
newValue = JSON.stringify({[resourceID]: true});
setCookie("_shescoding_likes", newValue, 365);
incrementLike(event, target);
}

//if shescoding_likes cookie does exist
else {
//check if resource id exists
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure indentations in this function are right - e.g., everything in this else should be indented further.

var oldCookie = getCookie("_shescoding_likes");
var newCookie = JSON.parse(oldCookie);

if (!(newCookie.hasOwnProperty(resourceID))){
newCookie[resourceID] = true;
newValue = JSON.stringify(newCookie);
setCookie("_shescoding_likes", newValue, 365);
incrementLike(event, target);
};
};
};

function incrementLike(event, target) {
event.preventDefault();
var form = $(target).parents('form');
Expand All @@ -45,4 +94,5 @@ function incrementLike(event, target) {
$.ajax(form.attr("action"), {
type: "POST"
});
}
}

2 changes: 1 addition & 1 deletion app/views/resources/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@

// Ajax Like
$(".heart").click(function(e){
incrementLike(e, this);
processLike(e, this);
});
});
</script>
Expand Down