Commit 1494e81f authored by Alfredo Sumaran's avatar Alfredo Sumaran

Code improvement and avoid code duplication

[ci skip]
parent fed0a5da
(global => { (global => {
global.gl = global.gl || {}; global.gl = global.gl || {};
const LEVEL_TYPES = {
USER: 'user',
ROLE: 'role',
};
const ACCESS_LEVELS = {
MERGE: 'merge_access_levels',
PUSH: 'push_access_levels',
};
gl.ProtectedBranchEdit = class { gl.ProtectedBranchEdit = class {
constructor(options) { constructor(options) {
this.$wraps = {};
this.hasChanges = false; 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.$wraps[ACCESS_LEVELS.MERGE] = this.$allowedToMergeDropdown.parents().eq(1);
this.$wraps[ACCESS_LEVELS.PUSH] = this.$allowedToPushDropdown.parents().eq(1);
this.buildDropdowns(); this.buildDropdowns();
// Save initial state // Save initial state with existing dropdowns
this.state = { this.state = {};
merge: this.getMergeAccessLevelsAttributes(), for (let ACCESS_LEVEL in ACCESS_LEVELS) {
push: this.getPushAccessLevelsAttributes() this.state[`${ACCESS_LEVELS[ACCESS_LEVEL]}_attributes`] = this.getAccessLevelData(ACCESS_LEVEL);
}; }
} }
buildDropdowns() { buildDropdowns() {
...@@ -48,9 +60,11 @@ ...@@ -48,9 +60,11 @@
} }
updatePermissions() { updatePermissions() {
let formData = {};
let merge = this.consolidateMergeData(); for (let ACCESS_LEVEL in ACCESS_LEVELS) {
let push = this.getPushAccessLevelsAttributes(); formData[`${ACCESS_LEVELS[ACCESS_LEVEL]}_attributes`] = this.consolidateAccessLevelData(ACCESS_LEVEL);
}
return $.ajax({ return $.ajax({
type: 'POST', type: 'POST',
...@@ -59,29 +73,37 @@ ...@@ -59,29 +73,37 @@
data: { data: {
_method: 'PATCH', _method: 'PATCH',
id: this.$wrap.data('banchId'), id: this.$wrap.data('banchId'),
protected_branch: { protected_branch: formData
merge_access_levels_attributes: merge,
push_access_levels_attributes: push
}
}, },
success: (response) => { success: (response) => {
this.$wrap.effect('highlight'); this.$wrap.effect('highlight');
this.hasChanges = false; this.hasChanges = false;
// Update State // Update State
this.state.merge = response.merge_access_levels.map((access) => { for (let ACCESS_LEVEL in ACCESS_LEVELS) {
if (access.user_id) { let accessLevel = ACCESS_LEVELS[ACCESS_LEVEL];
return {
id: access.id, this.state[`${accessLevel}_attributes`] = [];
user_id: access.user_id,
}; for (let i = 0; i < response[accessLevel].length; i++) {
} else { let access = response[accessLevel][i];
return { let accessData = {};
id: access.id,
access_level: access.access_level, if (access.user_id) {
}; accessData = {
id: access.id,
user_id: access.user_id,
};
} else {
accessData ={
id: access.id,
access_level: access.access_level,
};
}
this.state[`${accessLevel}_attributes`].push(accessData);
} }
}); }
}, },
error() { error() {
$.scrollTo(0); $.scrollTo(0);
...@@ -90,49 +112,48 @@ ...@@ -90,49 +112,48 @@
}); });
} }
consolidateMergeData() { consolidateAccessLevelData(accessLevelKey) {
// State takes precedence // State takes precedence
let mergeData = []; let accessLevel = ACCESS_LEVELS[accessLevelKey];
let mergeInputsData = this.getMergeAccessLevelsAttributes() let accessLevelData = [];
let dataFromInputs = this.getAccessLevelData(accessLevelKey);
for (var i = 0; i < mergeInputsData.length; i++) { for (let i = 0; i < dataFromInputs.length; i++) {
let inState; let inState;
let adding; let adding;
var userId = parseInt(mergeInputsData[i].user_id); var userId = parseInt(dataFromInputs[i].user_id);
if (userId) { if (userId) {
adding = 'user'; adding = 'user';
inState = _.findWhere(this.state.merge, {user_id: userId}); inState = _.findWhere(this.state[`${accessLevel}_attributes`], { user_id: userId });
} else { } else {
adding = 'role'; adding = 'role';
inState = _.findWhere(this.state.merge, {access_level: parseInt(mergeInputsData[i].access_level)}); inState = _.findWhere(this.state[`${accessLevel}_attributes`], { access_level: parseInt(dataFromInputs[i].access_level) });
} }
if (inState) { if (inState) {
mergeData.push(inState); accessLevelData.push(inState);
} else { } else {
if (adding === 'user') { if (adding === 'user') {
mergeData.push({ accessLevelData.push({
user_id: parseInt(mergeInputsData[i].user_id) user_id: parseInt(dataFromInputs[i].user_id)
}); });
} else if (adding === 'role') { } else if (adding === 'role') {
mergeData.push({ accessLevelData.push({
access_level: parseInt(mergeInputsData[i].access_level) access_level: parseInt(dataFromInputs[i].access_level)
}); });
} }
} }
} }
return mergeData; return accessLevelData;
} }
getMergeAccessLevelsAttributes() { getAccessLevelData(accessLevelKey) {
let accessLevels = []; let accessLevels = [];
let accessLevel = ACCESS_LEVELS[accessLevelKey];
this.$allowedToMergeDropdownWrap this.$wraps[accessLevel]
.find('input[name^="protected_branch[merge_access_levels_attributes]"]') .find(`input[name^="protected_branch[${accessLevel}_attributes]"]`)
.map((i, el) => { .map((i, el) => {
const $el = $(el); const $el = $(el);
const type = $el.data('type'); const type = $el.data('type');
...@@ -153,30 +174,6 @@ ...@@ -153,30 +174,6 @@
return accessLevels; return accessLevels;
} }
getPushAccessLevelsAttributes() {
let accessLevels = [];
this.$allowedToPushDropdownWrap
.find('input[name^="protected_branch[push_access_levels_attributes]"]')
.map((i, el) => {
const $el = $(el);
const type = $el.data('type');
const value = $el.val();
if (type === 'role') {
accessLevels.push({
access_level: value
});
} else if (type === 'user') {
accessLevels.push({
user_id: value
});
}
});
return accessLevels;
}
} }
})(window); })(window);
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