2023-03-08 09:51:59 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-03-09 21:11:25 +00:00
|
|
|
import {
|
|
|
|
__experimentalEditor as Editor,
|
|
|
|
AUTO_DRAFT_NAME,
|
2023-03-10 20:21:22 +00:00
|
|
|
ProductEditorSettings,
|
2023-03-09 21:11:25 +00:00
|
|
|
} from '@woocommerce/product-editor';
|
2023-03-08 09:51:59 +00:00
|
|
|
import { Product } from '@woocommerce/data';
|
2023-03-09 21:11:25 +00:00
|
|
|
import { useDispatch, useSelect, select as WPSelect } from '@wordpress/data';
|
|
|
|
import { useEffect, useState } from '@wordpress/element';
|
|
|
|
import { Spinner } from '@wordpress/components';
|
|
|
|
import { useParams } from 'react-router-dom';
|
2023-03-08 09:51:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './product-page.scss';
|
|
|
|
|
2023-03-10 20:21:22 +00:00
|
|
|
declare const productBlockEditorSettings: ProductEditorSettings;
|
|
|
|
|
2023-03-09 21:11:25 +00:00
|
|
|
const ProductEditor: React.FC< { product: Product | undefined } > = ( {
|
|
|
|
product,
|
|
|
|
} ) => {
|
|
|
|
if ( ! product ) {
|
|
|
|
return <Spinner />;
|
|
|
|
}
|
|
|
|
|
2023-03-10 20:21:22 +00:00
|
|
|
return (
|
|
|
|
<Editor
|
|
|
|
product={ product }
|
|
|
|
settings={ productBlockEditorSettings || {} }
|
|
|
|
/>
|
|
|
|
);
|
2023-03-09 21:11:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const EditProductEditor: React.FC< { productId: string } > = ( {
|
|
|
|
productId,
|
|
|
|
} ) => {
|
|
|
|
const { product } = useSelect( ( select: typeof WPSelect ) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore Missing types.
|
|
|
|
const { getEditedEntityRecord } = select( 'core' );
|
|
|
|
|
|
|
|
return {
|
|
|
|
product: getEditedEntityRecord( 'postType', 'product', productId ),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
return <ProductEditor product={ product } />;
|
|
|
|
};
|
|
|
|
|
|
|
|
const AddProductEditor = () => {
|
|
|
|
const { saveEntityRecord } = useDispatch( 'core' );
|
|
|
|
const [ product, setProduct ] = useState< Product | undefined >(
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
saveEntityRecord( 'postType', 'product', {
|
|
|
|
title: AUTO_DRAFT_NAME,
|
|
|
|
status: 'auto-draft',
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore Incorrect types.
|
|
|
|
} ).then( ( autoDraftProduct: Product ) => {
|
|
|
|
setProduct( autoDraftProduct );
|
|
|
|
} );
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
return <ProductEditor product={ product } />;
|
|
|
|
};
|
2023-03-08 09:51:59 +00:00
|
|
|
|
|
|
|
export default function ProductPage() {
|
2023-03-09 21:11:25 +00:00
|
|
|
const { productId } = useParams();
|
|
|
|
|
|
|
|
if ( productId ) {
|
|
|
|
return <EditProductEditor productId={ productId } />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <AddProductEditor />;
|
2023-03-08 09:51:59 +00:00
|
|
|
}
|