diff --git a/app/assets/javascripts/application.js.erb.coffee b/app/assets/javascripts/application.js.erb.coffee index 84a3a63a..f2e4e380 100644 --- a/app/assets/javascripts/application.js.erb.coffee +++ b/app/assets/javascripts/application.js.erb.coffee @@ -72,7 +72,6 @@ $(document).on "click", "button[name=ib-answer]", -> btn.button "reset" $("textarea[name=ib-answer][data-id=#{iid}]").removeAttr "readonly" -# TODO $(document).on "click", "button[name=ab-destroy]", -> btn = $(this) btn.button "loading" @@ -92,6 +91,49 @@ $(document).on "click", "button[name=ab-destroy]", -> complete: (jqxhr, status) -> btn.button "reset" +$(document).on "click", "button[name=user-action]", -> + btn = $(this) + btn.button "loading" + target = btn[0].dataset.target + action = btn[0].dataset.action + count = Number($("h4.entry-text#follower-count").html()) + + target_url = switch action + when 'follow' + count++ + '/ajax/create_friend' + when 'unfollow' + count-- + '/ajax/destroy_friend' + + success = false + + $.ajax + url: target_url + type: 'POST' + data: + screen_name: target + success: (data, status, jqxhr) -> + success = data.success + if data.success + $("h4.entry-text#follower-count").html(count) + showNotification data.message, data.success + error: (jqxhr, status, error) -> + console.log jqxhr, status, error + showNotification "An error occurred, a developer should check the console for details", false + complete: (jqxhr, status) -> + btn.button "reset" + if success + switch action + when 'follow' + btn[0].dataset.action = 'unfollow' + btn.attr 'class', 'btn btn-default btn-block' + btn.html 'Unfollow' + when 'unfollow' + btn[0].dataset.action = 'follow' + btn.attr 'class', 'btn btn-primary btn-block' + btn.html 'Follow' + $(document).on "click", "button#create-account", -> Turbolinks.visit "/sign_up"