Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show notice on save in site editor #36897

Merged
merged 5 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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