2023-05-31 22:21:33 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
|
|
|
|
const API_NAMESPACE = 'woocommerce-docs/v1';
|
|
|
|
|
|
|
|
export const useManifests = () => {
|
|
|
|
const [ manifests, setManifests ] = useState< string[] >( [] );
|
|
|
|
const [ loading, setLoading ] = useState< boolean >( true );
|
2023-06-29 04:47:22 +00:00
|
|
|
const [ error, setError ] = useState< string | null >( null );
|
2023-05-31 22:21:33 +00:00
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
const getManifests = async () => {
|
2023-06-29 04:47:22 +00:00
|
|
|
try {
|
|
|
|
const res = await apiFetch< string[] >( {
|
|
|
|
path: `${ API_NAMESPACE }/manifests`,
|
|
|
|
method: 'GET',
|
|
|
|
} );
|
|
|
|
|
|
|
|
setManifests( res );
|
|
|
|
setLoading( false );
|
|
|
|
} catch ( err: unknown ) {
|
|
|
|
if (
|
|
|
|
err &&
|
|
|
|
typeof err === 'object' &&
|
|
|
|
'message' in err &&
|
|
|
|
typeof err.message === 'string'
|
|
|
|
) {
|
|
|
|
setError( `Error occurred: ${ err.message }` );
|
|
|
|
setLoading( false );
|
|
|
|
} else {
|
|
|
|
setError( 'An unknown error occurred.' );
|
|
|
|
setLoading( false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
getManifests();
|
|
|
|
}, [] );
|
|
|
|
|
|
|
|
const deleteManifest = async ( manifest: string ) => {
|
|
|
|
setLoading( true );
|
|
|
|
|
|
|
|
try {
|
2023-05-31 22:21:33 +00:00
|
|
|
const res = await apiFetch< string[] >( {
|
|
|
|
path: `${ API_NAMESPACE }/manifests`,
|
2023-06-29 04:47:22 +00:00
|
|
|
method: 'DELETE',
|
|
|
|
data: { manifest },
|
2023-05-31 22:21:33 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
setManifests( res );
|
|
|
|
setLoading( false );
|
2023-06-29 04:47:22 +00:00
|
|
|
} catch ( err: unknown ) {
|
|
|
|
if (
|
|
|
|
err &&
|
|
|
|
typeof err === 'object' &&
|
|
|
|
'message' in err &&
|
|
|
|
typeof err.message === 'string'
|
|
|
|
) {
|
|
|
|
setError( `Error occurred: ${ err.message }` );
|
|
|
|
setLoading( false );
|
|
|
|
} else {
|
|
|
|
setError( 'An unknown error occurred.' );
|
|
|
|
setLoading( false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-05-31 22:21:33 +00:00
|
|
|
|
2023-06-29 04:47:22 +00:00
|
|
|
const createManifest = async ( manifest: string ) => {
|
|
|
|
setLoading( true );
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await apiFetch< string[] >( {
|
|
|
|
path: `${ API_NAMESPACE }/manifests`,
|
|
|
|
method: 'POST',
|
|
|
|
data: { manifest },
|
|
|
|
} );
|
|
|
|
|
|
|
|
setManifests( res );
|
|
|
|
setLoading( false );
|
|
|
|
} catch ( err: unknown ) {
|
|
|
|
if (
|
|
|
|
err &&
|
|
|
|
typeof err === 'object' &&
|
|
|
|
'message' in err &&
|
|
|
|
typeof err.message === 'string'
|
|
|
|
) {
|
|
|
|
setError( `Error occurred: ${ err.message }` );
|
|
|
|
setLoading( false );
|
|
|
|
} else {
|
|
|
|
setError( 'An unknown error occurred.' );
|
|
|
|
setLoading( false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-05-31 22:21:33 +00:00
|
|
|
|
2023-06-29 04:47:22 +00:00
|
|
|
return {
|
|
|
|
manifests,
|
|
|
|
error,
|
|
|
|
isLoading: loading,
|
|
|
|
createManifest,
|
|
|
|
deleteManifest,
|
|
|
|
};
|
2023-05-31 22:21:33 +00:00
|
|
|
};
|