| Server IP : 193.146.211.90 / Your IP : 10.85.6.55 Web Server : Apache System : Linux wordpress-gti-69dcdb54f7-n4wdq 6.11.8-arch1-2 #1 SMP PREEMPT_DYNAMIC Fri, 15 Nov 2024 15:35:07 +0000 x86_64 User : 1001 ( 1001) PHP Version : 8.2.25 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /bitnami/wordpress/wp-content/plugins/presto-player/src/admin/analytics/hocs/ |
Upload File : |
/**
* WordPress dependencies
*/
const { apiFetch } = wp;
const { useState } = wp.element;
const { createHigherOrderComponent } = wp.compose;
/**
* Higher order component factory
*
* @return {Function} The higher order component.
*/
export default () =>
createHigherOrderComponent(
(WrappedComponent) => (props) => {
const [loading, setLoading] = useState(false);
const [stat, setStat] = useState([]);
const [error, setError] = useState("");
// fetch data
// we could also abstract this function to make it reusable
// or do a higher order component
const fetchData = async ({ endpoint, params = {} }) => {
setLoading(true);
let responseData;
try {
responseData = await apiFetch({
path: wp.url.addQueryArgs(endpoint, {
...params,
}),
});
setStat(responseData);
} catch (e) {
console.error(e);
if (e?.message) {
setError(e.message);
}
} finally {
setLoading(false);
}
};
return (
<WrappedComponent
loading={loading}
setLoading={setLoading}
fetchData={fetchData}
stat={stat}
setStat={setStat}
error={error}
setError={setError}
{...props}
/>
);
},
"withStat"
);