Skip to content

Commit

Permalink
Extract usePublishedProductsCount hook and use it in about-the-editor…
Browse files Browse the repository at this point in the history
…-menu-item (woocommerce#38742)
  • Loading branch information
nathanss authored Jun 15, 2023
1 parent 2471dcb commit bfd720c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import { recordEvent } from '@woocommerce/tracks';
* Internal dependencies
*/
import BlockEditorGuide from '~/products/tour/block-editor/block-editor-guide';
import { usePublishedProductsCount } from '~/products/tour/block-editor/use-published-products-count';

export const AboutTheEditorMenuItem = ( {
onClose,
}: {
onClose: () => void;
} ) => {
const [ isGuideOpen, setIsGuideOpen ] = useState( false );
const { isNewUser } = usePublishedProductsCount();
return (
<>
<MenuItem
Expand All @@ -34,6 +36,7 @@ export const AboutTheEditorMenuItem = ( {
</MenuItem>
{ isGuideOpen && (
<BlockEditorGuide
isNewUser={ isNewUser }
onCloseGuide={ () => {
setIsGuideOpen( false );
onClose();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/**
* External dependencies
*/
import { useSelect } from '@wordpress/data';
import { useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Pill, TourKit } from '@woocommerce/components';
import { PRODUCTS_STORE_NAME } from '@woocommerce/data';
import { __experimentalUseFeedbackBar as useFeedbackBar } from '@woocommerce/product-editor';
import { recordEvent } from '@woocommerce/tracks';

Expand All @@ -15,35 +13,16 @@ import { recordEvent } from '@woocommerce/tracks';

import './style.scss';
import BlockEditorGuide from './block-editor-guide';
import { usePublishedProductsCount } from './use-published-products-count';

interface Props {
shouldTourBeShown: boolean;
dismissModal: () => void;
}

const PUBLISHED_PRODUCTS_QUERY_PARAMS = {
status: 'publish',
_fields: [ 'id' ],
};

const BlockEditorTour = ( { shouldTourBeShown, dismissModal }: Props ) => {
const { publishedProductsCount, loadingPublishedProductsCount } = useSelect(
( select ) => {
const { getProductsTotalCount, hasFinishedResolution } =
select( PRODUCTS_STORE_NAME );

return {
publishedProductsCount: getProductsTotalCount(
PUBLISHED_PRODUCTS_QUERY_PARAMS,
0
),
loadingPublishedProductsCount: ! hasFinishedResolution(
'getProductsTotalCount',
[ PUBLISHED_PRODUCTS_QUERY_PARAMS, 0 ]
),
};
}
);
const { isNewUser, loadingPublishedProductsCount } =
usePublishedProductsCount();

useEffect( () => {
if ( shouldTourBeShown ) {
Expand All @@ -55,9 +34,6 @@ const BlockEditorTour = ( { shouldTourBeShown, dismissModal }: Props ) => {

const { maybeShowFeedbackBar } = useFeedbackBar();

// we consider a user new if they have no published products
const isNewUser = publishedProductsCount < 1;

const openGuide = () => {
setIsGuideOpen( true );
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* External dependencies
*/
import { PRODUCTS_STORE_NAME } from '@woocommerce/data';
import { useSelect } from '@wordpress/data';

const PUBLISHED_PRODUCTS_QUERY_PARAMS = {
status: 'publish',
_fields: [ 'id' ],
};

export const usePublishedProductsCount = () => {
return useSelect( ( select ) => {
const { getProductsTotalCount, hasFinishedResolution } =
select( PRODUCTS_STORE_NAME );

const publishedProductsCount = getProductsTotalCount(
PUBLISHED_PRODUCTS_QUERY_PARAMS,
0
) as number;

const loadingPublishedProductsCount = ! hasFinishedResolution(
'getProductsTotalCount',
[ PUBLISHED_PRODUCTS_QUERY_PARAMS, 0 ]
);

return {
publishedProductsCount,
loadingPublishedProductsCount,
// we consider a user new if they have no published products
isNewUser: publishedProductsCount < 1,
};
} );
};
5 changes: 5 additions & 0 deletions plugins/woocommerce/changelog/fix-guide-new-user
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fix
Comment: Consider if user is new or not when clicking in "About the editor"


0 comments on commit bfd720c

Please sign in to comment.