Skip to content

Commit

Permalink
Discard changes i1
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Nov 4, 2021
1 parent 72a084f commit b426515
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
16 changes: 16 additions & 0 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,22 @@ export const __experimentalSaveSpecifiedEntityEdits = (
return await dispatch.saveEntityRecord( kind, name, editsToSave, options );
};

/**
* Action triggered to reset an entity record's edits.
*
* @param {string} kind Kind of the entity.
* @param {string} name Name of the entity.
* @param {Object} recordId ID of the record.
*/
export function __experimentalResetEditedEntityRecord( kind, name, recordId ) {
return {
type: 'RESET_ENTITY_RECORD_EDITS',
kind,
name,
recordId,
};
}

/**
* Returns an action object used in signalling that Upload permissions have been received.
*
Expand Down
9 changes: 9 additions & 0 deletions packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ function entity( entityConfig ) {
...state,
[ action.recordId ]: nextEdits,
};

case 'RESET_ENTITY_RECORD_EDITS':
const {
// eslint-disable-next-line no-unused-vars
[ action.recordId ]: recordEdits,
...otherEdits
} = state;

return otherEdits;
}

return state;
Expand Down
40 changes: 38 additions & 2 deletions packages/editor/src/components/entities-saved-states/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default function EntitiesSavedStates( { close } ) {
editEntityRecord,
saveEditedEntityRecord,
__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits,
__experimentalResetEditedEntityRecord: resetEditedEntityRecord,
} = useDispatch( coreStore );

// To group entities by type.
Expand Down Expand Up @@ -137,8 +138,8 @@ export default function EntitiesSavedStates( { close } ) {
}
);

// We're saving all changes and can thus safely return to the editor afterwards.
if ( entitiesToSave.length === dirtyEntityRecords.length ) {
// We're saving all changes and can thus safely return to the editor afterwards.
showDiscardEntitiesPanel( false );
close( entitiesToSave );
} else {
Expand Down Expand Up @@ -166,6 +167,41 @@ export default function EntitiesSavedStates( { close } ) {
saveSpecifiedEntityEdits( 'root', 'site', undefined, siteItemsToSave );
};

const discardCheckedEntities = () => {
const entitiesToDiscard = dirtyEntityRecords.filter(
( { kind, name, key, property } ) => {
return ! some(
unselectedEntities,
( elt ) =>
elt.kind === kind &&
elt.name === name &&
elt.key === key &&
elt.property === property
);
}
);

const siteItemsToDiscard = [];
entitiesToDiscard.forEach( ( { kind, name, key, property } ) => {
if ( 'root' === kind && 'site' === name ) {
siteItemsToDiscard.push( property );
} else {
// if (
// PUBLISH_ON_SAVE_ENTITIES.some(
// ( typeToPublish ) =>
// typeToPublish.kind === kind &&
// typeToPublish.name === name
// )
// ) {
// editEntityRecord( kind, name, key, { status: 'publish' } );
// }

resetEditedEntityRecord( kind, name, key );
}
} );
//resetSpecifiedEntityEdits( 'root', 'site', undefined, siteItemsToDiscard );
};

// Explicitly define this with no argument passed. Using `close` on
// its own will use the event object in place of the expected saved entities.
const dismissPanel = useCallback( () => close(), [ close ] );
Expand Down Expand Up @@ -247,7 +283,7 @@ export default function EntitiesSavedStates( { close } ) {
0
}
isDestructive
onClick={ dismissPanel }
onClick={ discardCheckedEntities }
>
{ __( 'Discard changes' ) }
</Button>
Expand Down

0 comments on commit b426515

Please sign in to comment.