summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/protected_branches/protected_branch_edit.js.es6
blob: 4ff2fa5a80f758f881e91109c7943e48ded3067a (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
64
65
66
67
68
/* eslint-disable no-new, arrow-parens, no-param-reassign, no-irregular-whitespace, padded-blocks, comma-dangle, no-trailing-spaces, semi, max-len */
/* global Flash */

(global => {
  global.gl = global.gl || {};

  gl.ProtectedBranchEdit = class {
    constructor(options) {
      this.$wrap = options.$wrap;
      this.$allowedToMergeDropdown = this.$wrap.find('.js-allowed-to-merge');
      this.$allowedToPushDropdown = this.$wrap.find('.js-allowed-to-push');

      this.buildDropdowns();
    }

    buildDropdowns() {

      // Allowed to merge dropdown
      new gl.ProtectedBranchAccessDropdown({
        $dropdown: this.$allowedToMergeDropdown,
        data: gon.merge_access_levels,
        onSelect: this.onSelect.bind(this)
      });

      // Allowed to push dropdown
      new gl.ProtectedBranchAccessDropdown({
        $dropdown: this.$allowedToPushDropdown,
        data: gon.push_access_levels,
        onSelect: this.onSelect.bind(this)
      });
    }

    onSelect() {
      const $allowedToMergeInput = this.$wrap.find(`input[name="${this.$allowedToMergeDropdown.data('fieldName')}"]`);
      const $allowedToPushInput = this.$wrap.find(`input[name="${this.$allowedToPushDropdown.data('fieldName')}"]`);

      // Do not update if one dropdown has not selected any option
      if (!($allowedToMergeInput.length && $allowedToPushInput.length)) return;

      $.ajax({
        type: 'POST',
        url: this.$wrap.data('url'),
        dataType: 'json',
        data: {
          _method: 'PATCH',
          protected_branch: {
            merge_access_levels_attributes: [{
              id: this.$allowedToMergeDropdown.data('access-level-id'),
              access_level: $allowedToMergeInput.val()
            }],
            push_access_levels_attributes: [{
              id: this.$allowedToPushDropdown.data('access-level-id'),
              access_level: $allowedToPushInput.val()
            }]
          }
        },
        success: () => {
          this.$wrap.effect('highlight');
        },
        error() {
          $.scrollTo(0);
          new Flash('Failed to update branch!');
        }
      });
    }
  }

})(window);