forked from woocommerce/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for unsaved changes before leaving product editor (woocommerce#…
…38430) * Check for unsaved changes before leaving product editor * Add changelog entry * Add hasEdit convenience method to useProductEdits hook
- Loading branch information
Showing
5 changed files
with
104 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: add | ||
|
||
Check for unsaved changes before leaving product editor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/js/product-editor/src/hooks/use-confirm-unsaved-product-changes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { useConfirmUnsavedChanges } from '@woocommerce/navigation'; | ||
import { useEntityProp } from '@wordpress/core-data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { preventLeavingProductForm } from '../utils/prevent-leaving-product-form'; | ||
import { useProductEdits } from './use-product-edits'; | ||
|
||
export function useConfirmUnsavedProductChanges() { | ||
const [ productId ] = useEntityProp< number >( | ||
'postType', | ||
'product', | ||
'id' | ||
); | ||
const { hasEdits } = useProductEdits(); | ||
const { isSaving } = useSelect( | ||
( select ) => { | ||
const { isSavingEntityRecord } = select( 'core' ); | ||
|
||
return { | ||
isSaving: isSavingEntityRecord< boolean >( | ||
'postType', | ||
'product', | ||
productId | ||
), | ||
}; | ||
}, | ||
[ productId ] | ||
); | ||
|
||
useConfirmUnsavedChanges( hasEdits || isSaving, preventLeavingProductForm ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useEntityProp } from '@wordpress/core-data'; | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
type EntityEdits = { | ||
[ key: string ]: unknown; | ||
}; | ||
|
||
// Filter out the "content" and "blocks" properties of the edits since | ||
// we do not use these properties within the product editor and they | ||
// will always create false positives. | ||
function filterProductEdits( edits: EntityEdits ) { | ||
delete edits.content; | ||
delete edits.blocks; | ||
return edits; | ||
} | ||
|
||
export function useProductEdits() { | ||
const [ productId ] = useEntityProp< number >( | ||
'postType', | ||
'product', | ||
'id' | ||
); | ||
|
||
const { edits } = useSelect( | ||
( select ) => { | ||
const { getEntityRecordNonTransientEdits } = select( 'core' ); | ||
|
||
const _edits = getEntityRecordNonTransientEdits( | ||
'postType', | ||
'product', | ||
productId | ||
) as EntityEdits; | ||
|
||
return { | ||
edits: filterProductEdits( _edits ), | ||
}; | ||
}, | ||
[ productId ] | ||
); | ||
|
||
function hasEdit( fieldName: string ) { | ||
return edits.hasOwnProperty( fieldName ); | ||
} | ||
|
||
return { | ||
hasEdit, | ||
hasEdits: Object.keys( edits ).length > 0, | ||
}; | ||
} |