Include simple product template directly in the request of product form

This commit is contained in:
Nathan Schneider 2024-06-14 15:32:58 -03:00
parent 91669cfe52
commit c58802a402
4 changed files with 41 additions and 17 deletions

View File

@ -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 }
</MenuItem>

View File

@ -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, {} );

View File

@ -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",

View File

@ -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;
}
/**