Skip to content

Commit

Permalink
eslint error fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidsector9 committed Aug 5, 2024
1 parent 5917791 commit 46b4ee0
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 140 deletions.
123 changes: 64 additions & 59 deletions src/admin/product-blocks/stock-management-field/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 = (
<a href="/wp-admin/admin.php?page=wc-settings&tab=square">
{__('Synced with Square', 'woocommerce-square')}
{ __( 'Synced with Square', 'woocommerce-square' ) }
</a>
);
}

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 {
Expand All @@ -115,37 +120,37 @@ export const Edit = ({ attributes, context: { postType } }) => {
<>
<BaseControl
id="wc-square-track-quantity"
label={__('Stock management', 'woocommerce-square')}
label={ __( 'Stock management', 'woocommerce-square' ) }
className="wc-square-track-quantity"
>
<Flex justify="flex-start">
<ToggleControl
label={label}
disabled={!canManageStock}
help={helpText}
checked={manageStock}
onChange={setManageStock}
label={ label }
disabled={ ! canManageStock }
help={ helpText }
checked={ manageStock }
onChange={ setManageStock }
/>
{!isStockManaged && isSquareSynced && isPublished && (
{ ! isStockManaged && isSquareSynced && isPublished && (
<>
(
<Button
variant="link"
disabled={isInventorySyncing}
isBusy={isInventorySyncing}
onClick={syncInventory}
disabled={ isInventorySyncing }
isBusy={ isInventorySyncing }
onClick={ syncInventory }
>
{__('Sync Inventory', 'woocommerce-square')}
{ __( 'Sync Inventory', 'woocommerce-square' ) }
</Button>
)
</>
)}
) }
</Flex>
{inventorySyncMessage && (
<p style={{ color: 'rgb(170, 0, 0)' }}>
{inventorySyncMessage}
{ inventorySyncMessage && (
<p style={ { color: 'rgb(170, 0, 0)' } }>
{ inventorySyncMessage }
</p>
)}
) }
</BaseControl>
</>
);
Expand Down
Loading

0 comments on commit 46b4ee0

Please sign in to comment.