Retrospring/app/javascript/packs/legacy/answerbox/subscribe.coffee

29 lines
1.0 KiB
CoffeeScript
Raw Normal View History

2015-04-20 18:12:11 -07:00
# the laziest coding known to man
# TODO: so lazy, I don't know how to localize it properly
2015-04-20 18:12:11 -07:00
$(document).on "click", "a[data-action=ab-submarine]", (ev) ->
ev.preventDefault()
btn = $(this)
aid = btn[0].dataset.aId
torpedo = 0
if btn[0].dataset.torpedo == "yes"
torpedo = 1
2015-06-08 07:49:47 -07:00
2015-04-20 18:12:11 -07:00
endpoint = "subscribe"
if torpedo == 0
endpoint = "un" + endpoint
2015-06-08 07:49:47 -07:00
2015-04-20 18:12:11 -07:00
$.ajax
url: '/ajax/' + endpoint # TODO: find a way to use rake routes instead of hardcoding them here
type: 'POST'
data:
answer: aid
success: (data, status, jqxhr) ->
if data.success
btn[0].dataset.torpedo = ["yes", "no"][torpedo]
2015-06-08 07:49:47 -07:00
btn[0].children[0].nextSibling.textContent = if torpedo then translate("views.actions.unsubscribe") else translate("views.actions.subscribe")
showNotification translate("frontend.subscription.#{endpoint}"), true
2015-04-20 18:12:11 -07:00
else
2015-06-08 07:49:47 -07:00
showNotification translate("frontend.subscription.fail.#{endpoint}"), false
error: showNotificationXHRError
2015-04-20 18:12:11 -07:00
complete: (jqxhr, status) ->