Skip to content

Commit

Permalink
MILESTONE: only delete on shared key value being final of its kind. T…
Browse files Browse the repository at this point in the history
…ODO: test
  • Loading branch information
kennethbruskiewicz committed Oct 1, 2024
1 parent c88f698 commit dbf4028
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions lib/utils/1m.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,10 @@ function hasDuplicates(array) {
return uniqueValues.size !== array.length;
}

const cellsInColumnUnique = (dh) => (shared_key_name, new_value = null) => {
const cellsInColumnUnique = (dh) => (shared_key_name, filter_value = null) => {
const columnIndex = dh.getColumnIndexByFieldName(shared_key_name);
const columnValues = getNonEmptyColumnValues(dh.hot, columnIndex);
if (new_value) columnValues.push(new_value);
if (filter_value !== null) columnValues.filter(el => el === filter_value);
return hasDuplicates(columnValues);
}

Expand Down Expand Up @@ -552,16 +552,24 @@ const bindChangeEmitter = (appContext) => (dh) => {
: ACTION.UPDATE;

// TODO: modal continuation interface

const cellIsUnique = cellsInColumnUnique(dh)(key_name);
const willHaveDuplicateValues = cellsInColumnUnique(dh)(key_name, Object.values(row_changes[row].newValues)[0]);

// propagate deletion when it is the final key
const currentValue = row_changes[row].oldValues[column_index];

// propagate deletion when the deleted cell was unique of its kind
// always propagate updates
const propagates = updateActionType === ACTION.DELETE ?
!cellIsUnique
// if deletion is triggering, the current non empty column values should still be one down from the original value
// NOTE: if this is problematic, need to replace *all* handsontable with CRUD
getNonEmptyColumnValues(dh.hot, column_index).filter(el => el === currentValue).length === 0
: true;

// console.warn(
// updateActionType,
// currentValue,
// futureValue,
// getNonEmptyColumnValues(dh.hot, column_index),
// getNonEmptyColumnValues(dh.hot, column_index).filter(el => el === currentValue).length === 0,
// propagates);

const details = {
emitted_by: dh.class_assignment,
target: dh.class_assignment,
Expand All @@ -579,15 +587,17 @@ const bindChangeEmitter = (appContext) => (dh) => {
propagates
};

// warn about deletion if it's the final key
if (updateActionType === ACTION.DELETE && !cellIsUnique && propagates) {
console.warn("PROPAGATION:", "Row collections with key will be deleted", details);
}
if (propagates) {
// warn about deletion if it's the final key
if (updateActionType === ACTION.DELETE) {
console.warn("PROPAGATION:", "Row collections with key will be deleted", details);
}

// // warn if update will cause a duplicate that merges entries in child tables which share that same key
// if (updateActionType === ACTION.UPDATE && willHaveDuplicateValues && propagates) {
// console.warn("PROPAGATION:", "Row collections will merge with existing those of existing key value", details);
// }
// // warn if update will cause a duplicate that merges entries in child tables which share that same key
if (updateActionType === ACTION.UPDATE && getNonEmptyColumnValues(dh.hot, column_index).filter(el => el === currentValue).length + 1 > 1) {
console.warn("PROPAGATION:", "Row collections will merge with existing those of existing key value", details);
}
}

dispatchHandsontableUpdate(updateActionType, details);

Expand Down Expand Up @@ -796,6 +806,7 @@ const handleAction = (appContext) => (dh) => (action, details) => {
}
};
} else {
// propagation
for (let col_index in shared_key_details.key_indexes) {
updateRows(dh, [
col_index,
Expand Down

0 comments on commit dbf4028

Please sign in to comment.