Skip to content

Commit

Permalink
Hide Variations section when it is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Marichal committed Jan 5, 2023
1 parent 390c9ca commit eaaf86b
Showing 1 changed file with 29 additions and 1 deletion.
Expand Up @@ -3,7 +3,12 @@
*/
import { __ } from '@wordpress/i18n';
import { recordEvent } from '@woocommerce/tracks';
import { Link } from '@woocommerce/components';
import { Link, useFormContext } from '@woocommerce/components';
import { useSelect } from '@wordpress/data';
import {
EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME,
Product,
} from '@woocommerce/data';

/**
* Internal dependencies
Expand All @@ -12,6 +17,29 @@ import { ProductSectionLayout } from '../layout/product-section-layout';
import { Variations } from '../fields/variations';

export const ProductVariationsSection: React.FC = () => {
const { values } = useFormContext< Product >();
const productId = values.id;

const { totalCount } = useSelect(
( select ) => {
const { getProductVariationsTotalCount } = select(
EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME
);
const requestParams = {
product_id: productId,
};
return {
totalCount:
getProductVariationsTotalCount< number >( requestParams ),
};
},
[ productId ]
);

if ( totalCount === 0 || isNaN( totalCount ) ) {
return null;
}

return (
<ProductSectionLayout
title={ __( 'Variations', 'woocommerce' ) }
Expand Down

0 comments on commit eaaf86b

Please sign in to comment.