mirror of
https://github.com/kremalicious/umami.git
synced 2024-12-21 08:43:37 +01:00
47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
import { useState, useEffect } from 'react';
|
|
import { useDispatch } from 'react-redux';
|
|
import { get } from 'lib/web';
|
|
import { updateQuery } from 'redux/actions/queries';
|
|
|
|
export default function useFetch(url, params = {}, options = {}) {
|
|
const dispatch = useDispatch();
|
|
const [data, setData] = useState();
|
|
const [error, setError] = useState();
|
|
const keys = Object.keys(params)
|
|
.sort()
|
|
.map(key => params[key]);
|
|
const { update = [], onDataLoad = () => {} } = options;
|
|
|
|
async function loadData() {
|
|
try {
|
|
setError(null);
|
|
const time = performance.now();
|
|
const data = await get(url, params);
|
|
|
|
dispatch(updateQuery({ url, time: performance.now() - time, completed: Date.now() }));
|
|
|
|
setData(data);
|
|
onDataLoad(data);
|
|
} catch (e) {
|
|
console.error(e);
|
|
setError(e);
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (url) {
|
|
const { interval, delay = 0 } = options;
|
|
|
|
setTimeout(() => loadData(), delay);
|
|
|
|
const id = interval ? setInterval(() => loadData(), interval) : null;
|
|
|
|
return () => {
|
|
clearInterval(id);
|
|
};
|
|
}
|
|
}, [url, ...keys, ...update]);
|
|
|
|
return { data, error, loadData };
|
|
}
|