Skip to content

Commit

Permalink
add __experimentalUpdateSpecifiedEntityEdits
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenedetto committed Apr 2, 2024
1 parent ebe7b20 commit d203b52
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,59 @@ export const saveEditedEntityRecord =
return await dispatch.saveEntityRecord( kind, name, record, options );
};

/**
* Action triggered to update only specified properties for the entity.
*
* @param {string} kind Kind of the entity.
* @param {string} name Name of the entity.
* @param {Object} recordId ID of the record.
* @param {Array} itemsToSave List of entity properties or property paths to save.
* @param {Object} options Saving options.
*/
export const __experimentalUpdateSpecifiedEntityEdits =
( kind, name, recordId, itemsToSave, options ) =>
async ( { select, dispatch } ) => {
if ( ! select.hasEditsForEntityRecord( kind, name, recordId ) ) {
return;
}
const edits = select.getEntityRecordNonTransientEdits(
kind,
name,
recordId
);

// Get the base record from the datase to apply the edits to.
const base = select.getEntityRecord( kind, name, recordId ) || {};

// Use the base record as the starting point for the edits.
const editsToSave = base;

for ( const item of itemsToSave ) {
setNestedValue( editsToSave, item, getNestedValue( edits, item ) );
}

const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
const entityConfig = configs.find(
( config ) => config.kind === kind && config.name === name
);

const entityIdKey = entityConfig?.key || DEFAULT_ENTITY_KEY;

// If a record key is provided then update the existing record.
// This necessitates providing `recordKey` to saveEntityRecord as part of the
// `record` argument (here called `editsToSave`) to stop that action creating
// a new record and instead cause it to update the existing record.
if ( recordId ) {
editsToSave[ entityIdKey ] = recordId;
}
return await dispatch.saveEntityRecord(
kind,
name,
editsToSave,
options
);
};

/**
* Action triggered to save only specified properties for the entity.
*
Expand Down

0 comments on commit d203b52

Please sign in to comment.