Add use-create-product-by-type & use-product-types-list-items hooks

This commit is contained in:
Chi-Hsuan Huang 2022-04-27 15:20:53 +08:00
parent 5f233898bf
commit cbfc393c6a
2 changed files with 31 additions and 2 deletions

View File

@ -17,7 +17,6 @@ const useCreateProductByType = () => {
const [ isRequesting, setIsRequesting ] = useState< boolean >( false ); const [ isRequesting, setIsRequesting ] = useState< boolean >( false );
const createProductByType = async ( type: ProductTypeKey ) => { const createProductByType = async ( type: ProductTypeKey ) => {
setIsRequesting( true );
if ( type === 'subscription' ) { if ( type === 'subscription' ) {
window.location = getAdminLink( window.location = getAdminLink(
'post-new.php?post_type=product&subscription_pointers=true' 'post-new.php?post_type=product&subscription_pointers=true'
@ -25,8 +24,11 @@ const useCreateProductByType = () => {
return; return;
} }
setIsRequesting( true );
try { try {
const data = await createProductFromTemplate( const data: {
id?: string;
} = await createProductFromTemplate(
{ {
template_name: type, template_name: type,
status: 'draft', status: 'draft',

View File

@ -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;