[Customize Your Store] Retain unsave changes and save all when users click the "done" button (#40541)
* Retain unsave changes and save all the unsaved when users click the "done" button * Add changelog * Update save hub
This commit is contained in:
parent
e8df6a634e
commit
7918e1ee5c
|
@ -4,7 +4,7 @@
|
||||||
/**
|
/**
|
||||||
* External dependencies
|
* External dependencies
|
||||||
*/
|
*/
|
||||||
import { useContext, useEffect, useState } from '@wordpress/element';
|
import { useContext, useState } from '@wordpress/element';
|
||||||
import { useQuery } from '@woocommerce/navigation';
|
import { useQuery } from '@woocommerce/navigation';
|
||||||
import { useSelect, useDispatch } from '@wordpress/data';
|
import { useSelect, useDispatch } from '@wordpress/data';
|
||||||
import {
|
import {
|
||||||
|
@ -39,7 +39,6 @@ const PUBLISH_ON_SAVE_ENTITIES = [
|
||||||
];
|
];
|
||||||
|
|
||||||
export const SaveHub = () => {
|
export const SaveHub = () => {
|
||||||
const saveNoticeId = 'site-edit-save-notice';
|
|
||||||
const urlParams = useQuery();
|
const urlParams = useQuery();
|
||||||
const { sendEvent } = useContext( CustomizeStoreContext );
|
const { sendEvent } = useContext( CustomizeStoreContext );
|
||||||
const [ isResolving, setIsResolving ] = useState< boolean >( false );
|
const [ isResolving, setIsResolving ] = useState< boolean >( false );
|
||||||
|
@ -49,7 +48,7 @@ export const SaveHub = () => {
|
||||||
const { __unstableMarkLastChangeAsPersistent } =
|
const { __unstableMarkLastChangeAsPersistent } =
|
||||||
useDispatch( blockEditorStore );
|
useDispatch( blockEditorStore );
|
||||||
|
|
||||||
const { createErrorNotice, removeNotice } = useDispatch( noticesStore );
|
const { createErrorNotice } = useDispatch( noticesStore );
|
||||||
|
|
||||||
const {
|
const {
|
||||||
dirtyEntityRecords,
|
dirtyEntityRecords,
|
||||||
|
@ -90,71 +89,7 @@ export const SaveHub = () => {
|
||||||
__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits,
|
__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits,
|
||||||
} = useDispatch( coreStore );
|
} = useDispatch( coreStore );
|
||||||
|
|
||||||
useEffect( () => {
|
|
||||||
dirtyEntityRecords.forEach( ( entity ) => {
|
|
||||||
/* This is a hack to reset the entity record when the user navigates away from editing page to main page.
|
|
||||||
This is needed because Gutenberg does not provide a way to reset the entity record. Replace this when we have a better way to do this.
|
|
||||||
We will need to add different conditions here when we implement editing for other entities.
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (
|
|
||||||
entity.kind === 'root' &&
|
|
||||||
entity.name === 'site' &&
|
|
||||||
entity.property
|
|
||||||
) {
|
|
||||||
// Reset site icon edit
|
|
||||||
editEntityRecord(
|
|
||||||
'root',
|
|
||||||
'site',
|
|
||||||
undefined,
|
|
||||||
{
|
|
||||||
[ entity.property ]: undefined,
|
|
||||||
},
|
|
||||||
{ undoIgnore: true }
|
|
||||||
);
|
|
||||||
} else if (
|
|
||||||
entity.kind === 'root' &&
|
|
||||||
entity.name === 'globalStyles'
|
|
||||||
) {
|
|
||||||
editEntityRecord(
|
|
||||||
entity.kind,
|
|
||||||
entity.name,
|
|
||||||
entity.key,
|
|
||||||
{
|
|
||||||
styles: undefined,
|
|
||||||
settings: undefined,
|
|
||||||
},
|
|
||||||
{ undoIgnore: true }
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
editEntityRecord(
|
|
||||||
entity.kind,
|
|
||||||
entity.name,
|
|
||||||
entity.key,
|
|
||||||
{
|
|
||||||
selection: undefined,
|
|
||||||
blocks: undefined,
|
|
||||||
content: undefined,
|
|
||||||
},
|
|
||||||
{ undoIgnore: true }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} );
|
|
||||||
// Only run when path changes.
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, [ urlParams.path ] );
|
|
||||||
|
|
||||||
const save = async () => {
|
const save = async () => {
|
||||||
const source = `${ urlParams.path.replace(
|
|
||||||
'/customize-store/assembler-hub/',
|
|
||||||
''
|
|
||||||
) }`;
|
|
||||||
recordEvent( 'customize_your_store_assembler_hub_save_click', {
|
|
||||||
source,
|
|
||||||
} );
|
|
||||||
removeNotice( saveNoticeId );
|
|
||||||
|
|
||||||
try {
|
|
||||||
for ( const { kind, name, key, property } of dirtyEntityRecords ) {
|
for ( const { kind, name, key, property } of dirtyEntityRecords ) {
|
||||||
if ( kind === 'root' && name === 'site' ) {
|
if ( kind === 'root' && name === 'site' ) {
|
||||||
await saveSpecifiedEntityEdits( 'root', 'site', undefined, [
|
await saveSpecifiedEntityEdits( 'root', 'site', undefined, [
|
||||||
|
@ -177,7 +112,19 @@ export const SaveHub = () => {
|
||||||
__unstableMarkLastChangeAsPersistent();
|
__unstableMarkLastChangeAsPersistent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const onClickSaveButton = async () => {
|
||||||
|
const source = `${ urlParams.path.replace(
|
||||||
|
'/customize-store/assembler-hub/',
|
||||||
|
''
|
||||||
|
) }`;
|
||||||
|
recordEvent( 'customize_your_store_assembler_hub_save_click', {
|
||||||
|
source,
|
||||||
|
} );
|
||||||
|
|
||||||
|
try {
|
||||||
|
await save();
|
||||||
navigator.goToParent();
|
navigator.goToParent();
|
||||||
} catch ( error ) {
|
} catch ( error ) {
|
||||||
createErrorNotice(
|
createErrorNotice(
|
||||||
|
@ -186,19 +133,27 @@ export const SaveHub = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const onDone = async () => {
|
||||||
|
recordEvent( 'customize_your_store_assembler_hub_done_click' );
|
||||||
|
setIsResolving( true );
|
||||||
|
|
||||||
|
try {
|
||||||
|
await save();
|
||||||
|
sendEvent( 'FINISH_CUSTOMIZATION' );
|
||||||
|
} catch ( error ) {
|
||||||
|
createErrorNotice(
|
||||||
|
`${ __( 'Saving failed.', 'woocommerce' ) } ${ error }`
|
||||||
|
);
|
||||||
|
setIsResolving( false );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const renderButton = () => {
|
const renderButton = () => {
|
||||||
if ( urlParams.path === '/customize-store/assembler-hub' ) {
|
if ( urlParams.path === '/customize-store/assembler-hub' ) {
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
variant="primary"
|
variant="primary"
|
||||||
onClick={ () => {
|
onClick={ onDone }
|
||||||
recordEvent(
|
|
||||||
'customize_your_store_assembler_hub_done_click'
|
|
||||||
);
|
|
||||||
|
|
||||||
setIsResolving( true );
|
|
||||||
sendEvent( 'FINISH_CUSTOMIZATION' );
|
|
||||||
} }
|
|
||||||
className="edit-site-save-hub__button"
|
className="edit-site-save-hub__button"
|
||||||
// @ts-ignore No types for this exist yet.
|
// @ts-ignore No types for this exist yet.
|
||||||
__next40pxDefaultSize
|
__next40pxDefaultSize
|
||||||
|
@ -218,7 +173,7 @@ export const SaveHub = () => {
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
variant="primary"
|
variant="primary"
|
||||||
onClick={ save }
|
onClick={ onClickSaveButton }
|
||||||
isBusy={ isSaving }
|
isBusy={ isSaving }
|
||||||
disabled={ isDisabled }
|
disabled={ isDisabled }
|
||||||
aria-disabled={ isDisabled }
|
aria-disabled={ isDisabled }
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: patch
|
||||||
|
Type: update
|
||||||
|
|
||||||
|
CYS: Retain unsave changes and save all the unsaved when users click the "done" button
|
Loading…
Reference in New Issue