From 195bcd23681143a3f3f9ba146cb42ba26761ffb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maikel=20David=20P=C3=A9rez=20G=C3=B3mez?= Date: Mon, 22 May 2023 05:59:58 -0400 Subject: [PATCH] Limit 'email me when stock reaches' field to numerical only (#38353) * Limit 'email me when stock reaches' field to numerical only * Add changelog file --- .../js/product-editor/changelog/fix-38244 | 4 + .../src/blocks/inventory-email/edit.tsx | 83 ++++++++++++++----- .../src/blocks/inventory-email/index.ts | 27 +++--- .../src/blocks/inventory-email/types.ts | 8 ++ 4 files changed, 89 insertions(+), 33 deletions(-) create mode 100644 packages/js/product-editor/changelog/fix-38244 create mode 100644 packages/js/product-editor/src/blocks/inventory-email/types.ts diff --git a/packages/js/product-editor/changelog/fix-38244 b/packages/js/product-editor/changelog/fix-38244 new file mode 100644 index 0000000000000..627bbf7477306 --- /dev/null +++ b/packages/js/product-editor/changelog/fix-38244 @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Limit 'email me when stock reaches' field to numerical only#38244 diff --git a/packages/js/product-editor/src/blocks/inventory-email/edit.tsx b/packages/js/product-editor/src/blocks/inventory-email/edit.tsx index ead26d920999c..916b885e33be1 100644 --- a/packages/js/product-editor/src/blocks/inventory-email/edit.tsx +++ b/packages/js/product-editor/src/blocks/inventory-email/edit.tsx @@ -3,16 +3,16 @@ */ import { __, sprintf } from '@wordpress/i18n'; import { Link } from '@woocommerce/components'; - +import { Product } from '@woocommerce/data'; import { createElement, Fragment, createInterpolateElement, } from '@wordpress/element'; import { getSetting } from '@woocommerce/settings'; - +import { BlockEditProps } from '@wordpress/blocks'; import { useBlockProps } from '@wordpress/block-editor'; - +import { useInstanceId } from '@wordpress/compose'; import { BaseControl, // @ts-expect-error `__experimentalInputControl` does exist. @@ -23,54 +23,91 @@ import { // eslint-disable-next-line @woocommerce/dependency-group import { useEntityProp } from '@wordpress/core-data'; -export function Edit() { +/** + * Internal dependencies + */ +import { useValidation } from '../../contexts/validation-context'; +import { InventoryEmailBlockAttributes } from './types'; + +export function Edit( { + clientId, +}: BlockEditProps< InventoryEmailBlockAttributes > ) { const blockProps = useBlockProps(); const notifyLowStockAmount = getSetting( 'notifyLowStockAmount', 2 ); - const [ lowStockAmount, setLowStockAmount ] = useEntityProp( + const [ lowStockAmount, setLowStockAmount ] = useEntityProp< number >( 'postType', 'product', 'low_stock_amount' ); + const id = useInstanceId( BaseControl, 'low_stock_amount' ) as string; + + const { + ref: lowStockAmountRef, + error: lowStockAmountValidationError, + validate: validateLowStockAmount, + } = useValidation< Product >( + `low_stock_amount-${ clientId }`, + async function stockQuantityValidator() { + if ( lowStockAmount && lowStockAmount < 0 ) { + return __( + 'This field must be a positive number.', + 'woocommerce' + ); + } + }, + [ lowStockAmount ] + ); + return ( <>
store settings.', - 'woocommerce' - ), - { - link: ( - + help={ + lowStockAmountValidationError || + createInterpolateElement( + __( + 'Make sure to enable notifications in store settings.', + 'woocommerce' ), - } - ) } + { + link: ( + + ), + } + ) + } + className={ + lowStockAmountValidationError && 'has-error' + } > diff --git a/packages/js/product-editor/src/blocks/inventory-email/index.ts b/packages/js/product-editor/src/blocks/inventory-email/index.ts index 59d35906a1224..f022db4925477 100644 --- a/packages/js/product-editor/src/blocks/inventory-email/index.ts +++ b/packages/js/product-editor/src/blocks/inventory-email/index.ts @@ -1,22 +1,29 @@ +/** + * External dependencies + */ +import { createElement } from '@wordpress/element'; +import { BlockConfiguration } from '@wordpress/blocks'; + /** * Internal dependencies */ -import { initBlock } from '../../utils'; -import metadata from './block.json'; +import { initBlock } from '../../utils/init-blocks'; +import blockConfiguration from './block.json'; import { Edit } from './edit'; +import { InventoryEmailBlockAttributes } from './types'; -const { name } = metadata; +const { name, ...metadata } = + blockConfiguration as BlockConfiguration< InventoryEmailBlockAttributes >; export { metadata, name }; -export const settings = { +export const settings: Partial< + BlockConfiguration< InventoryEmailBlockAttributes > +> = { example: {}, edit: Edit, }; -export const init = () => - initBlock( { - name, - metadata: metadata as never, - settings, - } ); +export function init() { + return initBlock( { name, metadata, settings } ); +} diff --git a/packages/js/product-editor/src/blocks/inventory-email/types.ts b/packages/js/product-editor/src/blocks/inventory-email/types.ts new file mode 100644 index 0000000000000..a34d55343f590 --- /dev/null +++ b/packages/js/product-editor/src/blocks/inventory-email/types.ts @@ -0,0 +1,8 @@ +/** + * External dependencies + */ +import { BlockAttributes } from '@wordpress/blocks'; + +export interface InventoryEmailBlockAttributes extends BlockAttributes { + name: string; +}