36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import React from "react";
|
|
import AsyncState from "../../shared/enumerations/AsyncState";
|
|
import IAsyncStateContainer from "../../shared/types/IAsyncStateContainer";
|
|
|
|
export interface IAsyncProps {
|
|
container: IAsyncStateContainer<any>;
|
|
children: JSX.Element;
|
|
error?: JSX.Element;
|
|
loading?: JSX.Element;
|
|
notFound?: JSX.Element;
|
|
inactive?: JSX.Element;
|
|
}
|
|
|
|
const AsyncGuard: React.FC<IAsyncProps> = (props) => {
|
|
switch (props.container.state) {
|
|
case AsyncState.Inactive:
|
|
default:
|
|
return props.inactive ? props.inactive : <></>;
|
|
case AsyncState.Loading:
|
|
return props.loading ? props.loading : <>Loading...</>;
|
|
case AsyncState.Invalid:
|
|
case AsyncState.Failure:
|
|
return props.error ? props.error : <>Data failed to load</>;
|
|
case AsyncState.NotFound:
|
|
return props.notFound ? props.notFound : <>Data was not found</>;
|
|
case AsyncState.Success:
|
|
return props.container.data ? (
|
|
props.children
|
|
) : (
|
|
<>No Data Returned</>
|
|
);
|
|
}
|
|
};
|
|
|
|
export default AsyncGuard;
|