Skip to content

Commit

Permalink
restore deletion propagation behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethbruskiewicz committed Oct 23, 2024
1 parent 464ec41 commit 8bf5c64
Showing 1 changed file with 29 additions and 92 deletions.
121 changes: 29 additions & 92 deletions lib/utils/1m.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const SchemaClassesList = (schema) => {
);
}


const SchemaClasses = (schema) =>
takeKeys(SchemaClassesList(schema))(schema.classes);

Expand Down Expand Up @@ -937,31 +936,16 @@ function parentBroadcastsCRUD(appContext, dhs) {
for (let row in row_changes) {

// dispatch action based on changes

// Dispatch a DELETE action if the value the new value is an empty unit value
// Else dispatch an UPDATE on a change
const updateActionType =
Object.values(
row_changes[row].newValues
).map(isEmptyUnitVal).includes(true) ?
ACTION.DELETE
: ACTION.UPDATE;

// TODO: modal continuation interface
const currentValue = row_changes[row].oldValues[column_index]; // NOTE: can be `undefined` as it is a "zero" value in handsontable

console.warn('row_changes', row_changes,
'column_index', column_index,
row_changes[row].oldValues,
row_changes[row].newValues);

// propagate deletion when the deleted cell was unique of its kind
// always propagate updates
const propagates = updateActionType === ACTION.DELETE ?
// 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;

const detail = {
action: updateActionType,
emitted_by: foreign_key_class,
Expand All @@ -976,8 +960,7 @@ function parentBroadcastsCRUD(appContext, dhs) {
),
old_changes: row_changes[row].oldValues,
new_changes: row_changes[row].newValues,
row_changes,
propagates
row_changes
};

dispatchHandsontableUpdate2(detail);
Expand Down Expand Up @@ -1007,7 +990,6 @@ function childListensCRUD(appContext, dhs) {
const unique_key = node.unique_keys[unique_key_name];
if (unique_key.foreign_key) {
document.addEventListener('handsontableUpdate', function (event) {
console.warn('receiving change:', node, event.detail);
routeAction2(appContext, dhs, event.detail.action, event);
});
}
Expand All @@ -1016,77 +998,24 @@ function childListensCRUD(appContext, dhs) {
}

const routeAction2 = (appContext, dhs, action, event) => {
const { emitted_by, target, key_name, propagates } = event.detail;
const { emitted_by, target, key_name } = event.detail;
const unique_key = appContext[emitted_by]?.unique_keys[key_name];
console.info("ROUTING:", action, event.detail);
if (action === ACTION.READ && emitted_by === target) {
handleAction2(appContext, dhs, dhs[emitted_by], action, event.detail);
event.detail.action = ACTION.SELECT
dispatchHandsontableUpdate2(event.detail);
} else if (action === ACTION.SELECT) {
const activeDataHarmonizer = dhs[findActiveDataHarmonizer(appContext)];
const activeDataHarmonizer = dhs[findActiveDataHarmonizer(appContext)]
event.detail.currentSelection =
makeCurrentSelection(appContext, activeDataHarmonizer);
event.detail.target = event.detail.currentSelection.name;
handleAction2(appContext, dhs, activeDataHarmonizer, action, event.detail);
if ('child_classes' in unique_key) {
unique_key.child_classes.forEach(child_class => {
dispatchHandsontableUpdate2({
...event.detail,
action: ACTION.FILTER,
emitted_by,
key_name,
target: child_class,
});
});
}
// // propagate filter actions to child tables if possible and allowed
// // TODO
// if (propagates) {
// // warn about deletion if it's the final key
// if (action === ACTION.SELECT) {
// console.warn("PROPAGATION:", "Row collections with key will be filtered", event.detail);
// }
// // TODO: how to scope row changes
// event.detail.row = null;
// dispatchChildHooks2(unique_key, ACTION.FILTER, event.detail);
// }
} else if (action === ACTION.FILTER) {
handleAction2(appContext, dhs, dhs[target], action, event.detail);
} else if (action === ACTION.UPDATE || action === ACTION.DELETE) {
handleAction2(appContext, dhs, dhs[target], action, event.detail);
if (emitted_by === target) {
// propagate update actions to child table if possible
// TODO
if (propagates) {

// warn about deletion if it's the final key
if (action === ACTION.DELETE) {
console.warn("PROPAGATION:", "Row collections with key will be deleted", event.detail);
}

// // 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", event.detail);
}

// TODO: how to scope row changes
event.detail.row = null;
dispatchChildHooks2(unique_key, action, event.detail);
}
}
dispatchChildHooks2(unique_key, ACTION.FILTER, event.detail);
} else if (action === ACTION.VALIDATE) {
handleAction2(appContext, dhs, dhs[emitted_by], action, event.detail);
} else {
console.warn(
'not firing hook',
emitted_by,
action,
appContext[emitted_by]?.unique_keys,
unique_key,
unique_key.foreign_key,
'foreign_key_class' in unique_key
);
// FILTER, UPDATE, DELETE
handleAction2(appContext, dhs, dhs[target], action, event.detail);
}
}

Expand All @@ -1102,6 +1031,7 @@ const handleAction2 = (appContext, dhs, dh, action, details) => {
target,
currentSelection
} = details;

const shared_key_details = appContext[emitted_by].unique_keys[key_name];
if (!shared_key_details) {
console.error(`Key details not found for ${key_name} in ${emitted_by}.`);
Expand Down Expand Up @@ -1152,16 +1082,23 @@ const handleAction2 = (appContext, dhs, dh, action, details) => {
}
};
} else {
// propagation
for (let col_index in shared_key_details.key_indexes) {
updateRows(dh, [
col_index,
appContext[emitted_by].unique_keys[key_name].key_old_values[
for (let col_index in shared_key_details.key_indexes) {
// propagation
// propagate deletion when the deleted cell was unique of its kind (always propagate updates)
const deletion_propagates = getNonEmptyColumnValues(dhs[emitted_by].hot, [col_index]).filter(col_val => col_val === appContext[emitted_by].unique_keys[key_name].key_old_values[
col_index
],
appContext[emitted_by].unique_keys[key_name].key_values[col_index],
]);
}
]).length === 0
if (deletion_propagates) {
updateRows(dh, [
col_index,
appContext[emitted_by].unique_keys[key_name].key_old_values[
col_index
],
appContext[emitted_by].unique_keys[key_name].key_values[col_index],
]);
}
}

}
break;
case ACTION.SELECT:
Expand All @@ -1184,8 +1121,9 @@ const dispatchChildHooks2 = (unique_key, action, details) => {
dispatchHandsontableUpdate2({
...details,
action,
// emitted_by,
// key_name,
target: child_class,
propagates: false,
});
}
}
Expand All @@ -1199,13 +1137,12 @@ export const setup1M = ({ appContext }, dhs) => {
bindReadEmitter(appContext, dhs[dh]);
if (DEBUG) bindKeyConstraintValidation(appContext, dhs[dh]);
createRowFocusTracker(dhs[dh], () => dhs[dh].validate());
// bindChangeEmitter(appContext, dhs[dh]); // create, update, delete
// bindActionHandler(appContext, dhs[dh]);

console.log(appContext)
parentBroadcastsCRUD(appContext, dhs);
childListensCRUD(appContext, dhs);

// bindChangeEmitter(appContext, dhs[dh]); // create, update, delete
// bindActionHandler(appContext, dhs[dh]);
}
}
handsontableUpdateRouter(appContext, dhs);
Expand Down

0 comments on commit 8bf5c64

Please sign in to comment.