buildChildRowToSave function
Implementation
Map<String, dynamic> buildChildRowToSave({
required Map<String, dynamic> editorConfig,
required String action,
required String? rowId,
required Map<String, dynamic> submittedItem,
required Map<String, dynamic> initialValues,
}) {
final Map<String, dynamic> rowToSave = Map<String, dynamic>.from(
submittedItem,
)..remove('resultset');
if (gceComBuildChildRowToSaveDebug) {
logDebug(
'CRUD_commons | buildChildRowToSave / submittedItem: $submittedItem',
);
logDebug('CRUD_commons | buildChildRowToSave / rowToSave: $rowToSave');
}
final Map<String, dynamic> cleanInitialValues = Map<String, dynamic>.from(
initialValues,
)..remove('resultset');
if (editorConfig['type'] != 'child_listing') {
return {'rowId': rowId, 'rowToSave': rowToSave};
}
// Parent id field name(s) and value(s), from endpointKeyNames + parentData
final Map<String, dynamic> parentData = Map<String, dynamic>.from(
editorConfig['parentData'] ?? const {},
);
final Map<String, dynamic> parentKeys = {};
for (final keyPair in (editorConfig['endpointKeyNames'] as List)) {
parentKeys[keyPair['parameterName']] = getParentElementValue(
keyPair,
parentData,
);
}
if (editorConfig['subType'] == 'array') {
final String arrayName = editorConfig['array_name'];
if (action == actionDelete) {
Map<String, dynamic> resultDelete = {
'rowId': null,
'rowToSave': {...parentKeys, '${arrayName}_old': cleanInitialValues},
};
if (gceComBuildChildRowToSaveDebug) {
logDebug(
'CRUD_commons | buildChildRowToSave / resultDelete: $resultDelete',
);
}
return resultDelete;
}
Map<String, dynamic> resultSave = {
'rowId': null,
'rowToSave': {
...parentKeys,
arrayName: rowToSave,
'${arrayName}_old': cleanInitialValues,
},
};
if (gceComBuildChildRowToSaveDebug) {
logDebug('CRUD_commons | buildChildRowToSave / resultSave: $resultSave');
}
return resultSave;
}
// subType 'table'
return {
'rowId': action == actionCreate ? null : rowId,
'rowToSave': {...rowToSave, ...parentKeys},
};
}