mirror of
https://github.com/Unleash/unleash.git
synced 2025-10-09 11:14:29 +02:00
38 lines
953 B
TypeScript
38 lines
953 B
TypeScript
import useSWR, { mutate, SWRConfiguration } from 'swr';
|
|
import { useState, useEffect } from 'react';
|
|
import { formatApiPath } from 'utils/formatPath';
|
|
import handleErrorResponses from '../httpErrorResponseHandler';
|
|
|
|
const KEY = `api/admin/invoices`;
|
|
const path = formatApiPath(KEY);
|
|
|
|
const useInvoices = (options: SWRConfiguration = {}) => {
|
|
const fetcher = () => {
|
|
return fetch(path, {
|
|
method: 'GET',
|
|
})
|
|
.then(handleErrorResponses('Invoices'))
|
|
.then(res => res.json());
|
|
};
|
|
|
|
const { data, error } = useSWR(KEY, fetcher, options);
|
|
const [loading, setLoading] = useState(!error && !data);
|
|
|
|
const refetchInvoices = () => {
|
|
mutate(KEY);
|
|
};
|
|
|
|
useEffect(() => {
|
|
setLoading(!error && !data);
|
|
}, [data, error]);
|
|
|
|
return {
|
|
invoices: data?.invoices || [],
|
|
error,
|
|
loading,
|
|
refetchInvoices,
|
|
};
|
|
};
|
|
|
|
export default useInvoices;
|