2022-08-11 14:04:14 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import {
|
2023-01-19 08:52:02 +00:00
|
|
|
EXPERIMENTAL_PRODUCT_FORM_STORE_NAME,
|
2022-12-16 14:20:10 +00:00
|
|
|
EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME,
|
2022-08-11 14:04:14 +00:00
|
|
|
PartialProduct,
|
|
|
|
Product,
|
|
|
|
PRODUCTS_STORE_NAME,
|
|
|
|
WCDataSelector,
|
|
|
|
} from '@woocommerce/data';
|
2022-12-16 14:20:10 +00:00
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
import { useEffect, useRef } from '@wordpress/element';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { Spinner, FormRef } from '@woocommerce/components';
|
2022-08-11 14:04:14 +00:00
|
|
|
import { useParams } from 'react-router-dom';
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-12-05 20:46:21 +00:00
|
|
|
import { ProductForm } from './product-form';
|
2022-12-12 13:43:34 +00:00
|
|
|
import { ProductFormLayout } from './layout/product-form-layout';
|
2022-12-16 14:20:10 +00:00
|
|
|
import { ProductVariationForm } from './product-variation-form';
|
2023-05-18 08:25:36 +00:00
|
|
|
import './add-edit-product-page.scss';
|
2022-08-11 14:04:14 +00:00
|
|
|
|
|
|
|
const EditProductPage: React.FC = () => {
|
2022-12-16 14:20:10 +00:00
|
|
|
const { productId, variationId } = useParams();
|
|
|
|
const isProductVariation = !! variationId;
|
2022-08-11 14:04:14 +00:00
|
|
|
const previousProductRef = useRef< PartialProduct >();
|
|
|
|
const formRef = useRef< FormRef< Partial< Product > > >( null );
|
2022-12-16 14:20:10 +00:00
|
|
|
const { product, isLoading, isPendingAction, productVariation } = useSelect(
|
2022-08-11 14:04:14 +00:00
|
|
|
( select: WCDataSelector ) => {
|
2022-08-19 15:05:13 +00:00
|
|
|
const {
|
|
|
|
getProduct,
|
2022-12-16 14:20:10 +00:00
|
|
|
hasFinishedResolution: hasProductFinishedResolution,
|
2022-08-19 15:05:13 +00:00
|
|
|
isPending,
|
|
|
|
getPermalinkParts,
|
|
|
|
} = select( PRODUCTS_STORE_NAME );
|
2023-01-19 08:52:02 +00:00
|
|
|
const { hasFinishedResolution: hasProductFormFinishedResolution } =
|
|
|
|
select( EXPERIMENTAL_PRODUCT_FORM_STORE_NAME );
|
2022-12-16 14:20:10 +00:00
|
|
|
const {
|
|
|
|
getProductVariation,
|
|
|
|
hasFinishedResolution: hasProductVariationFinishedResolution,
|
|
|
|
} = select( EXPERIMENTAL_PRODUCT_VARIATIONS_STORE_NAME );
|
2022-08-11 14:04:14 +00:00
|
|
|
if ( productId ) {
|
2022-08-19 15:05:13 +00:00
|
|
|
const retrievedProduct = getProduct(
|
|
|
|
parseInt( productId, 10 ),
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
const permalinkParts = getPermalinkParts(
|
|
|
|
parseInt( productId, 10 )
|
|
|
|
);
|
2022-08-11 14:04:14 +00:00
|
|
|
return {
|
2022-08-19 15:05:13 +00:00
|
|
|
product:
|
|
|
|
permalinkParts && retrievedProduct
|
|
|
|
? retrievedProduct
|
|
|
|
: undefined,
|
2022-12-16 14:20:10 +00:00
|
|
|
productVariation:
|
|
|
|
isProductVariation &&
|
|
|
|
getProductVariation( {
|
|
|
|
id: parseInt( variationId, 10 ),
|
2022-12-22 17:23:44 +00:00
|
|
|
product_id: parseInt( productId, 10 ),
|
2022-12-16 14:20:10 +00:00
|
|
|
} ),
|
2022-08-19 15:05:13 +00:00
|
|
|
isLoading:
|
2022-12-16 14:20:10 +00:00
|
|
|
! hasProductFinishedResolution( 'getProduct', [
|
2022-08-19 15:05:13 +00:00
|
|
|
parseInt( productId, 10 ),
|
|
|
|
] ) ||
|
2022-12-16 14:20:10 +00:00
|
|
|
! hasProductFinishedResolution( 'getPermalinkParts', [
|
2022-08-19 15:05:13 +00:00
|
|
|
parseInt( productId, 10 ),
|
2022-12-16 14:20:10 +00:00
|
|
|
] ) ||
|
|
|
|
! (
|
|
|
|
isProductVariation &&
|
|
|
|
hasProductVariationFinishedResolution(
|
|
|
|
'getProductVariation',
|
|
|
|
[ parseInt( variationId, 10 ) ]
|
|
|
|
)
|
2023-01-19 08:52:02 +00:00
|
|
|
) ||
|
|
|
|
! hasProductFormFinishedResolution( 'getProductForm' ),
|
2022-08-11 14:04:14 +00:00
|
|
|
isPendingAction:
|
|
|
|
isPending( 'createProduct' ) ||
|
|
|
|
isPending(
|
|
|
|
'deleteProduct',
|
|
|
|
parseInt( productId, 10 )
|
|
|
|
) ||
|
|
|
|
isPending( 'updateProduct', parseInt( productId, 10 ) ),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
isLoading: false,
|
|
|
|
isPendingAction: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect( () => {
|
2023-05-08 07:55:09 +00:00
|
|
|
// used for determining the wasDeletedUsingAction condition.
|
2022-08-11 14:04:14 +00:00
|
|
|
if (
|
|
|
|
previousProductRef.current &&
|
|
|
|
product &&
|
|
|
|
previousProductRef.current.id !== product.id &&
|
|
|
|
formRef.current
|
|
|
|
) {
|
|
|
|
formRef.current.resetForm( product );
|
|
|
|
}
|
|
|
|
previousProductRef.current = product;
|
|
|
|
}, [ product ] );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
recordEvent( 'view_new_product_management_experience' );
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
const wasDeletedUsingAction =
|
|
|
|
previousProductRef.current?.id === product?.id &&
|
|
|
|
previousProductRef.current?.status !== 'trash' &&
|
|
|
|
product?.status === 'trash';
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="woocommerce-edit-product">
|
|
|
|
{ isLoading && ! product ? (
|
|
|
|
<div className="woocommerce-edit-product__spinner">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
) : null }
|
|
|
|
{ product &&
|
|
|
|
product.status === 'trash' &&
|
|
|
|
! isPendingAction &&
|
|
|
|
! wasDeletedUsingAction && (
|
2023-01-24 23:37:21 +00:00
|
|
|
<ProductFormLayout id="error">
|
2022-08-11 14:04:14 +00:00
|
|
|
<div className="woocommerce-edit-product__error">
|
|
|
|
{ __(
|
|
|
|
'You cannot edit this item because it is in the Trash. Please restore it and try again.',
|
|
|
|
'woocommerce'
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
</ProductFormLayout>
|
|
|
|
) }
|
2024-05-13 18:05:47 +00:00
|
|
|
{ productVariation && product && (
|
|
|
|
<ProductVariationForm
|
|
|
|
product={ product }
|
|
|
|
productVariation={ productVariation }
|
|
|
|
/>
|
|
|
|
) }
|
2022-12-16 14:20:10 +00:00
|
|
|
{ ! isProductVariation &&
|
|
|
|
product &&
|
2022-08-11 14:04:14 +00:00
|
|
|
( product.status !== 'trash' || wasDeletedUsingAction ) && (
|
2022-12-05 20:46:21 +00:00
|
|
|
<ProductForm formRef={ formRef } product={ product } />
|
2022-08-11 14:04:14 +00:00
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EditProductPage;
|