import React, { useEffect, useState } from "react"; import { Spinner } from "@patternfly/react-core"; import { asyncStateFetch } from "../../context/auth/AdminClient"; type DataLoaderProps = { loader: () => Promise; deps?: any[]; children: ((arg: Result) => any) | React.ReactNode; }; type Result = { data: T; refresh: () => void; }; export function DataLoader(props: DataLoaderProps) { const [data, setData] = useState<{ result: T } | undefined>(undefined); const [key, setKey] = useState(0); const refresh = () => setKey(new Date().getTime()); useEffect(() => { return asyncStateFetch( () => props.loader(), (result) => setData({ result }) ); }, [key]); if (data) { if (props.children instanceof Function) { return props.children({ data: data.result, refresh: refresh, }); } return props.children; } return (
); }