Skip to content

Commit

Permalink
Show notice on save in site editor (#36897)
Browse files Browse the repository at this point in the history
* Show notice on save in site editor
Co-authored-by: Kai Hao <[email protected]>
  • Loading branch information
tellthemachines authored Dec 14, 2021
1 parent 3d2c44d commit caa75e5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
18 changes: 15 additions & 3 deletions packages/e2e-tests/specs/site-editor/multi-entity-saving.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe( 'Multi-entity save flow', () => {
const activatedTemplatePartSelector = `${ templatePartSelector }.block-editor-block-list__layout`;
const savePanelSelector = '.entities-saved-states__panel';
const closePanelButtonSelector =
'.editor-post-publish-panel__header-cancel-button button';
'.editor-post-publish-panel__header-cancel-button button:not(:disabled)';
const createNewButtonSelector =
'//button[contains(text(), "New template part")]';

Expand Down Expand Up @@ -158,7 +158,10 @@ describe( 'Multi-entity save flow', () => {
await assertExistance( savePanelSelector, false );

// Close publish panel.
await page.click( closePanelButtonSelector );
const closePanelButton = await page.waitForSelector(
closePanelButtonSelector
);
await closePanelButton.click();

// Verify saving is disabled.
const draftSaved = await page.waitForSelector( draftSavedSelector );
Expand All @@ -167,13 +170,22 @@ describe( 'Multi-entity save flow', () => {
await assertExistance( saveA11ySelector, false );

await publishPost();
// Wait for the success notice specifically for the published post.
// `publishPost()` has a similar check but it only checks for the
// existence of any snackbars. In this case, there's another "Site updated"
// notice which will be sufficient for that and thus creating a false-positive.
await page.waitForXPath(
'//*[@id="a11y-speak-polite"][contains(text(), "Post published")]'
);

// Update the post.
await page.click( '.editor-post-title' );
await page.keyboard.type( '...more title!' );

// Verify update button is enabled.
const enabledSaveButton = await page.$( enabledSavePostSelector );
const enabledSaveButton = await page.waitForSelector(
enabledSavePostSelector
);
expect( enabledSaveButton ).not.toBeNull();
// Verify multi-entity saving not enabled.
await assertMultiSaveDisabled();
Expand Down
38 changes: 32 additions & 6 deletions packages/editor/src/components/entities-saved-states/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { store as coreStore } from '@wordpress/core-data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { __experimentalUseDialog as useDialog } from '@wordpress/compose';
import { close as closeIcon } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand Down Expand Up @@ -80,6 +81,10 @@ export default function EntitiesSavedStates( { close } ) {
blockEditorStore
);

const { createSuccessNotice, createErrorNotice } = useDispatch(
noticesStore
);

// To group entities by type.
const partitionedSavables = groupBy( dirtyEntityRecords, 'name' );

Expand Down Expand Up @@ -139,6 +144,7 @@ export default function EntitiesSavedStates( { close } ) {
close( entitiesToSave );

const siteItemsToSave = [];
const pendingSavedRecords = [];
entitiesToSave.forEach( ( { kind, name, key, property } ) => {
if ( 'root' === kind && 'site' === name ) {
siteItemsToSave.push( property );
Expand All @@ -153,19 +159,39 @@ export default function EntitiesSavedStates( { close } ) {
editEntityRecord( kind, name, key, { status: 'publish' } );
}

saveEditedEntityRecord( kind, name, key );
pendingSavedRecords.push(
saveEditedEntityRecord( kind, name, key )
);
}
} );
if ( siteItemsToSave.length ) {
saveSpecifiedEntityEdits(
'root',
'site',
undefined,
siteItemsToSave
pendingSavedRecords.push(
saveSpecifiedEntityEdits(
'root',
'site',
undefined,
siteItemsToSave
)
);
}

__unstableMarkLastChangeAsPersistent();

Promise.all( pendingSavedRecords )
.then( ( values ) => {
if (
values.some( ( value ) => typeof value === 'undefined' )
) {
createErrorNotice( __( 'Saving failed.' ) );
} else {
createSuccessNotice( __( 'Site updated.' ), {
type: 'snackbar',
} );
}
} )
.catch( ( error ) =>
createErrorNotice( `${ __( 'Saving failed.' ) } ${ error }` )
);
};

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

0 comments on commit caa75e5

Please sign in to comment.