Request variations conditionally (#47360)

* Get variations conditionally

* Make second call conditionally

* Add changelog

* Remove usesContext from JSON
This commit is contained in:
Fernando Marichal 2024-05-13 10:57:03 -03:00 committed by GitHub
parent 5bbbc52c0d
commit 69fa74ef8d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 17 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: dev
Request variations conditionally #47360

View File

@ -32,7 +32,7 @@ import { EmptyState } from './empty-state';
export function Edit( {
attributes,
context,
context: { isInSelectedTab },
}: ProductEditorBlockEditProps< VariationOptionsBlockAttributes > ) {
const noticeDimissed = useRef( false );
const { invalidateResolution } = useDispatch(
@ -79,14 +79,19 @@ export function Edit( {
);
return {
totalCountWithoutPrice: productHasOptions
? getProductVariationsTotalCount< number >(
totalCountWithoutPriceRequestParams
)
: 0,
totalCountWithoutPrice:
isInSelectedTab && productHasOptions
? getProductVariationsTotalCount< number >(
totalCountWithoutPriceRequestParams
)
: 0,
};
},
[ productHasOptions, totalCountWithoutPriceRequestParams ]
[
isInSelectedTab,
productHasOptions,
totalCountWithoutPriceRequestParams,
]
);
const {
@ -184,6 +189,7 @@ export function Edit( {
return (
<div { ...blockProps }>
<VariationsTable
isVisible={ isInSelectedTab }
ref={ variationTableRef as React.Ref< HTMLDivElement > }
noticeText={ noticeText }
onNoticeDismiss={ () => {
@ -220,7 +226,7 @@ export function Edit( {
}
} }
/>
{ context.isInSelectedTab && <VariableProductTour /> }
{ isInSelectedTab && <VariableProductTour /> }
</div>
);
}

View File

@ -8,13 +8,7 @@ import {
ProductVariation,
} from '@woocommerce/data';
import { dispatch, resolveSelect } from '@wordpress/data';
import {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from '@wordpress/element';
import { useCallback, useMemo, useRef, useState } from '@wordpress/element';
/**
* Internal dependencies
@ -487,7 +481,7 @@ export function useVariations( { productId }: UseVariationsProps ) {
const wasGenerating = useRef( false );
useEffect( () => {
function getCurrentVariations() {
if ( isGenerating ) {
setFilters( [] );
onClearSelection();
@ -508,7 +502,7 @@ export function useVariations( { productId }: UseVariationsProps ) {
}
wasGenerating.current = Boolean( isGenerating );
}, [ productId, isGenerating ] );
}
return {
isLoading,
@ -541,5 +535,6 @@ export function useVariations( { productId }: UseVariationsProps ) {
isGenerating,
onGenerate,
variationsError: generateError ?? getVariationsError,
getCurrentVariations,
};
}

View File

@ -15,6 +15,7 @@ import {
Fragment,
forwardRef,
useMemo,
useEffect,
} from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@ -35,6 +36,7 @@ import { VariationsTableRow } from './variations-table-row';
import { MultipleUpdateMenu } from './variation-actions-menus';
type VariationsTableProps = {
isVisible?: boolean;
noticeText?: string;
noticeStatus?: 'error' | 'warning' | 'success' | 'info';
onNoticeDismiss?: () => void;
@ -97,6 +99,7 @@ export const VariationsTable = forwardRef<
VariationsTableProps
>( function Table(
{
isVisible = false,
noticeText,
noticeActions = [],
noticeStatus = 'error',
@ -158,8 +161,15 @@ export const VariationsTable = forwardRef<
isGenerating,
variationsError,
onGenerate,
getCurrentVariations,
} = useVariations( { productId } );
useEffect( () => {
if ( isVisible ) {
getCurrentVariations();
}
}, [ isVisible, isGenerating, productId ] );
function handleEmptyTableStateActionClick() {
onGenerate( productAttributes );
}