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';
|
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() {
|
|
|
|
const { productId } = useParams();
|
|
|
|
|
|
|
|
const product = useProductEntityRecord( productId );
|
|
|
|
|
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-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={
|
|
|
|
productId
|
|
|
|
? parseInt( productId, 10 )
|
|
|
|
: 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
|
|
|
} );
|
|
|
|
|
|
|
|
const unregisterBlocks = initBlocks();
|
|
|
|
|
|
|
|
return () => {
|
2024-04-01 12:50:25 +00:00
|
|
|
document.body.classList.remove( 'is-product-editor' );
|
2023-10-13 20:22:11 +00:00
|
|
|
unregisterPlugin( 'wc-admin-more-menu' );
|
|
|
|
unregisterBlocks();
|
|
|
|
};
|
2024-02-14 14:57:17 +00:00
|
|
|
}, [ productId ] );
|
2023-05-16 10:41:26 +00:00
|
|
|
|
2023-07-12 20:01:06 +00:00
|
|
|
useEffect(
|
|
|
|
function trackViewEvents() {
|
|
|
|
if ( productId ) {
|
|
|
|
recordEvent( 'product_edit_view', {
|
|
|
|
source: TRACKS_SOURCE,
|
|
|
|
product_id: productId,
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
recordEvent( 'product_add_view', {
|
|
|
|
source: TRACKS_SOURCE,
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[ productId ]
|
|
|
|
);
|
|
|
|
|
2023-03-10 20:21:22 +00:00
|
|
|
return (
|
2023-06-06 15:06:26 +00:00
|
|
|
<>
|
2024-03-14 17:02:40 +00:00
|
|
|
<Editor product={ product } />
|
2023-06-06 15:06:26 +00:00
|
|
|
</>
|
2023-03-10 20:21:22 +00:00
|
|
|
);
|
2023-03-08 09:51:59 +00:00
|
|
|
}
|