From c58802a402374e353deb5652470818395eaa3c65 Mon Sep 17 00:00:00 2001 From: Nathan Schneider Date: Fri, 14 Jun 2024 15:32:58 -0300 Subject: [PATCH] Include simple product template directly in the request of product form --- .../edit.tsx | 12 +++++++++- .../components/block-editor/block-editor.tsx | 23 +++++++------------ plugins/woocommerce/package.json | 1 - .../ProductFormsController.php | 22 ++++++++++++++++++ 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/packages/js/product-editor/src/blocks/product-fields/product-details-section-description/edit.tsx b/packages/js/product-editor/src/blocks/product-fields/product-details-section-description/edit.tsx index 3c2aa01acdb..553a631e3ce 100644 --- a/packages/js/product-editor/src/blocks/product-fields/product-details-section-description/edit.tsx +++ b/packages/js/product-editor/src/blocks/product-fields/product-details-section-description/edit.tsx @@ -84,6 +84,12 @@ export function ProductDetailsSectionDescriptionBlockEdit( { 'status' ); + const [ , setSelectedProductFormId ] = useEntityProp< number >( + 'postType', + 'product', + '__provisorySelectedProductFormId' + ); + const { validate } = useValidations< Product >(); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore @@ -388,7 +394,11 @@ export function ProductDetailsSectionDescriptionBlockEdit( { icon={ resolveIcon( 'external' ) } info={ formPost.excerpt.raw } iconPosition="left" - onClick={ onClose } // close the dropdown for now + onClick={ () => + setSelectedProductFormId( + formPost.id + ) + } > { formPost.title.rendered } diff --git a/packages/js/product-editor/src/components/block-editor/block-editor.tsx b/packages/js/product-editor/src/components/block-editor/block-editor.tsx index 90d3d4981cb..83cacc407af 100644 --- a/packages/js/product-editor/src/components/block-editor/block-editor.tsx +++ b/packages/js/product-editor/src/components/block-editor/block-editor.tsx @@ -35,6 +35,7 @@ import { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore store should be included. useEntityBlockEditor, + useEntityProp, // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore store should be included. useEntityRecord, @@ -87,10 +88,6 @@ export function BlockEditor( { productId, setIsEditorLoading, }: BlockEditorProps ) { - const [ selectedProductFormId, setSelectedProductFormId ] = useState< - number | null - >( null ); - useConfirmUnsavedProductChanges( postType ); /** @@ -214,6 +211,12 @@ export function BlockEditor( { { id: productId !== -1 ? productId : 0 } ); + const [ selectedProductFormId ] = useEntityProp( + 'postType', + postType, + '__provisorySelectedProductFormId' + ); + // Pull the product templates from the store. const productForms = useSelect( ( sel ) => { return ( @@ -223,15 +226,6 @@ export function BlockEditor( { ); }, [] ) as ProductFormPostProps[]; - // Set the default product form template ID. - useEffect( () => { - if ( ! productForms.length ) { - return; - } - - setSelectedProductFormId( productForms[ 0 ].id ); - }, [ productForms ] ); - const isEditorLoading = ! settings || ! layoutTemplate || @@ -261,7 +255,6 @@ export function BlockEditor( { }, [ productForms, selectedProductFormId ] ); - useLayoutEffect( function setupEditor() { if ( isEditorLoading ) { @@ -277,7 +270,7 @@ export function BlockEditor( { * If the product form template is not available, use the block instances. * ToDo: Remove this fallback once the product form template is stable/available. */ - const editorTemplate = blockInstances ?? productFormTemplate; + const editorTemplate = productFormTemplate || blockInstances; onChange( editorTemplate, {} ); diff --git a/plugins/woocommerce/package.json b/plugins/woocommerce/package.json index eed928074ea..18c097b069b 100644 --- a/plugins/woocommerce/package.json +++ b/plugins/woocommerce/package.json @@ -695,7 +695,6 @@ "node_modules/@woocommerce/e2e-core-tests/CHANGELOG.md", "node_modules/@woocommerce/api/dist/", "node_modules/@woocommerce/admin-e2e-tests/build", - "node_modules/@woocommerce/classic-assets/build", "node_modules/@woocommerce/block-library/build", "node_modules/@woocommerce/block-library/blocks.ini", "node_modules/@woocommerce/admin-library/build", diff --git a/plugins/woocommerce/src/Admin/Features/ProductBlockEditor/ProductFormsController.php b/plugins/woocommerce/src/Admin/Features/ProductBlockEditor/ProductFormsController.php index edf75069486..26cec365c0d 100644 --- a/plugins/woocommerce/src/Admin/Features/ProductBlockEditor/ProductFormsController.php +++ b/plugins/woocommerce/src/Admin/Features/ProductBlockEditor/ProductFormsController.php @@ -5,6 +5,8 @@ namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor; +use Automattic\WooCommerce\Internal\Features\ProductBlockEditor\ProductTemplates\SimpleProductTemplate; + /** * Handle retrieval of product forms. */ @@ -24,6 +26,26 @@ class ProductFormsController { */ public function init() { // phpcs:ignore WooCommerce.Functions.InternalInjectionMethod.MissingFinal, WooCommerce.Functions.InternalInjectionMethod.MissingInternalTag -- Not an injection. add_action( 'upgrader_process_complete', array( $this, 'migrate_templates_when_plugin_updated' ), 10, 2 ); + add_action( 'rest_post_dispatch', array( $this, 'maybe_add_product_form_templates' ), 10, 3 ); + } + + /** + * Maybe add product form templates to the posts array. + */ + public function maybe_add_product_form_templates( $response, $server, $request ) { + if ( $request->get_route() === '/wp/v2/product_form' ) { + $test = new SimpleProductTemplate(); + $response->data[] = array( + 'content' => array( 'raw' => $test->get_comment_delimited_template() ), + 'id' => 9999, + 'title' => array( + 'raw' => 'simple_test', + 'rendered' => 'simple_test', + ), + 'excerpt' => array( 'ray' => 'simple_test' ), + ); + } + return $response; } /**