BigW Consortium Gitlab

notifications_form.js.coffee 1.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
class @NotificationsForm
  constructor: ->
    @removeEventListeners()
    @initEventListeners()

  removeEventListeners: ->
    $(document).off 'change', '.js-custom-notification-event'

  initEventListeners: ->
    $(document).on 'change', '.js-custom-notification-event', @toggleCheckbox

  toggleCheckbox: (e) =>
    $checkbox = $(e.currentTarget)
    $parent = $checkbox.closest('.checkbox')
    @saveEvent($checkbox, $parent)

  showCheckboxLoadingSpinner: ($parent) ->
    $parent
      .addClass 'is-loading'
      .find '.custom-notification-event-loading'
      .removeClass 'fa-check'
      .addClass 'fa-spin fa-spinner'
      .removeClass 'is-done'

  saveEvent: ($checkbox, $parent) ->
26 27
    form = $parent.parents('form:first')

28
    $.ajax(
29 30
      url: form.attr('action')
      method: form.attr('method')
31
      dataType: 'json'
32
      data: form.serialize()
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
      beforeSend: =>
        @showCheckboxLoadingSpinner($parent)
    ).done (data) ->
      $checkbox.enable()

      if data.saved
        $parent
          .find '.custom-notification-event-loading'
          .toggleClass 'fa-spin fa-spinner fa-check is-done'

        setTimeout(->
          $parent
            .removeClass 'is-loading'
            .find '.custom-notification-event-loading'
            .toggleClass 'fa-spin fa-spinner fa-check is-done'
        , 2000)