diff --git a/src/admin/product-blocks/stock-management-field/edit.js b/src/admin/product-blocks/stock-management-field/edit.js index b2ffa900..aa880084 100644 --- a/src/admin/product-blocks/stock-management-field/edit.js +++ b/src/admin/product-blocks/stock-management-field/edit.js @@ -12,50 +12,55 @@ import { useEffect, useState } from '@wordpress/element'; import { __experimentalUseProductEntityProp as useProductEntityProp } from '@woocommerce/product-editor'; import parse from 'html-react-parser'; -export const Edit = ({ attributes, context: { postType } }) => { +export const Edit = ( { attributes, context: { postType } } ) => { const { disabled, disabledCopy } = attributes; - const [manageStock, setManageStock] = useProductEntityProp('manage_stock', { - postType, - }); - const [isSquareSynced, setIsSquareSynced] = useProductEntityProp( + const [ manageStock, setManageStock ] = useProductEntityProp( + 'manage_stock', + { + postType, + } + ); + const [ isSquareSynced, setIsSquareSynced ] = useProductEntityProp( 'is_square_synced', { postType } ); - const [isInventorySyncEnabled] = useProductEntityProp( + const [ isInventorySyncEnabled ] = useProductEntityProp( 'is_inventory_sync_enabled', { postType } ); - const [isSyncEnabled] = useProductEntityProp('is_sync_enabled', { + const [ isSyncEnabled ] = useProductEntityProp( 'is_sync_enabled', { + postType, + } ); + const [ sku ] = useProductEntityProp( 'sku', { postType } ); + const [ sor ] = useProductEntityProp( 'sor', { postType } ); + const [ isGiftCard ] = useProductEntityProp( 'is_gift_card', { postType } ); + const [ isStockManaged ] = useProductEntityProp( 'manage_stock', { postType, - }); - const [sku] = useProductEntityProp('sku', { postType }); - const [sor] = useProductEntityProp('sor', { postType }); - const [isGiftCard] = useProductEntityProp('is_gift_card', { postType }); - const [isStockManaged] = useProductEntityProp('manage_stock', { postType }); - const [productStatus] = useProductEntityProp('status', { postType }); - const [productId] = useProductEntityProp('id', { postType }); - const [fetchStockNonce] = useProductEntityProp('fetch_stock_nonce', { + } ); + const [ productStatus ] = useProductEntityProp( 'status', { postType } ); + const [ productId ] = useProductEntityProp( 'id', { postType } ); + const [ fetchStockNonce ] = useProductEntityProp( 'fetch_stock_nonce', { postType, - }); - const [isInventorySyncing, setIsInventorySyncing] = useState(false); - const [inventorySyncMessage, setInventorySyncMessage] = useState(''); - let label = __('Track quantity for this product', 'woocommerce-square'); + } ); + const [ isInventorySyncing, setIsInventorySyncing ] = useState( false ); + const [ inventorySyncMessage, setInventorySyncMessage ] = useState( '' ); + let label = __( 'Track quantity for this product', 'woocommerce-square' ); let helpText = ''; - useEffect(() => { - if (isGiftCard === 'yes') { - setIsSquareSynced(false); + useEffect( () => { + if ( isGiftCard === 'yes' ) { + setIsSquareSynced( false ); } - }, [isGiftCard]); + }, [ isGiftCard ] ); - useEffect(() => { - if (sku.length) { + useEffect( () => { + if ( sku.length ) { return; } - setIsSquareSynced(false); - }, [sku]); + setIsSquareSynced( false ); + }, [ sku ] ); const syncInventory = async () => { const fetchStockData = new FormData(); @@ -64,46 +69,46 @@ export const Edit = ({ attributes, context: { postType } }) => { 'action', 'wc_square_fetch_product_stock_with_square' ); - fetchStockData.append('security', fetchStockNonce); - fetchStockData.append('product_id', productId); + fetchStockData.append( 'security', fetchStockNonce ); + fetchStockData.append( 'product_id', productId ); - setIsInventorySyncing(true); - setInventorySyncMessage(''); + setIsInventorySyncing( true ); + setInventorySyncMessage( '' ); - let response = await fetch(window.ajaxurl, { + let response = await fetch( window.ajaxurl, { method: 'POST', body: fetchStockData, - }); + } ); response = await response.json(); - if (!response.success) { - setInventorySyncMessage(response.data); + if ( ! response.success ) { + setInventorySyncMessage( response.data ); } - setIsInventorySyncing(false); + setIsInventorySyncing( false ); }; - if (isSquareSynced && isSyncEnabled && sku.length) { + if ( isSquareSynced && isSyncEnabled && sku.length ) { label = ( - {__('Synced with Square', 'woocommerce-square')} + { __( 'Synced with Square', 'woocommerce-square' ) } ); } - if (disabled && disabledCopy) { - helpText = parse(disabledCopy); + if ( disabled && disabledCopy ) { + helpText = parse( disabledCopy ); } let canManageStock = true; const isPublished = productStatus === 'publish'; - if (disabled || (isSquareSynced && isSyncEnabled && sku.length)) { - if (!isInventorySyncEnabled || sor === 'square') { + if ( disabled || ( isSquareSynced && isSyncEnabled && sku.length ) ) { + if ( ! isInventorySyncEnabled || sor === 'square' ) { canManageStock = false; } else if ( - !(disabled || (isSquareSynced && isSyncEnabled && sku.length)) + ! ( disabled || ( isSquareSynced && isSyncEnabled && sku.length ) ) ) { canManageStock = true; } else { @@ -115,37 +120,37 @@ export const Edit = ({ attributes, context: { postType } }) => { <> - {!isStockManaged && isSquareSynced && isPublished && ( + { ! isStockManaged && isSquareSynced && isPublished && ( <> ( ) - )} + ) } - {inventorySyncMessage && ( -

- {inventorySyncMessage} + { inventorySyncMessage && ( +

+ { inventorySyncMessage }

- )} + ) }
); diff --git a/src/admin/product-blocks/stock-quantity-field/edit.js b/src/admin/product-blocks/stock-quantity-field/edit.js index 47d5c3a7..1cb19afd 100644 --- a/src/admin/product-blocks/stock-quantity-field/edit.js +++ b/src/admin/product-blocks/stock-quantity-field/edit.js @@ -15,43 +15,50 @@ import { __experimentalUseProductEntityProp as useProductEntityProp, } from '@woocommerce/product-editor'; -export function Edit({ attributes, clientId, context }) { - const blockProps = useWooBlockProps(attributes); +export function Edit( { attributes, clientId, context } ) { + const blockProps = useWooBlockProps( attributes ); const stockQuantityId = useInstanceId( BaseControl, 'product_stock_quantity' ); - const [manageStock] = useProductEntityProp( + const [ manageStock ] = useProductEntityProp( 'manage_stock', context.postType ); - const [stockQuantity, setStockQuantity] = useProductEntityProp( + const [ stockQuantity, setStockQuantity ] = useProductEntityProp( 'stock_quantity', context.postType ); - const [productId] = useProductEntityProp('id', context.postType); - const [isSquareSynced, setIsSquareSynced] = useProductEntityProp( + const [ productId ] = useProductEntityProp( 'id', context.postType ); + const [ isSquareSynced, setIsSquareSynced ] = useProductEntityProp( 'is_square_synced', context.postType ); - const [isSyncEnabled] = useProductEntityProp( + const [ isSyncEnabled ] = useProductEntityProp( 'is_sync_enabled', context.postType ); - const [isInventorySyncEnabled, setIsInventorySyncEnabled] = - useProductEntityProp('is_inventory_sync_enabled', context.postType); - const [fetchStockNonce] = useProductEntityProp( + const [ isInventorySyncEnabled, setIsInventorySyncEnabled ] = + useProductEntityProp( 'is_inventory_sync_enabled', context.postType ); + const [ fetchStockNonce ] = useProductEntityProp( 'fetch_stock_nonce', context.postType ); - const [sor] = useProductEntityProp('sor', context.postType); - const [productStatus] = useProductEntityProp('status', context.postType); - const [fetchStockProgress, setFetchStockProgress] = useState(false); - const [isQuantityDisabled, setIsQuantityDisabled] = useState(true); - const [isQuantityNull, setIsQuantityNull] = useState(false); - const [canManageStockInSquare, setCanManageStockInSquare] = useState(null); - const [isGiftCard] = useProductEntityProp('is_gift_card', context.postType); + const [ sor ] = useProductEntityProp( 'sor', context.postType ); + const [ productStatus ] = useProductEntityProp( + 'status', + context.postType + ); + const [ fetchStockProgress, setFetchStockProgress ] = useState( false ); + const [ isQuantityDisabled, setIsQuantityDisabled ] = useState( true ); + const [ isQuantityNull, setIsQuantityNull ] = useState( false ); + const [ canManageStockInSquare, setCanManageStockInSquare ] = + useState( null ); + const [ isGiftCard ] = useProductEntityProp( + 'is_gift_card', + context.postType + ); const isPublished = productStatus === 'publish'; const { @@ -59,129 +66,131 @@ export function Edit({ attributes, clientId, context }) { error: stockQuantityValidationError, validate: validateStockQuantity, } = useValidation( - `stock_quantity-${clientId}`, + `stock_quantity-${ clientId }`, async function stockQuantityValidator() { - if (manageStock && stockQuantity && stockQuantity < 0) { + if ( manageStock && stockQuantity && stockQuantity < 0 ) { return __( 'Stock quantity must be a positive number.', 'woocommerce-square' ); } }, - [manageStock, stockQuantity] + [ manageStock, stockQuantity ] ); - useEffect(() => { - if (isGiftCard === 'yes') { - setIsSquareSynced(false); + useEffect( () => { + if ( isGiftCard === 'yes' ) { + setIsSquareSynced( false ); } - }, [isGiftCard]); + }, [ isGiftCard ] ); - useEffect(() => { - if (manageStock && stockQuantity === null) { - setStockQuantity(1); + useEffect( () => { + if ( manageStock && stockQuantity === null ) { + setStockQuantity( 1 ); } - }, [manageStock, stockQuantity]); + }, [ manageStock, stockQuantity ] ); - useEffect(() => { - setIsQuantityNull(stockQuantity === null); - }, [stockQuantity]); + useEffect( () => { + setIsQuantityNull( stockQuantity === null ); + }, [ stockQuantity ] ); - async function fetchStockFromSquare(enableQuantityField = false) { + async function fetchStockFromSquare( enableQuantityField = false ) { const fetchStockData = new FormData(); fetchStockData.append( 'action', 'wc_square_fetch_product_stock_with_square' ); - fetchStockData.append('security', fetchStockNonce); - fetchStockData.append('product_id', productId); + fetchStockData.append( 'security', fetchStockNonce ); + fetchStockData.append( 'product_id', productId ); - setFetchStockProgress(true); - setCanManageStockInSquare(null); + setFetchStockProgress( true ); + setCanManageStockInSquare( null ); - let response = await fetch(window.ajaxurl, { + let response = await fetch( window.ajaxurl, { method: 'POST', body: fetchStockData, - }); + } ); response = await response.json(); - if (response.success) { + if ( response.success ) { const { quantity, manage_stock } = response.data; - if (enableQuantityField) { - setIsQuantityDisabled(false); + if ( enableQuantityField ) { + setIsQuantityDisabled( false ); } - setCanManageStockInSquare(manage_stock); + setCanManageStockInSquare( manage_stock ); - if (manage_stock === false) { - setIsInventorySyncEnabled(false); - setIsQuantityDisabled(true); + if ( manage_stock === false ) { + setIsInventorySyncEnabled( false ); + setIsQuantityDisabled( true ); } - if (quantity !== null) { - setStockQuantity(Number(quantity)); - setIsQuantityNull(false); + if ( quantity !== null ) { + setStockQuantity( Number( quantity ) ); + setIsQuantityNull( false ); } else { - setIsQuantityNull(true); + setIsQuantityNull( true ); } } - setFetchStockProgress(false); + setFetchStockProgress( false ); } - if (isSquareSynced && canManageStockInSquare === false) { + if ( isSquareSynced && canManageStockInSquare === false ) { return ( -
-
- {__( +
+
+ { __( 'Inventory tracking is disabled for this product. Enable it from the Square dashboard.', 'woocommerce-square' - )} + ) }
); } - if (!manageStock) { + if ( ! manageStock ) { return null; } - if (isSquareSynced && isQuantityNull) { + if ( isSquareSynced && isQuantityNull ) { return null; } return ( -
+
- {isPublished && + { isPublished && isSyncEnabled && isInventorySyncEnabled && isSquareSynced && @@ -191,15 +200,21 @@ export function Edit({ attributes, clientId, context }) {

- )} + ) } - {isPublished && + { isPublished && isSyncEnabled && isInventorySyncEnabled && isSquareSynced && @@ -208,17 +223,17 @@ export function Edit({ attributes, clientId, context }) {