Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Product Search Results template: fix the preview when the Inherit Que…
Browse files Browse the repository at this point in the history
…ry option is enabled (#7965)

* Product Search Results: fix the preview when the Inherit Query option is enabled

* create a component to make the code more readable

---------

Co-authored-by: Manish Menaria <[email protected]>
  • Loading branch information
gigitux and imanish003 authored Mar 16, 2023
1 parent 18916c3 commit 8624555
Showing 1 changed file with 64 additions and 29 deletions.
93 changes: 64 additions & 29 deletions assets/js/blocks/product-query/inspector-controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useSelect } from '@wordpress/data';
import { addFilter } from '@wordpress/hooks';
import { ProductQueryFeedbackPrompt } from '@woocommerce/editor-components/feedback-prompt';
import { EditorBlock } from '@woocommerce/types';
import { usePrevious } from '@woocommerce/base-hooks';
import {
FormTokenField,
ToggleControl,
Expand All @@ -33,6 +34,7 @@ import {
} from './utils';
import {
ALL_PRODUCT_QUERY_CONTROLS,
QUERY_DEFAULT_ATTRIBUTES,
QUERY_LOOP_ID,
STOCK_STATUS_OPTIONS,
} from './constants';
Expand Down Expand Up @@ -88,6 +90,59 @@ function getStockStatusIdByLabel( statusLabel: FormTokenField.Value ) {
)?.[ 0 ];
}

export const WooInheritToggleControl = (
props: ProductQueryBlock & {
defaultWooQueryParams: Partial< ProductQueryArguments >;
}
) => {
const queryObjectBeforeInheritEnabled = usePrevious(
props.attributes.query,
( value ) => {
return value.inherit === false;
}
);

return (
<ToggleControl
className="woo-inherit-query-toggle"
label={ __(
'Inherit query from template',
'woo-gutenberg-products-block'
) }
help={ __(
'Toggle to use the global query context that is set with the current template, such as variations of the product catalog or search. Disable to customize the filtering independently.',
'woo-gutenberg-products-block'
) }
checked={
isCustomInheritGlobalQueryImplementationEnabled
? props.attributes.query.__woocommerceInherit || false
: props.attributes.query.inherit || false
}
onChange={ ( inherit ) => {
if ( isCustomInheritGlobalQueryImplementationEnabled ) {
return setQueryAttribute( props, {
...QUERY_DEFAULT_ATTRIBUTES.query,
__woocommerceInherit: inherit,
// Restore the query object value before inherit was enabled.
...( inherit === false && {
...queryObjectBeforeInheritEnabled,
} ),
} );
}

setQueryAttribute( props, {
...props.defaultWooQueryParams,
inherit,
// Restore the query object value before inherit was enabled.
...( inherit === false && {
...queryObjectBeforeInheritEnabled,
} ),
} );
} }
/>
);
};

export const TOOLS_PANEL_CONTROLS = {
attributes: AttributesFilter,
onSale: ( props: ProductQueryBlock ) => {
Expand Down Expand Up @@ -149,41 +204,15 @@ export const TOOLS_PANEL_CONTROLS = {
</ToolsPanelItem>
);
},
wooInherit: ( props: ProductQueryBlock ) => {
return (
<ToggleControl
className="woo-inherit-query-toggle"
label={ __(
'Inherit query from template',
'woo-gutenberg-products-block'
) }
help={ __(
'Toggle to use the global query context that is set with the current template, such as variations of the product catalog or search. Disable to customize the filtering independently.',
'woo-gutenberg-products-block'
) }
checked={
isCustomInheritGlobalQueryImplementationEnabled
? props.attributes.query.__woocommerceInherit || false
: props.attributes.query.inherit || false
}
onChange={ ( inherit ) => {
if ( isCustomInheritGlobalQueryImplementationEnabled ) {
return setQueryAttribute( props, {
__woocommerceInherit: inherit,
} );
}
return setQueryAttribute( props, { inherit } );
} }
/>
);
},
wooInherit: WooInheritToggleControl,
};

const ProductQueryControls = ( props: ProductQueryBlock ) => {
const allowedControls = useAllowedControls( props.attributes );
const defaultWooQueryParams = useDefaultWooQueryParamsForVariation(
props.attributes.namespace
);

return (
<>
<InspectorControls>
Expand All @@ -203,7 +232,13 @@ const ProductQueryControls = ( props: ProductQueryBlock ) => {
{ Object.entries( TOOLS_PANEL_CONTROLS ).map(
( [ key, Control ] ) =>
allowedControls?.includes( key ) ? (
<Control { ...props } key={ key } />
<Control
{ ...props }
defaultWooQueryParams={
defaultWooQueryParams
}
key={ key }
/>
) : null
) }
</ToolsPanel>
Expand Down

0 comments on commit 8624555

Please sign in to comment.