Skip to content

Limit resource likes #87

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

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

function incrementLike(event, target) {
//getCookie function taken from http://www.w3schools.com/js/js_cookies.asp
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 "";
}

//setCookie function taken from http://www.w3schools.com/js/js_cookies.asp
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 createNewCookie(target, resourceID){
document.cookie = "_shescoding_likes = {}";
newValue = JSON.stringify({[resourceID]: true});
setCookie("_shescoding_likes", newValue, 365);
incrementLikeinDb(target);
}

function processLike(event, target){
event.preventDefault();
var form = $(target).parents('form');
resourceID = target.parentNode.parentNode.action.split('/').slice(-2)[0]
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be slice(-3)[0]. My mistake for suggesting slice(-2)[0] in an earlier review. I think at that point that did work, because resources without likes did end on '...//like. It would not have worked when trying to process likes of resources with previous likes.
Either way, as of the current status, each URL ends on a number behind /like, so slice(-3) should always give the correct result.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. Sounds good.

if (getCookie("_shescoding_likes") === ""){
createNewCookie(target, resrouceID);
Copy link
Member

Choose a reason for hiding this comment

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

Typo, 'resrouceID' should be 'resourceID'.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ha, thank you. will update!

}
else {
updateExistingCookie(target, resourceID);
};
};

function updateLikeInDb(target, direction) {
updateLikesHtmlNumber(target, direction);
let form = $(target).parents('form');
let incrementUrl = form.attr("action").substring(0, form.attr("action").length-1) + direction;
$.ajax(incrementUrl, {
type: "POST"
});
}

function updateExistingCookie(target, resourceID){
let direction;
var oldCookie = getCookie("_shescoding_likes");
var newCookie = JSON.parse(oldCookie);
//if resourceId does not exist in the cookie (not liked yet) add it to the cookie and increment the like
if (!(newCookie.hasOwnProperty(resourceID))){
newCookie[resourceID] = true;
direction = 1;
} else {
//if resource id does exist in the cookie (liked), remove that resource id from cookie
delete newCookie[resourceID];
direction = -1;
}
newValue = JSON.stringify(newCookie);
setCookie("_shescoding_likes", newValue, 365);
updateLikeInDb(target, direction);
}

function updateLikesHtmlNumber(target, direction){
let form = $(target).parents('form');
var counterEl = form.find('span')[0];
var newCount = 1 + parseInt(counterEl.innerText, 10);
counterEl.innerText = newCount;
var button = form.find('button');
var newCount = parseInt(counterEl.innerText, 10) + direction;

if (newCount >= 0){
counterEl.innerText = newCount;
}

if (newCount === 0) {
button.removeClass('filled-heart');
button.addClass('heart');
}

if (newCount === 1) {
var button = form.find('button');
button.addClass('filled-heart');
}
}
}

$.ajax(form.attr("action"), {
type: "POST"
});
}
2 changes: 1 addition & 1 deletion app/controllers/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def update
end

def like
@resource.likes += 1
@resource.likes += params[:count].to_i
@resource.save
redirect_to resources_path
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/resources/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<div class="panel-data">
<div class="date"><%= format_date(resource.date) %></div>
<div class="likes">
<%= button_to like_path(resource.id), method: :post, class: "heart #{empty_or_full_heart(resource)}" do %>
<%= button_to like_path(resource.id, 0), method: :post, class: "heart #{empty_or_full_heart(resource)}" do %>
<span><%= resource.likes %></span>
<% end %>
</div>
Expand Down Expand Up @@ -79,7 +79,7 @@

// Ajax Like
$(".heart").click(function(e){
incrementLike(e, this);
processLike(e, this);
});
});
</script>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

resources :resources, only: [:index]
get 'resources/tags/:tag', to: 'resources#index', as: :tag
post 'resources/:id/like', to: 'resources#like', as: :like
post 'resources/:id/like/:count', to: 'resources#like', as: :like

# Static pages
get 'about', to: "static_pages#about"
Expand Down