summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/jira_connect.js
blob: 0864a3024ac094294c0754e6e851fae58a6ba48e (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* eslint-disable func-names, no-var, no-alert */
/* global $ */
/* global AP */

/**
 * This script is not going through Webpack bundling
 * as it is only included in `app/views/jira_connect/subscriptions/index.html.haml`
 * which is going to be rendered within iframe on Jira app dashboard
 * hence any code written here needs to be IE11+ compatible (no fully ES6)
 */

function onLoaded() {
  var reqComplete = function() {
    AP.navigator.reload();
  };

  var reqFailed = function(res) {
    alert(res.responseJSON.error);
  };

  AP.getLocation(function(location) {
    $('.js-jira-connect-sign-in').each(function() {
      var updatedLink = `${$(this).attr('href')}?return_to=${location}`;
      $(this).attr('href', updatedLink);
    });
  });

  $('#add-subscription-form').on('submit', function(e) {
    var actionUrl = $(this).attr('action');
    e.preventDefault();

    AP.context.getToken(function(token) {
      // eslint-disable-next-line no-jquery/no-ajax
      $.post(actionUrl, {
        jwt: token,
        namespace_path: $('#namespace-input').val(),
        format: 'json',
      })
        .done(reqComplete)
        .fail(reqFailed);
    });
  });

  $('.remove-subscription').on('click', function(e) {
    var href = $(this).attr('href');
    e.preventDefault();

    AP.context.getToken(function(token) {
      // eslint-disable-next-line no-jquery/no-ajax
      $.ajax({
        url: href,
        method: 'DELETE',
        data: {
          jwt: token,
          format: 'json',
        },
      })
        .done(reqComplete)
        .fail(reqFailed);
    });
  });
}
document.addEventListener('DOMContentLoaded', onLoaded);