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';
|
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() {
|
|
|
|
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-02-02 05:51:27 -05:00
|
|
|
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'),
|
2018-02-02 05:51:27 -05:00
|
|
|
access_level: $allowedToMergeInput.val(),
|
|
|
|
}],
|
|
|
|
push_access_levels_attributes: [{
|
2018-02-20 17:20:48 -05:00
|
|
|
id: this.$allowedToPushDropdown.data('accessLevelId'),
|
2018-02-02 05:51:27 -05:00
|
|
|
access_level: $allowedToPushInput.val(),
|
|
|
|
}],
|
2017-07-19 08:44:15 -04:00
|
|
|
},
|
2018-02-02 05:51:27 -05:00
|
|
|
}).then(() => {
|
|
|
|
this.$allowedToMergeDropdown.enable();
|
|
|
|
this.$allowedToPushDropdown.enable();
|
|
|
|
}).catch(() => {
|
2017-07-19 08:44:15 -04:00
|
|
|
this.$allowedToMergeDropdown.enable();
|
|
|
|
this.$allowedToPushDropdown.enable();
|
2018-02-02 05:51:27 -05:00
|
|
|
|
|
|
|
flash('Failed to update branch!', 'alert', document.querySelector('.js-protected-branches-list'));
|
2017-07-19 08:44:15 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|