Commit 8c3b7ee6 authored by Alfredo Sumaran's avatar Alfredo Sumaran

Be able to update protected branches

[ci skip]
parent 75ac9804
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
gl.ProtectedBranchAccessDropdown = class { gl.ProtectedBranchAccessDropdown = class {
constructor(options) { constructor(options) {
const { $dropdown, data, onSelect } = options; const { $dropdown, onSelect, onHide } = options;
const self = this; const self = this;
this.$dropdown = $dropdown; this.$dropdown = $dropdown;
...@@ -25,6 +25,10 @@ ...@@ -25,6 +25,10 @@
hidden() { hidden() {
// Here because last selected item is not considered after first close // Here because last selected item is not considered after first close
this.activeIds = self.getActiveIds(); this.activeIds = self.getActiveIds();
if (onHide) {
onHide();
}
}, },
setActiveIds() { setActiveIds() {
// Needed for pre select options // Needed for pre select options
...@@ -33,8 +37,11 @@ ...@@ -33,8 +37,11 @@
clicked(item, $el, e) { clicked(item, $el, e) {
e.preventDefault(); e.preventDefault();
self.inputCount++; self.inputCount++;
if (onSelect) {
onSelect(); onSelect();
} }
}
}); });
} }
......
...@@ -3,34 +3,85 @@ ...@@ -3,34 +3,85 @@
gl.ProtectedBranchEdit = class { gl.ProtectedBranchEdit = class {
constructor(options) { constructor(options) {
this.hasChanges = false;
this.$wrap = options.$wrap; this.$wrap = options.$wrap;
this.$allowedToMergeDropdown = this.$wrap.find('.js-allowed-to-merge'); this.$allowedToMergeDropdown = this.$wrap.find('.js-allowed-to-merge');
this.$allowedToMergeDropdownWrap = this.$allowedToMergeDropdown.parents().eq(1);
this.$allowedToPushDropdown = this.$wrap.find('.js-allowed-to-push'); this.$allowedToPushDropdown = this.$wrap.find('.js-allowed-to-push');
this.$allowedToPushDropdownWrap = this.$allowedToPushDropdown.parents().eq(1);
this.buildDropdowns(); this.buildDropdowns();
} }
buildDropdowns() { buildDropdowns() {
// Allowed to merge dropdown // Allowed to merge dropdown
new gl.allowedToMergeDropdown({ new gl.allowedToMergeDropdown({
$dropdown: this.$allowedToMergeDropdown, $dropdown: this.$allowedToMergeDropdown,
data: gon.merge_access_levels, onSelect: this.onSelect.bind(this),
onSelect: this.onSelect.bind(this) onHide: this.onHide.bind(this),
}); });
// Allowed to push dropdown // Allowed to push dropdown
new gl.allowedToPushDropdown({ new gl.allowedToPushDropdown({
$dropdown: this.$allowedToPushDropdown, $dropdown: this.$allowedToPushDropdown,
data: gon.push_access_levels, onSelect: this.onSelect.bind(this),
onSelect: this.onSelect.bind(this) onHide: this.onHide.bind(this)
}); });
} }
onSelect() { onSelect() {
this.hasChanges = true;
}
onHide() {
if (!this.hasChanges) {
return;
}
this.hasChanges = true;
const $allowedToMergeInput = this.$wrap.find(`input[name="${this.$allowedToMergeDropdown.data('fieldName')}"]`); const $allowedToMergeInput = this.$wrap.find(`input[name="${this.$allowedToMergeDropdown.data('fieldName')}"]`);
const $allowedToPushInput = this.$wrap.find(`input[name="${this.$allowedToPushDropdown.data('fieldName')}"]`); const $allowedToPushInput = this.$wrap.find(`input[name="${this.$allowedToPushDropdown.data('fieldName')}"]`);
let $mergeInputs = this.$allowedToMergeDropdownWrap.find('input[name^="protected_branch[merge_access_levels_attributes]"]')
let $pushInputs = this.$allowedToPushDropdownWrap.find('input[name^="protected_branch[push_access_levels_attributes]"]')
let merge_access_levels_attributes = [];
let push_access_levels_attributes = [];
$mergeInputs.map((i, el) => {
const $el = $(el);
const type = $el.data('type');
const value = $el.val();
if (type === 'role') {
merge_access_levels_attributes.push({
access_level: value
});
} else if (type === 'user') {
merge_access_levels_attributes.push({
user_id: value
});
}
});
$pushInputs.map((i, el) => {
const $el = $(el);
const type = $el.data('type');
const value = $el.val();
if (type === 'role') {
push_access_levels_attributes.push({
access_level: value
});
} else if (type === 'user') {
push_access_levels_attributes.push({
user_id: value
});
}
});
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: this.$wrap.data('url'), url: this.$wrap.data('url'),
...@@ -39,18 +90,13 @@ ...@@ -39,18 +90,13 @@
_method: 'PATCH', _method: 'PATCH',
id: this.$wrap.data('banchId'), id: this.$wrap.data('banchId'),
protected_branch: { protected_branch: {
merge_access_levels_attributes: [{ merge_access_levels_attributes,
id: this.$allowedToMergeDropdown.data('access-level-id'), push_access_levels_attributes
access_level: $allowedToMergeInput.val()
}],
push_access_levels_attributes: [{
id: this.$allowedToPushDropdown.data('access-level-id'),
access_level: $allowedToPushInput.val()
}]
} }
}, },
success: () => { success: () => {
this.$wrap.effect('highlight'); this.$wrap.effect('highlight');
this.hasChanges = false;
}, },
error() { error() {
$.scrollTo(0); $.scrollTo(0);
......
...@@ -10,4 +10,4 @@ ...@@ -10,4 +10,4 @@
value: value, data: { type: level.type } } value: value, data: { type: level.type } }
= dropdown_tag('Select', options: { toggle_class: "#{toggle_class} js-multiselect", dropdown_class: 'dropdown-menu-user dropdown-menu-selectable', filter: true, = dropdown_tag('Select', options: { toggle_class: "#{toggle_class} js-multiselect", dropdown_class: 'dropdown-menu-user dropdown-menu-selectable', filter: true,
data: { input_id: input_basic_name, default_label: 'Select' } }) data: { default_label: 'Select' } })
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment