2023-03-08 09:51:59 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-03-09 21:11:25 +00:00
|
|
|
import {
|
|
|
|
__experimentalEditor as Editor,
|
2023-05-16 10:41:26 +00:00
|
|
|
__experimentalInitBlocks as initBlocks,
|
2023-10-12 19:59:13 +00:00
|
|
|
__experimentalWooProductMoreMenuItem as WooProductMoreMenuItem,
|
2023-04-13 15:45:50 +00:00
|
|
|
productApiFetchMiddleware,
|
2024-04-24 10:24:09 +00:00
|
|
|
productEditorHeaderApiFetchMiddleware,
|
2023-07-12 20:01:06 +00:00
|
|
|
TRACKS_SOURCE,
|
2023-10-12 14:56:48 +00:00
|
|
|
__experimentalProductMVPCESFooter as FeedbackBar,
|
2024-02-14 14:57:17 +00:00
|
|
|
__experimentalEditorLoadingContext as EditorLoadingContext,
|
2023-03-09 21:11:25 +00:00
|
|
|
} from '@woocommerce/product-editor';
|
2024-04-16 15:15:47 +00:00
|
|
|
import { Spinner } from '@woocommerce/components';
|
2023-07-12 20:01:06 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2024-04-16 15:15:47 +00:00
|
|
|
import React, { lazy, Suspense, useContext, useEffect } from 'react';
|
2023-10-13 20:22:11 +00:00
|
|
|
import { registerPlugin, unregisterPlugin } from '@wordpress/plugins';
|
2023-03-09 21:11:25 +00:00
|
|
|
import { useParams } from 'react-router-dom';
|
2023-10-12 14:56:48 +00:00
|
|
|
import { WooFooterItem } from '@woocommerce/admin-layout';
|
2024-05-24 12:39:53 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2023-03-08 09:51:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-03-22 07:48:55 +00:00
|
|
|
import { useProductEntityRecord } from './hooks/use-product-entity-record';
|
2023-10-12 19:59:13 +00:00
|
|
|
import { MoreMenuFill } from './fills/product-block-editor-fills';
|
|
|
|
import './product-page.scss';
|
2023-03-08 09:51:59 +00:00
|
|
|
|
2024-04-24 10:24:09 +00:00
|
|
|
productEditorHeaderApiFetchMiddleware();
|
2023-04-13 15:45:50 +00:00
|
|
|
productApiFetchMiddleware();
|
|
|
|
|
2024-04-16 15:15:47 +00:00
|
|
|
// Lazy load components
|
|
|
|
const BlockEditorTourWrapper = lazy(
|
|
|
|
() => import( './tour/block-editor/block-editor-tour-wrapper' )
|
|
|
|
);
|
|
|
|
const ProductMVPFeedbackModalContainer = lazy( () =>
|
|
|
|
import( '@woocommerce/product-editor' ).then( ( module ) => ( {
|
|
|
|
default: module.__experimentalProductMVPFeedbackModalContainer,
|
|
|
|
} ) )
|
|
|
|
);
|
|
|
|
|
2023-03-22 07:48:55 +00:00
|
|
|
export default function ProductPage() {
|
2024-05-24 12:39:53 +00:00
|
|
|
const { productId: productIdSearchParam } = useParams();
|
2023-03-22 07:48:55 +00:00
|
|
|
|
2024-07-22 12:22:35 +00:00
|
|
|
/**
|
|
|
|
* Only register blocks and unregister them when the product page is being rendered or unmounted.
|
|
|
|
* Note: Dependency array should stay empty.
|
|
|
|
*/
|
2023-05-16 10:41:26 +00:00
|
|
|
useEffect( () => {
|
2024-04-01 12:50:25 +00:00
|
|
|
document.body.classList.add( 'is-product-editor' );
|
2024-07-22 12:22:35 +00:00
|
|
|
|
|
|
|
const unregisterBlocks = initBlocks();
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
document.body.classList.remove( 'is-product-editor' );
|
|
|
|
unregisterBlocks();
|
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
useEffect( () => {
|
2024-02-14 14:57:17 +00:00
|
|
|
registerPlugin( 'wc-admin-product-editor', {
|
2023-10-13 20:22:11 +00:00
|
|
|
// @ts-expect-error 'scope' does exist. @types/wordpress__plugins is outdated.
|
|
|
|
scope: 'woocommerce-product-block-editor',
|
2024-02-14 14:57:17 +00:00
|
|
|
render: () => {
|
|
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
|
const isEditorLoading = useContext( EditorLoadingContext );
|
|
|
|
|
|
|
|
if ( isEditorLoading ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<WooProductMoreMenuItem>
|
|
|
|
{ ( { onClose }: { onClose: () => void } ) => (
|
|
|
|
<MoreMenuFill onClose={ onClose } />
|
|
|
|
) }
|
|
|
|
</WooProductMoreMenuItem>
|
|
|
|
|
|
|
|
<WooFooterItem>
|
|
|
|
<>
|
|
|
|
<FeedbackBar productType="product" />
|
2024-04-16 15:15:47 +00:00
|
|
|
<Suspense fallback={ <Spinner /> }>
|
|
|
|
<ProductMVPFeedbackModalContainer
|
|
|
|
productId={
|
2024-05-24 12:39:53 +00:00
|
|
|
productIdSearchParam
|
|
|
|
? Number.parseInt(
|
|
|
|
productIdSearchParam,
|
|
|
|
10
|
|
|
|
)
|
2024-04-16 15:15:47 +00:00
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Suspense>
|
2024-02-14 14:57:17 +00:00
|
|
|
</>
|
|
|
|
</WooFooterItem>
|
|
|
|
|
2024-04-16 15:15:47 +00:00
|
|
|
<Suspense fallback={ <Spinner /> }>
|
|
|
|
<BlockEditorTourWrapper />
|
|
|
|
</Suspense>
|
2024-02-14 14:57:17 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
},
|
2023-10-13 20:22:11 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
return () => {
|
2024-05-24 12:39:53 +00:00
|
|
|
unregisterPlugin( 'wc-admin-product-editor' );
|
2023-10-13 20:22:11 +00:00
|
|
|
};
|
2024-05-24 12:39:53 +00:00
|
|
|
}, [ productIdSearchParam ] );
|
2023-05-16 10:41:26 +00:00
|
|
|
|
2023-07-12 20:01:06 +00:00
|
|
|
useEffect(
|
|
|
|
function trackViewEvents() {
|
2024-05-24 12:39:53 +00:00
|
|
|
if ( productIdSearchParam ) {
|
2023-07-12 20:01:06 +00:00
|
|
|
recordEvent( 'product_edit_view', {
|
|
|
|
source: TRACKS_SOURCE,
|
2024-05-24 12:39:53 +00:00
|
|
|
product_id: productIdSearchParam,
|
2023-07-12 20:01:06 +00:00
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
recordEvent( 'product_add_view', {
|
|
|
|
source: TRACKS_SOURCE,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
},
|
2024-05-24 12:39:53 +00:00
|
|
|
[ productIdSearchParam ]
|
2023-07-12 20:01:06 +00:00
|
|
|
);
|
|
|
|
|
2024-05-24 12:39:53 +00:00
|
|
|
const productId = useProductEntityRecord( productIdSearchParam );
|
|
|
|
|
|
|
|
if ( ! productId ) {
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-layout__loading">
|
|
|
|
<Spinner
|
|
|
|
aria-label={ __( 'Creating the product', 'woocommerce' ) }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return <Editor productId={ productId } />;
|
2023-03-08 09:51:59 +00:00
|
|
|
}
|