debian-mirror-gitlab/app/assets/javascripts/protected_branches/protected_branch_edit.js

79 lines
2.4 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import flash from '../flash';
import axios from '../lib/utils/axios_utils';
2017-09-10 17:25:29 +05:30
import ProtectedBranchAccessDropdown from './protected_branch_access_dropdown';
2019-07-31 22:56:46 +05:30
import { __ } from '~/locale';
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
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);
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
this.buildDropdowns();
}
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
buildDropdowns() {
// Allowed to merge dropdown
this.protectedBranchAccessDropdown = new ProtectedBranchAccessDropdown({
$dropdown: this.$allowedToMergeDropdown,
data: gon.merge_access_levels,
onSelect: this.onSelectCallback,
});
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
// Allowed to push dropdown
this.protectedBranchAccessDropdown = new ProtectedBranchAccessDropdown({
$dropdown: this.$allowedToPushDropdown,
data: gon.push_access_levels,
onSelect: this.onSelectCallback,
});
}
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
onSelect() {
2018-12-13 13:39:08 +05:30
const $allowedToMergeInput = this.$wrap.find(
`input[name="${this.$allowedToMergeDropdown.data('fieldName')}"]`,
);
const $allowedToPushInput = this.$wrap.find(
`input[name="${this.$allowedToPushDropdown.data('fieldName')}"]`,
);
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
// Do not update if one dropdown has not selected any option
if (!($allowedToMergeInput.length && $allowedToPushInput.length)) return;
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
this.$allowedToMergeDropdown.disable();
this.$allowedToPushDropdown.disable();
2016-09-13 17:45:13 +05:30
2018-12-13 13:39:08 +05:30
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(
2019-07-31 22:56:46 +05:30
__('Failed to update branch!'),
2018-12-13 13:39:08 +05:30
'alert',
document.querySelector('.js-protected-branches-list'),
);
});
2017-09-10 17:25:29 +05:30
}
}