Skip to content

Commit

Permalink
Fix TS errors in plugins/woocommerce/client/admin/client/products/ (w…
Browse files Browse the repository at this point in the history
…oocommerce#54129)

* Fix TS errors in plugins/woocommerce/client/admin/client/products/fills/more-menu-items/classic-editor-menu-item.tsx

* Fix TS errors in plugins/woocommerce/client/admin/client/products/fills/more-menu-items/delete-variation-menu-item.tsx

* Fix TS errors in plugins/woocommerce/client/admin/client/products/fills/product-block-editor-fills.tsx

* Update some fix to be more generics

* Fix TS errors in plugins/woocommerce/client/admin/client/products/hooks/use-product-entity-record.ts

* Fix TS errors in plugins/woocommerce/client/admin/client/products/hooks/use-product-variation-entity-record.ts

* Fix plugins/woocommerce/client/admin/client/products/tour/block-editor/use-published-products-count.tsx

* Fix plugins/woocommerce/client/admin/client/products/tour/block-editor/use-block-editor-tour-options.ts

* Add TODOs to useSelect related fixes

* Update todo to remove error expect

* Cover the type problems regarding Slot Fills

* Add changelog

* Expect TS error and mark two places for further investigation

* Update the TS error expect

* Fix function as a children problem in two Slot Fill places

* Add missing changelog files
  • Loading branch information
kmanijak authored Jan 10, 2025
1 parent 49f1f20 commit 2c193d1
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 35 deletions.
4 changes: 4 additions & 0 deletions packages/js/components/changelog/fix-54123
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: update

Typescript types update
16 changes: 12 additions & 4 deletions packages/js/components/src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type SlotProps = React.ComponentProps< typeof Slot >;
function getChildrenAndProps< T = FillProps, S = Record< string, unknown > >(
children:
| React.ReactNode
| ( ( props: T & { order: number } ) => React.ReactElement ),
| ( ( props: T & { order: number } ) => React.ReactNode ),
order: number,
props: T,
injectProps?: S
Expand Down Expand Up @@ -65,14 +65,22 @@ function getChildrenAndProps< T = FillProps, S = Record< string, unknown > >(
* @return {Node} Node.
*/
function createOrderedChildren< T = FillProps, S = Record< string, unknown > >(
children: React.ReactNode,
children:
| React.ReactNode
| ( ( props: T & { order: number } ) => React.ReactNode ),
order: number,
props: T,
injectProps?: S
): React.ReactElement {
): React.ReactNode {
const { children: childrenToRender, props: propsToRender } =
getChildrenAndProps( children, order, props, injectProps );
return cloneElement( childrenToRender, propsToRender );
if ( ! childrenToRender || typeof childrenToRender === 'string' ) {
return childrenToRender;
}
return cloneElement(
childrenToRender as React.ReactElement,
propsToRender
);
}
export { createOrderedChildren };

Expand Down
4 changes: 4 additions & 0 deletions packages/js/product-editor/changelog/fix-54123
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: update

Typescript types update
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import {

export const WC_PRODUCT_MORE_MENU_SLOT_NAME = 'WooProductMenuMenuItem';

type FillProps = React.ComponentProps< typeof Fill >;
export const WooProductMoreMenuItem: React.FC< {
children?: React.ReactNode;
children?: FillProps[ 'children' ];
order?: number;
} > & {
Slot: React.FC< Slot.Props >;
Expand Down
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/fix-54123
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: dev

Fix TS errors in plugins/woocommerce/admin/client/products
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ export const ClassicEditorMenuItem = ( {
select( OPTIONS_STORE_NAME );

const allowTrackingOption =
// @ts-expect-error Todo: awaiting more global fix, demo: https://github.com/woocommerce/woocommerce/pull/54146
getOption( ALLOW_TRACKING_OPTION_NAME ) || 'no';

// @ts-expect-error Todo: awaiting more global fix, demo: https://github.com/woocommerce/woocommerce/pull/54146
const resolving = ! hasFinishedResolution( 'getOption', [
ALLOW_TRACKING_OPTION_NAME,
] );
Expand All @@ -40,7 +42,7 @@ export const ClassicEditorMenuItem = ( {
allowTracking: allowTrackingOption === 'yes',
resolving,
};
} );
}, [] );

const _feature_nonce = getAdminSetting( '_feature_nonce' );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { MenuGroup, MenuItem } from '@wordpress/components';
import { useDispatch } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import {
EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME,
ProductVariation,
} from '@woocommerce/data';
import { EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME } from '@woocommerce/data';
import { getNewPath, navigateTo } from '@woocommerce/navigation';
import {
RemoveConfirmationModal,
Expand Down Expand Up @@ -42,13 +39,9 @@ export const DeleteVariationMenuItem = ( {
variationId,
} );

const [ name ] = useEntityProp< string >(
'postType',
'product_variation',
'name'
);
const [ name ] = useEntityProp( 'postType', 'product_variation', 'name' );

const [ status ] = useEntityProp< string >(
const [ status ] = useEntityProp(
'postType',
'product_variation',
'status'
Expand Down Expand Up @@ -80,7 +73,7 @@ export const DeleteVariationMenuItem = ( {
product_status: status,
} );

return deleteProductVariation< Promise< ProductVariation > >( {
return deleteProductVariation( {
product_id: productId,
id: variationId,
} )
Expand Down Expand Up @@ -128,11 +121,7 @@ export const DeleteVariationMenuItem = ( {
return (
<>
<MenuGroup>
<MenuItem
isDestructive
variant="tertiary"
onClick={ handleMenuItemClick }
>
<MenuItem isDestructive onClick={ handleMenuItemClick }>
{ __( 'Delete variation', 'woocommerce' ) }
</MenuItem>
</MenuGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { __ } from '@wordpress/i18n';
import { recordEvent } from '@woocommerce/tracks';
import { useSelect } from '@wordpress/data';
import { Product, ProductVariation } from '@woocommerce/data';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore No types for this exist yet.
// eslint-disable-next-line @woocommerce/dependency-group
import { useEntityProp } from '@wordpress/core-data';
import { store as coreStore, useEntityProp } from '@wordpress/core-data';

/**
* Internal dependencies
Expand All @@ -28,9 +25,9 @@ export const MoreMenuFill = ( {
}: MoreMenuFillProps ) => {
const [ id ] = useEntityProp( 'postType', productType, 'id' );

const product = useSelect< Product | ProductVariation >(
const product = useSelect(
( select ) => {
const { getEntityRecord } = select( 'core' );
const { getEntityRecord } = select( coreStore );

return getEntityRecord( 'postType', productType, id ) as
| Product
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { AUTO_DRAFT_NAME } from '@woocommerce/product-editor';
import { type Product } from '@woocommerce/data';
import { store as coreStore } from '@wordpress/core-data';
import { dispatch } from '@wordpress/data';
import { useEffect, useState } from '@wordpress/element';

Expand All @@ -17,7 +18,7 @@ export function useProductEntityRecord(
return;
}

const createProductPromise = dispatch( 'core' ).saveEntityRecord(
const createProductPromise = dispatch( coreStore ).saveEntityRecord(
'postType',
'product',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { ProductVariation } from '@woocommerce/data';
import { resolveSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { useEffect, useState } from '@wordpress/element';

export function useProductVariationEntityRecord(
Expand All @@ -14,8 +15,8 @@ export function useProductVariationEntityRecord(

useEffect( () => {
const getRecordPromise: Promise< ProductVariation > = resolveSelect(
'core'
).getEntityRecord< ProductVariation >(
coreStore
).getEntityRecord(
'postType',
'product_variation',
Number.parseInt( variationId, 10 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default function ProductPage() {
return (
<>
<WooProductMoreMenuItem>
{ ( { onClose }: { onClose: () => void } ) => (
{ ( { onClose } ) => (
<MoreMenuFill onClose={ onClose } />
) }
</WooProductMoreMenuItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function ProductPage() {
render: () => (
<>
<WooProductMoreMenuItem>
{ ( { onClose }: { onClose: () => void } ) => (
{ ( { onClose } ) => (
<>
<DeleteVariationMenuItem onClose={ onClose } />
<MoreMenuFill
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ export const useBlockEditorTourOptions = () => {
select( OPTIONS_STORE_NAME );

const wasTourShown =
// @ts-expect-error Todo: awaiting more global fix, demo: https://github.com/woocommerce/woocommerce/pull/54146
getOption( BLOCK_EDITOR_TOUR_SHOWN_OPTION ) === 'yes' ||
// @ts-expect-error Todo: awaiting more global fix, demo: https://github.com/woocommerce/woocommerce/pull/54146
! hasFinishedResolution( 'getOption', [
BLOCK_EDITOR_TOUR_SHOWN_OPTION,
] );

return {
shouldTourBeShown: ! wasTourShown,
};
} );
}, [] );

const dismissModal = () => {
updateOptions( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export const usePublishedProductsCount = () => {
const { getProductsTotalCount, hasFinishedResolution } =
select( PRODUCTS_STORE_NAME );

// @ts-expect-error Todo: awaiting more global fix, demo: https://github.com/woocommerce/woocommerce/pull/54146
const publishedProductsCount = getProductsTotalCount(
PUBLISHED_PRODUCTS_QUERY_PARAMS,
0
) as number;

// @ts-expect-error Todo: awaiting more global fix, demo: https://github.com/woocommerce/woocommerce/pull/54146
const loadingPublishedProductsCount = ! hasFinishedResolution(
'getProductsTotalCount',
[ PUBLISHED_PRODUCTS_QUERY_PARAMS, 0 ]
Expand All @@ -30,5 +32,5 @@ export const usePublishedProductsCount = () => {
// we consider a user new if they have no published products
isNewUser: publishedProductsCount < 1,
};
} );
}, [] );
};

0 comments on commit 2c193d1

Please sign in to comment.