From 04074ff372c3cd8b010451d9637a3bc4d679c397 Mon Sep 17 00:00:00 2001 From: Daniel O'Connor Date: Sat, 9 Jul 2016 21:53:33 +0930 Subject: [PATCH] Reimplement ajax like/unlike behaviour --- app/assets/javascripts/posts.js | 18 ++++++++++++++++++ app/views/posts/_single.html.haml | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/posts.js diff --git a/app/assets/javascripts/posts.js b/app/assets/javascripts/posts.js new file mode 100644 index 000000000..1097a47ed --- /dev/null +++ b/app/assets/javascripts/posts.js @@ -0,0 +1,18 @@ +$(document).ready(function () { + $('.post-like').show(); + + $('.post-like').on('ajax:success', function(event, data) { + var like_control = $('#post-' + data.id + ' .post-like'); + + $('#post-' + data.id + ' .like-count').text(data.description); + if (data.liked_by_member) { + like_control.data("method", "delete"); + like_control.attr("href", data.url); + like_control.text("Unlike"); + } else { + like_control.data("method", "post"); + like_control.attr("href", '/likes.json?post_id=' + data.id); + like_control.text("Like"); + } + }); +}); \ No newline at end of file diff --git a/app/views/posts/_single.html.haml b/app/views/posts/_single.html.haml index 9c59576d9..cd37ff196 100644 --- a/app/views/posts/_single.html.haml +++ b/app/views/posts/_single.html.haml @@ -39,8 +39,8 @@ = pluralize(post.likes.count, "like") unless post.likes.empty? - if member_signed_in? - if !post.members.include? current_member - = link_to 'Like', likes_path(Like.new, post_id: post.id, format: :json), method: :post, remote: true, class: 'post-like', style: "display: none" + = link_to 'Like', likes_path(Like.new, post_id: post.id, format: :json), method: :post, remote: true, class: 'post-like'#, style: "display: none" - else - like = post.likes.find_by(member: current_member) - if like - = link_to 'Unlike', like_path(like, format: :json), method: :delete, remote: true, class: 'post-like', style: "display: none" + = link_to 'Unlike', like_path(like, format: :json), method: :delete, remote: true, class: 'post-like'#, style: "display: none"