protected_branch_edit.js 2.2 KB
Newer Older
1
/* eslint-disable no-new, arrow-parens, no-param-reassign, comma-dangle, max-len */
2 3
/* global Flash */

4
(global => {
5
  global.gl = global.gl || {};
6

7 8 9 10 11
  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');
12

13 14 15 16 17 18 19 20 21 22
      this.buildDropdowns();
    }

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

24 25 26 27 28 29 30 31 32
      // Allowed to push dropdown
      new gl.ProtectedBranchAccessDropdown({
        $dropdown: this.$allowedToPushDropdown,
        data: gon.push_access_levels,
        onSelect: this.onSelect.bind(this)
      });
    }

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

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

39 40 41
      this.$allowedToMergeDropdown.disable();
      this.$allowedToPushDropdown.disable();

42 43 44 45 46 47 48
      $.ajax({
        type: 'POST',
        url: this.$wrap.data('url'),
        dataType: 'json',
        data: {
          _method: 'PATCH',
          protected_branch: {
49 50
            merge_access_levels_attributes: [{
              id: this.$allowedToMergeDropdown.data('access-level-id'),
51
              access_level: $allowedToMergeInput.val()
52 53 54
            }],
            push_access_levels_attributes: [{
              id: this.$allowedToPushDropdown.data('access-level-id'),
55
              access_level: $allowedToPushInput.val()
56
            }]
57
          }
58 59 60 61
        },
        error() {
          $.scrollTo(0);
          new Flash('Failed to update branch!');
62
        }
63 64 65
      }).always(() => {
        this.$allowedToMergeDropdown.enable();
        this.$allowedToPushDropdown.enable();
66 67
      });
    }
68
  };
69
})(window);