diff --git a/packages/js/product-editor/changelog/product-editor-prompt-exports b/packages/js/product-editor/changelog/product-editor-prompt-exports new file mode 100644 index 00000000000..9fb937de19b --- /dev/null +++ b/packages/js/product-editor/changelog/product-editor-prompt-exports @@ -0,0 +1,4 @@ +Significance: minor +Type: dev + +Exports handlePrompt from @woocommerce/product-editor for use by extensions expanding the variation quick update menu. Also introduces handleConfirm and exposes it also diff --git a/packages/js/product-editor/src/utils/handle-confirm.ts b/packages/js/product-editor/src/utils/handle-confirm.ts new file mode 100644 index 00000000000..726f5fc2121 --- /dev/null +++ b/packages/js/product-editor/src/utils/handle-confirm.ts @@ -0,0 +1,24 @@ +/** + * External dependencies + */ +import { __ } from '@wordpress/i18n'; + +export type HandleConfirmProps = { + message?: string; + onOk(): void; + onCancel?(): void; +}; + +export async function handleConfirm( { + message = __( 'Are you sure?', 'woocommerce' ), + onOk, + onCancel, +}: HandleConfirmProps ) { + // eslint-disable-next-line no-alert + if ( window.confirm( message ) ) { + onOk?.(); + return; + } + + onCancel?.(); +} diff --git a/packages/js/product-editor/src/utils/handle-prompt.ts b/packages/js/product-editor/src/utils/handle-prompt.ts index c472ae32f0e..055c2197ca4 100644 --- a/packages/js/product-editor/src/utils/handle-prompt.ts +++ b/packages/js/product-editor/src/utils/handle-prompt.ts @@ -20,9 +20,7 @@ export async function handlePrompt( { const value = window.prompt( message, defaultValue ); if ( value === null ) { - if ( onCancel ) { - onCancel(); - } + onCancel?.(); return; } diff --git a/packages/js/product-editor/src/utils/index.ts b/packages/js/product-editor/src/utils/index.ts index 9d198880923..c3067698897 100644 --- a/packages/js/product-editor/src/utils/index.ts +++ b/packages/js/product-editor/src/utils/index.ts @@ -21,6 +21,8 @@ import { import { preventLeavingProductForm } from './prevent-leaving-product-form'; import { hasAttributesUsedForVariations } from './has-attributes-used-for-variations'; import { isValidEmail } from './validate-email'; +import { handlePrompt } from './handle-prompt'; +import { handleConfirm } from './handle-confirm'; export * from './create-ordered-children'; export * from './date'; @@ -45,6 +47,8 @@ export { getProductTitle, getProductVariationTitle, getTruncatedProductVariationTitle, + handleConfirm, + handlePrompt, hasAttributesUsedForVariations, isValidEmail, preventLeavingProductForm,