Commit b7ff3b3b authored by Alfredo Sumaran's avatar Alfredo Sumaran

Add comments explaining methods functionality

parent ea5bcab9
...@@ -113,7 +113,9 @@ ...@@ -113,7 +113,9 @@
} }
renderRow(item, instance) { renderRow(item, instance) {
const isActive = _.findWhere(instance.activeIds, {id: item.id, type: item.type}) ? 'is-active' : ''; // Dectect if the current item is already saved so we can add
// the `is-active` class so the item looks as marked
const isActive = _.findWhere(instance.activeIds, { id: item.id, type: item.type }) ? 'is-active' : '';
if (item.type === 'user') { if (item.type === 'user') {
return this.userRowHtml(item, isActive); return this.userRowHtml(item, isActive);
} else if (item.type === 'role') { } else if (item.type === 'role') {
......
...@@ -56,14 +56,20 @@ ...@@ -56,14 +56,20 @@
let itemToDestroy; let itemToDestroy;
let accessLevelState = this.state[`${dropdownInstance.accessLevel}_attributes`]; let accessLevelState = this.state[`${dropdownInstance.accessLevel}_attributes`];
// If we are unselecting an option // If element is not active it means it has been active
if (!$el.is('.is-active')) { if (!$el.is('.is-active')) {
// We need to know if the selected item was already saved
// if so we need to append the `_destroy` property
// in order to delete it from the database
// Retrieve the full data of the item we just selected
if (item.type === LEVEL_TYPES.USER) { if (item.type === LEVEL_TYPES.USER) {
itemToDestroy = _.findWhere(accessLevelState, { user_id: item.id }); itemToDestroy = _.findWhere(accessLevelState, { user_id: item.id });
} else if (item.type === LEVEL_TYPES.ROLE) { } else if (item.type === LEVEL_TYPES.ROLE) {
itemToDestroy = _.findWhere(accessLevelState, { access_level: item.id }); itemToDestroy = _.findWhere(accessLevelState, { access_level: item.id });
} }
// State updated by reference
itemToDestroy['_destroy'] = 1; itemToDestroy['_destroy'] = 1;
} }
} }
...@@ -135,11 +141,14 @@ ...@@ -135,11 +141,14 @@
let accessLevelData = []; let accessLevelData = [];
let dataFromInputs = this.getAccessLevelDataFromInputs(accessLevelKey); let dataFromInputs = this.getAccessLevelDataFromInputs(accessLevelKey);
// Collect and format items that will be sent to the server
for (let i = 0; i < dataFromInputs.length; i++) { for (let i = 0; i < dataFromInputs.length; i++) {
let inState; let inState;
let adding; let adding;
var userId = parseInt(dataFromInputs[i].user_id); var userId = parseInt(dataFromInputs[i].user_id);
// Inputs give us the *state* of the dropdown on the frontend before it's persisted
// so we need to compare them with the persisted state which can be get or set on this.state
if (userId) { if (userId) {
adding = LEVEL_TYPES.USER; adding = LEVEL_TYPES.USER;
inState = _.findWhere(this.state[`${accessLevel}_attributes`], { user_id: userId }); inState = _.findWhere(this.state[`${accessLevel}_attributes`], { user_id: userId });
...@@ -149,8 +158,10 @@ ...@@ -149,8 +158,10 @@
} }
if (inState) { if (inState) {
// collect item if it's already saved
accessLevelData.push(inState); accessLevelData.push(inState);
} else { } else {
// format item according the level type
if (adding === LEVEL_TYPES.USER) { if (adding === LEVEL_TYPES.USER) {
accessLevelData.push({ accessLevelData.push({
user_id: parseInt(dataFromInputs[i].user_id) user_id: parseInt(dataFromInputs[i].user_id)
...@@ -163,7 +174,9 @@ ...@@ -163,7 +174,9 @@
} }
} }
// Items to be deleted // Since we didn't considered inputs that were removed
// (because they are not present in the DOM anymore)
// We can get them from the state
this.state[`${accessLevel}_attributes`].forEach((item) => { this.state[`${accessLevel}_attributes`].forEach((item) => {
if (item._destroy) { if (item._destroy) {
accessLevelData.push(item); accessLevelData.push(item);
......
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