Add use-create-product-by-type & use-product-types-list-items hooks
This commit is contained in:
parent
5f233898bf
commit
cbfc393c6a
|
@ -17,7 +17,6 @@ const useCreateProductByType = () => {
|
|||
const [ isRequesting, setIsRequesting ] = useState< boolean >( false );
|
||||
|
||||
const createProductByType = async ( type: ProductTypeKey ) => {
|
||||
setIsRequesting( true );
|
||||
if ( type === 'subscription' ) {
|
||||
window.location = getAdminLink(
|
||||
'post-new.php?post_type=product&subscription_pointers=true'
|
||||
|
@ -25,8 +24,11 @@ const useCreateProductByType = () => {
|
|||
return;
|
||||
}
|
||||
|
||||
setIsRequesting( true );
|
||||
try {
|
||||
const data = await createProductFromTemplate(
|
||||
const data: {
|
||||
id?: string;
|
||||
} = await createProductFromTemplate(
|
||||
{
|
||||
template_name: type,
|
||||
status: 'draft',
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { useMemo } from '@wordpress/element';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { getProductTypes } from './utils';
|
||||
import useCreateProductByType from './use-create-product-by-type';
|
||||
|
||||
const useProductTypeListItems = () => {
|
||||
const { createProductByType } = useCreateProductByType();
|
||||
|
||||
const productTypes = useMemo(
|
||||
() =>
|
||||
getProductTypes().map( ( productType ) => ( {
|
||||
...productType,
|
||||
onClick: () => createProductByType( productType.key ),
|
||||
} ) ),
|
||||
[ createProductByType ]
|
||||
);
|
||||
|
||||
return productTypes;
|
||||
};
|
||||
|
||||
export default useProductTypeListItems;
|
Loading…
Reference in New Issue