Commit 312d3370 authored by Alfredo Sumaran's avatar Alfredo Sumaran

Return correct input name for selected option

[ci skip]
parent 9a23da2d
...@@ -5,12 +5,24 @@ ...@@ -5,12 +5,24 @@
class allowedToMergeDropdown extends gl.ProtectedBranchAccessDropdown { class allowedToMergeDropdown extends gl.ProtectedBranchAccessDropdown {
fieldName(selectedItem) { fieldName(selectedItem) {
// Role by default let fieldName = '';
let fieldName = `protected_branch[merge_access_levels_attributes][${this.inputCount}][access_level]`; let typeToName = {
role: 'access_level',
user: 'user_id',
};
let $input = this.$wrap.find(`input[name$="[${typeToName[selectedItem.type]}]"][value="${selectedItem.id}"]`);
if ($input.length) {
// If input exists return actual name
fieldName = $input.attr('name');
} else {
// If not suggest a name
fieldName = `protected_branch[merge_access_levels_attributes][${this.inputCount}][access_level]`; // Role by default
if (selectedItem.type === 'user') { if (selectedItem.type === 'user') {
fieldName = `protected_branch[merge_access_levels_attributes][${this.inputCount}][user_id]`; fieldName = `protected_branch[merge_access_levels_attributes][${this.inputCount}][user_id]`;
} }
}
return fieldName; return fieldName;
} }
...@@ -18,10 +30,7 @@ ...@@ -18,10 +30,7 @@
getActiveIds() { getActiveIds() {
let selected = []; let selected = [];
// Todo: Find a better way to get the wrap element of each dropdown this.$wrap.find('input[name^="protected_branch[merge_access_levels_attributes]"]')
let $wrap = this.$dropdown.parents().eq(1); // Please, don't judge me
$wrap.find('input[name^="protected_branch[merge_access_levels_attributes]"]')
.map((i, el) => { .map((i, el) => {
const $el = $(el); const $el = $(el);
selected.push({ selected.push({
......
...@@ -5,12 +5,24 @@ ...@@ -5,12 +5,24 @@
class allowedToPushDropdown extends gl.ProtectedBranchAccessDropdown { class allowedToPushDropdown extends gl.ProtectedBranchAccessDropdown {
fieldName(selectedItem) { fieldName(selectedItem) {
// Role by default let fieldName = '';
let fieldName = `protected_branch[push_access_levels_attributes][${this.inputCount}][access_level]`; let typeToName = {
role: 'access_level',
user: 'user_id',
};
let $input = this.$wrap.find(`input[name$="[${typeToName[selectedItem.type]}]"][value="${selectedItem.id}"]`);
if ($input.length) {
// If input exists return actual name
fieldName = $input.attr('name');
} else {
// If not suggest a name
fieldName = `protected_branch[push_access_levels_attributes][${this.inputCount}][access_level]`; // Role by default
if (selectedItem.type === 'user') { if (selectedItem.type === 'user') {
fieldName = `protected_branch[push_access_levels_attributes][${this.inputCount}][user_id]`; fieldName = `protected_branch[push_access_levels_attributes][${this.inputCount}][user_id]`;
} }
}
return fieldName; return fieldName;
} }
...@@ -18,10 +30,7 @@ ...@@ -18,10 +30,7 @@
getActiveIds() { getActiveIds() {
let selected = []; let selected = [];
// Todo: Find a better way to get the wrap element of each dropdown this.$wrap.find('input[name^="protected_branch[push_access_levels_attributes]"]')
let $wrap = this.$dropdown.parents().eq(1); // Please, don't judge me
$wrap.find('input[name^="protected_branch[push_access_levels_attributes]"]')
.map((i, el) => { .map((i, el) => {
const $el = $(el); const $el = $(el);
selected.push({ selected.push({
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
const self = this; const self = this;
this.$dropdown = $dropdown; this.$dropdown = $dropdown;
this.$wrap = this.$dropdown.parents().eq(1); // TODO: Find a better way to get the wrap element
this.usersPath = '/autocomplete/users.json'; this.usersPath = '/autocomplete/users.json';
this.inputCount = 0; this.inputCount = 0;
this.defaultLabel = this.$dropdown.data('defaultLabel'); this.defaultLabel = this.$dropdown.data('defaultLabel');
......
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