gitlab-org--gitlab-foss/app/assets/javascripts/protected_branches/protected_branch_edit.js

63 lines
2.2 KiB
JavaScript
Raw Normal View History

import flash from '../flash';
import axios from '../lib/utils/axios_utils';
2017-07-19 08:44:15 -04:00
import ProtectedBranchAccessDropdown from './protected_branch_access_dropdown';
2016-08-03 14:02:42 -04:00
2017-07-19 08:44:15 -04:00
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-08-03 14:02:42 -04:00
2017-07-19 08:44:15 -04:00
this.buildDropdowns();
}
2017-07-19 08:44:15 -04:00
buildDropdowns() {
// Allowed to merge dropdown
this.protectedBranchAccessDropdown = new ProtectedBranchAccessDropdown({
$dropdown: this.$allowedToMergeDropdown,
data: gon.merge_access_levels,
onSelect: this.onSelectCallback,
});
2016-08-03 14:02:42 -04:00
2017-07-19 08:44:15 -04:00
// Allowed to push dropdown
this.protectedBranchAccessDropdown = new ProtectedBranchAccessDropdown({
$dropdown: this.$allowedToPushDropdown,
data: gon.push_access_levels,
onSelect: this.onSelectCallback,
});
}
2017-07-19 08:44:15 -04:00
onSelect() {
const $allowedToMergeInput = this.$wrap.find(`input[name="${this.$allowedToMergeDropdown.data('fieldName')}"]`);
const $allowedToPushInput = this.$wrap.find(`input[name="${this.$allowedToPushDropdown.data('fieldName')}"]`);
2017-07-19 08:44:15 -04:00
// Do not update if one dropdown has not selected any option
if (!($allowedToMergeInput.length && $allowedToPushInput.length)) return;
2017-07-19 08:44:15 -04:00
this.$allowedToMergeDropdown.disable();
this.$allowedToPushDropdown.disable();
axios.patch(this.$wrap.data('url'), {
protected_branch: {
merge_access_levels_attributes: [{
2018-02-20 17:20:48 -05:00
id: this.$allowedToMergeDropdown.data('accessLevelId'),
access_level: $allowedToMergeInput.val(),
}],
push_access_levels_attributes: [{
2018-02-20 17:20:48 -05:00
id: this.$allowedToPushDropdown.data('accessLevelId'),
access_level: $allowedToPushInput.val(),
}],
2017-07-19 08:44:15 -04:00
},
}).then(() => {
this.$allowedToMergeDropdown.enable();
this.$allowedToPushDropdown.enable();
}).catch(() => {
2017-07-19 08:44:15 -04:00
this.$allowedToMergeDropdown.enable();
this.$allowedToPushDropdown.enable();
flash('Failed to update branch!', 'alert', document.querySelector('.js-protected-branches-list'));
2017-07-19 08:44:15 -04:00
});
}
}