Skip to content

Commit

Permalink
Show notice on save in site editor
Browse files Browse the repository at this point in the history
  • Loading branch information
tellthemachines committed Nov 29, 2021
1 parent f839092 commit 1c2b3aa
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions packages/editor/src/components/entities-saved-states/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useState, useCallback, useRef } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
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 @@ -75,6 +76,10 @@ export default function EntitiesSavedStates( { close } ) {
__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits,
} = useDispatch( coreStore );

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

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

Expand Down Expand Up @@ -134,6 +139,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 @@ -148,17 +154,31 @@ 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
)
);
}

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

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

0 comments on commit 1c2b3aa

Please sign in to comment.