protected_branch_edit.js 2.2 KB
Newer Older
1 2
import flash from '../flash';
import axios from '../lib/utils/axios_utils';
K
kushalpandya 已提交
3
import ProtectedBranchAccessDropdown from './protected_branch_access_dropdown';
4

K
kushalpandya 已提交
5 6 7 8 9 10
export default class ProtectedBranchEdit {
  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.onSelectCallback = this.onSelect.bind(this);
11

K
kushalpandya 已提交
12 13
    this.buildDropdowns();
  }
14

K
kushalpandya 已提交
15 16 17 18 19 20 21
  buildDropdowns() {
    // Allowed to merge dropdown
    this.protectedBranchAccessDropdown = new ProtectedBranchAccessDropdown({
      $dropdown: this.$allowedToMergeDropdown,
      data: gon.merge_access_levels,
      onSelect: this.onSelectCallback,
    });
22

K
kushalpandya 已提交
23 24 25 26 27 28 29
    // Allowed to push dropdown
    this.protectedBranchAccessDropdown = new ProtectedBranchAccessDropdown({
      $dropdown: this.$allowedToPushDropdown,
      data: gon.push_access_levels,
      onSelect: this.onSelectCallback,
    });
  }
30

K
kushalpandya 已提交
31 32 33
  onSelect() {
    const $allowedToMergeInput = this.$wrap.find(`input[name="${this.$allowedToMergeDropdown.data('fieldName')}"]`);
    const $allowedToPushInput = this.$wrap.find(`input[name="${this.$allowedToPushDropdown.data('fieldName')}"]`);
34

K
kushalpandya 已提交
35 36
    // Do not update if one dropdown has not selected any option
    if (!($allowedToMergeInput.length && $allowedToPushInput.length)) return;
37

K
kushalpandya 已提交
38 39
    this.$allowedToMergeDropdown.disable();
    this.$allowedToPushDropdown.disable();
40

41 42 43
    axios.patch(this.$wrap.data('url'), {
      protected_branch: {
        merge_access_levels_attributes: [{
J
Jacob Schatz 已提交
44
          id: this.$allowedToMergeDropdown.data('accessLevelId'),
45 46 47
          access_level: $allowedToMergeInput.val(),
        }],
        push_access_levels_attributes: [{
J
Jacob Schatz 已提交
48
          id: this.$allowedToPushDropdown.data('accessLevelId'),
49 50
          access_level: $allowedToPushInput.val(),
        }],
K
kushalpandya 已提交
51
      },
52 53 54 55
    }).then(() => {
      this.$allowedToMergeDropdown.enable();
      this.$allowedToPushDropdown.enable();
    }).catch(() => {
K
kushalpandya 已提交
56 57
      this.$allowedToMergeDropdown.enable();
      this.$allowedToPushDropdown.enable();
58 59

      flash('Failed to update branch!', 'alert', document.querySelector('.js-protected-branches-list'));
K
kushalpandya 已提交
60 61 62
    });
  }
}