2022-08-11 14:04:14 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
import { useEffect, useRef } from '@wordpress/element';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { Form, Spinner, FormRef } from '@woocommerce/components';
|
|
|
|
import {
|
|
|
|
PartialProduct,
|
|
|
|
Product,
|
|
|
|
PRODUCTS_STORE_NAME,
|
|
|
|
WCDataSelector,
|
|
|
|
} from '@woocommerce/data';
|
|
|
|
import { useParams } from 'react-router-dom';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { ProductFormLayout } from './layout/product-form-layout';
|
|
|
|
import { ProductFormActions } from './product-form-actions';
|
|
|
|
import { ProductDetailsSection } from './sections/product-details-section';
|
2022-10-12 20:11:05 +00:00
|
|
|
import { ProductInventorySection } from './sections/product-inventory-section';
|
2022-08-30 13:26:24 +00:00
|
|
|
import { PricingSection } from './sections/pricing-section';
|
2022-09-23 12:54:52 +00:00
|
|
|
import { ProductShippingSection } from './sections/product-shipping-section';
|
2022-10-12 18:16:22 +00:00
|
|
|
import { ImagesSection } from './sections/images-section';
|
2022-08-11 14:04:14 +00:00
|
|
|
import './product-page.scss';
|
2022-08-30 13:26:24 +00:00
|
|
|
import { validate } from './product-validation';
|
2022-09-26 17:44:06 +00:00
|
|
|
import { AttributesSection } from './sections/attributes-section';
|
2022-08-11 14:04:14 +00:00
|
|
|
|
|
|
|
const EditProductPage: React.FC = () => {
|
|
|
|
const { productId } = useParams();
|
|
|
|
const previousProductRef = useRef< PartialProduct >();
|
|
|
|
const formRef = useRef< FormRef< Partial< Product > > >( null );
|
|
|
|
const { product, isLoading, isPendingAction } = useSelect(
|
|
|
|
( select: WCDataSelector ) => {
|
2022-08-19 15:05:13 +00:00
|
|
|
const {
|
|
|
|
getProduct,
|
|
|
|
hasFinishedResolution,
|
|
|
|
isPending,
|
|
|
|
getPermalinkParts,
|
|
|
|
} = select( PRODUCTS_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,
|
|
|
|
isLoading:
|
|
|
|
! hasFinishedResolution( 'getProduct', [
|
|
|
|
parseInt( productId, 10 ),
|
|
|
|
] ) ||
|
|
|
|
! hasFinishedResolution( 'getPermalinkParts', [
|
|
|
|
parseInt( productId, 10 ),
|
|
|
|
] ),
|
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( () => {
|
|
|
|
// used for determening the wasDeletedUsingAction condition.
|
|
|
|
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 && (
|
|
|
|
<ProductFormLayout>
|
|
|
|
<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>
|
|
|
|
) }
|
|
|
|
{ product &&
|
|
|
|
( product.status !== 'trash' || wasDeletedUsingAction ) && (
|
|
|
|
<Form< Partial< Product > >
|
|
|
|
ref={ formRef }
|
|
|
|
initialValues={ product || {} }
|
2022-08-30 13:26:24 +00:00
|
|
|
validate={ validate }
|
2022-08-11 14:04:14 +00:00
|
|
|
errors={ {} }
|
|
|
|
>
|
|
|
|
<ProductFormLayout>
|
|
|
|
<ProductDetailsSection />
|
2022-08-30 13:26:24 +00:00
|
|
|
<PricingSection />
|
2022-10-12 18:16:22 +00:00
|
|
|
<ImagesSection />
|
2022-10-12 20:11:05 +00:00
|
|
|
<ProductInventorySection />
|
2022-10-11 15:00:35 +00:00
|
|
|
<ProductShippingSection product={ product } />
|
2022-09-29 17:54:04 +00:00
|
|
|
<AttributesSection />
|
2022-08-11 14:04:14 +00:00
|
|
|
<ProductFormActions />
|
|
|
|
</ProductFormLayout>
|
|
|
|
</Form>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EditProductPage;
|