-
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
Open
probinson2015
wants to merge
15
commits into
shescoding:master
Choose a base branch
from
probinson2015:LimitResourceLikes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a36fd9f
added shescoding_likes to document.cookie
probinson2015 52d23c9
comments updated
probinson2015 75fe78a
ability to add multiple key value pairs to shescoding cookie
probinson2015 1960c32
added processLike function in order to process likes before decrement…
probinson2015 2a368b4
added processLike function that sends to incrementLike if resource is…
probinson2015 6b0b3e5
removed console logs
probinson2015 911663c
Merge branch 'master' into LimitResourceLikes
probinson2015 94e617b
updated indentation. modified resource id so that it doesn't rely on …
probinson2015 936135d
likes now only increase or decrease once. cookie is updating accordingly
probinson2015 5957b98
unlike route added. unlike method added to resource controller.
probinson2015 d6a2c34
Merge branch 'master' into LimitResourceLikes
a62eec8
updated to accomodate unlikes, added resource update functions
eda5ad3
remove unused controller action
41da8b0
fix typo and slice parameters.
probinson2015 902dae5
Merge branch 'master' into LimitResourceLikes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
|
Member
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(-3)[0] | ||
| if (getCookie("_shescoding_likes") === ""){ | ||
| createNewCookie(target, resourceID); | ||
| } | ||
| 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" | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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