From f65ce65ddd4a52d30b8e91c06315520362bdecce Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 19 May 2018 21:11:58 -0600 Subject: [PATCH] Add likebutton.js --- resources/assets/js/components/likebutton.js | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 resources/assets/js/components/likebutton.js diff --git a/resources/assets/js/components/likebutton.js b/resources/assets/js/components/likebutton.js new file mode 100644 index 000000000..ed955f4ea --- /dev/null +++ b/resources/assets/js/components/likebutton.js @@ -0,0 +1,29 @@ +$(document).ready(function() { + if(!ls.get('likes')) { + ls.set('likes', []); + } + + $('.like-form').submit(function(e) { + e.preventDefault(); + var el = $(this); + var id = el.data('id'); + var res = axios.post('/i/like', {item: id}); + var likes = ls.get('likes'); + var action = false; + var counter = el.parent().parent().find('.like-count'); + var count = parseInt(counter.text()); + if(likes.indexOf(id) > -1) { + likes.splice(id, 1); + count--; + counter.text(count); + action = 'unlike'; + } else { + likes.push(id); + count++; + counter.text(count); + action = 'like'; + } + ls.set('likes', likes); + console.log(action + ' - ' + $(this).data('id') + ' like event'); + }); +}); \ No newline at end of file