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

79 lines
2.4 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';
import { __ } from '~/locale';
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: [
{
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(
__('Failed to update branch!'),
'alert',
document.querySelector('.js-protected-branches-list'),
);
});
2017-07-19 08:44:15 -04:00
}
}