-
Notifications
You must be signed in to change notification settings - Fork 35
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
base: master
Are you sure you want to change the base?
Changes from 13 commits
a36fd9f
52d23c9
75fe78a
1960c32
2a368b4
6b0b3e5
911663c
94e617b
936135d
5957b98
d6a2c34
a62eec8
eda5ad3
41da8b0
902dae5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
||
if (getCookie("_shescoding_likes") === ""){ | ||
createNewCookie(target, resrouceID); | ||
|
||
} | ||
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" | ||
}); | ||
} |
There was a problem hiding this comment.
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