protected_branch_edit.js 2.4 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
import { __ } from '~/locale';
5

K
kushalpandya 已提交
6 7 8 9 10 11
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);
12

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

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

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

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

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

K
kushalpandya 已提交
43 44
    this.$allowedToMergeDropdown.disable();
    this.$allowedToPushDropdown.disable();
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    axios
      .patch(this.$wrap.data('url'), {
        protected_branch: {
          merge_access_levels_attributes: [
            {
              id: this.$allowedToMergeDropdown.data('accessLevelId'),
              access_level: $allowedToMergeInput.val(),
            },
          ],
          push_access_levels_attributes: [
            {
              id: this.$allowedToPushDropdown.data('accessLevelId'),
              access_level: $allowedToPushInput.val(),
            },
          ],
        },
      })
      .then(() => {
        this.$allowedToMergeDropdown.enable();
        this.$allowedToPushDropdown.enable();
      })
      .catch(() => {
        this.$allowedToMergeDropdown.enable();
        this.$allowedToPushDropdown.enable();

        flash(
72
          __('Failed to update branch!'),
73 74 75 76
          'alert',
          document.querySelector('.js-protected-branches-list'),
        );
      });
K
kushalpandya 已提交
77 78
  }
}