2018-02-02 05:51:27 -05:00
|
|
|
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';
|
2019-05-07 13:44:46 -04:00
|
|
|
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();
|
|
|
|
}
|
2016-08-05 02:57:53 -04:00
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
2016-08-05 02:57:53 -04:00
|
|
|
|
2017-07-19 08:44:15 -04:00
|
|
|
onSelect() {
|
2018-10-10 02:48:36 -04:00
|
|
|
const $allowedToMergeInput = this.$wrap.find(
|
|
|
|
`input[name="${this.$allowedToMergeDropdown.data('fieldName')}"]`,
|
|
|
|
);
|
|
|
|
const $allowedToPushInput = this.$wrap.find(
|
|
|
|
`input[name="${this.$allowedToPushDropdown.data('fieldName')}"]`,
|
|
|
|
);
|
2016-08-05 02:57:53 -04:00
|
|
|
|
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;
|
2016-08-21 01:19:22 -04:00
|
|
|
|
2017-07-19 08:44:15 -04:00
|
|
|
this.$allowedToMergeDropdown.disable();
|
|
|
|
this.$allowedToPushDropdown.disable();
|
2017-01-03 11:31:43 -05:00
|
|
|
|
2018-10-10 02:48:36 -04:00
|
|
|
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-05-07 13:44:46 -04:00
|
|
|
__('Failed to update branch!'),
|
2018-10-10 02:48:36 -04:00
|
|
|
'alert',
|
|
|
|
document.querySelector('.js-protected-branches-list'),
|
|
|
|
);
|
|
|
|
});
|
2017-07-19 08:44:15 -04:00
|
|
|
}
|
|
|
|
}
|