keycloak-scim/src/components/data-loader/DataLoader.tsx

33 lines
737 B
TypeScript
Raw Normal View History

import React, { DependencyList, useState } from "react";
import { Spinner } from "@patternfly/react-core";
import { useFetch } from "../../context/auth/AdminClient";
type DataLoaderProps<T> = {
loader: () => Promise<T>;
deps?: DependencyList;
children: ((arg: T) => any) | React.ReactNode;
};
export function DataLoader<T>(props: DataLoaderProps<T>) {
const [data, setData] = useState<T | undefined>();
useFetch(
() => props.loader(),
(result) => setData(result),
props.deps || []
);
2020-08-21 00:09:05 +00:00
if (data) {
if (props.children instanceof Function) {
return props.children(data);
}
return props.children;
}
return (
<div className="pf-u-text-align-center">
<Spinner />
</div>
);
}