Skip to content

Commit

Permalink
Make work for site entities
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Nov 5, 2021
1 parent fec8455 commit 4ef3551
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
37 changes: 37 additions & 0 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,43 @@ export function __experimentalResetEditedEntityRecord( kind, name, recordId ) {
};
}

/**
* Action triggered to reset 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} itemsToReset List of entity properties to reset.
*/
export const __experimentalResetSpecifiedEntityEdits = (
kind,
name,
recordId,
itemsToReset
) => async ( { select, dispatch } ) => {
if ( ! select.hasEditsForEntityRecord( kind, name, recordId ) ) {
return;
}
const edits = select.getEntityRecordNonTransientEdits(
kind,
name,
recordId
);
const editsToDiscard = {};
for ( const edit in edits ) {
if ( itemsToReset.some( ( item ) => item === edit ) ) {
editsToDiscard[ edit ] = edits[ edit ];
}
}
return await dispatch( {
type: 'RESET_SPECIFIED_ENTITY_RECORD_EDITS',
kind,
name,
recordId,
edits: Object.keys( editsToDiscard ),
} );
};

/**
* Returns an action object used in signalling that Upload permissions have been received.
*
Expand Down
10 changes: 9 additions & 1 deletion packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { keyBy, map, groupBy, flowRight, isEqual, get } from 'lodash';
import { keyBy, map, groupBy, flowRight, isEqual, get, omit } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -272,6 +272,14 @@ function entity( entityConfig ) {
} = state;

return otherEdits;

case 'RESET_SPECIFIED_ENTITY_RECORD_EDITS':
const edits = state[ action.recordId ];

return {
...state,
[ action.recordId ]: omit( edits, action.edits ),
};
}

return state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export default function EntitiesSavedStates( { close } ) {
saveEditedEntityRecord,
__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits,
__experimentalResetEditedEntityRecord: resetEditedEntityRecord,
__experimentalResetSpecifiedEntityEdits: resetSpecifiedEntityEdits,
} = useDispatch( coreStore );

// To group entities by type.
Expand Down Expand Up @@ -199,7 +200,12 @@ export default function EntitiesSavedStates( { close } ) {
resetEditedEntityRecord( kind, name, key );
}
} );
//resetSpecifiedEntityEdits( 'root', 'site', undefined, siteItemsToDiscard );
resetSpecifiedEntityEdits(
'root',
'site',
undefined,
siteItemsToDiscard
);
};

// Explicitly define this with no argument passed. Using `close` on
Expand Down

0 comments on commit 4ef3551

Please sign in to comment.