Skip to content

Commit

Permalink
Add an async __unstablePreSavePost hook; resolving with false preve…
Browse files Browse the repository at this point in the history
…nts saving (#58022)

* Add an async `__unstablePreSavePost` hook; resolving with false prevents saving

* Simplify preSave check

* Filter edits & enable throwing Error in `__unstablePreSavePost` filter

* Pass Promise.resolve( edits ) instead of edits

* Move REQUEST_POST_UPDATE_START to before preSave async filter

* Update saveEntityRecord error handling to use try/catch

* Use explicit false check for error

* reduce changes, test functionality, don’t pass data to filter

* whitespace

Unlinked contributors: aduth, lex127, audiovisuel-uqam, eballeste, jenilk, zhitaryksergey, christianMiguez.

Co-authored-by: adamsilverstein <[email protected]>
Co-authored-by: jsnajdr <[email protected]>
Co-authored-by: ockham <[email protected]>
Co-authored-by: sadmansh <[email protected]>
Co-authored-by: sc0ttkclark <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: gziolo <[email protected]>
Co-authored-by: acafourek <[email protected]>
Co-authored-by: marcwieland95 <[email protected]>
Co-authored-by: margolisj <[email protected]>
Co-authored-by: noisysocks <[email protected]>
  • Loading branch information
12 people committed Aug 2, 2024
1 parent 41ee29e commit 3c7d4d6
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,45 @@ export const savePost =
content,
};
dispatch( { type: 'REQUEST_POST_UPDATE_START', options } );
await registry
.dispatch( coreStore )
.saveEntityRecord(
'postType',
previousRecord.type,
edits,

let error = false;
try {
error = await applyFilters(
'editor.__unstablePreSavePost',
Promise.resolve( false ),
options
);
} catch ( err ) {
error = err;
}

let error = registry
.select( coreStore )
.getLastEntitySaveError(
'postType',
previousRecord.type,
previousRecord.id
);
if ( ! error ) {
try {
await registry
.dispatch( coreStore )
.saveEntityRecord(
'postType',
previousRecord.type,
edits,
options
);
} catch ( err ) {
error =
err.message && err.code !== 'unknown_error'
? err.message
: __( 'An error occurred while updating.' );
}
}

if ( ! error ) {
error = registry
.select( coreStore )
.getLastEntitySaveError(
'postType',
previousRecord.type,
previousRecord.id
);
}

if ( ! error ) {
await applyFilters(
Expand Down

0 comments on commit 3c7d4d6

Please sign in to comment.