Files
insomnia/packages/insomnia-components/components/button/async-button.js
T
Opender Singh ef2fc3baec Validate protofile during upload (#2864)
* feat(insomnia-components): add async-button

* feat: show error if parsing protofile fails during upload/add

* chore: flow is poop

* feat: use try-finally
2020-11-25 10:02:28 +13:00

50 lines
1.1 KiB
JavaScript

// @flow
import * as React from 'react';
import { Button } from './button';
import type { ButtonProps } from './button';
// Taken from https://github.com/then/is-promise
function isPromise(obj) {
return (
!!obj &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function'
);
}
type AsyncButtonProps = ButtonProps & {
onClick?: (e: SyntheticEvent<HTMLButtonElement>) => Promise<any>,
loadingNode?: React.Node,
};
export const AsyncButton = ({
onClick,
disabled,
loadingNode,
children,
...props
}: AsyncButtonProps) => {
const [loading, setLoading] = React.useState(false);
const asyncHandler = React.useCallback(
async e => {
const result = onClick(e);
if (isPromise(result)) {
try {
setLoading(true);
await result;
} finally {
setLoading(false);
}
}
},
[onClick],
);
return (
<Button {...props} onClick={asyncHandler} disabled={loading || disabled}>
{(loading && loadingNode) || children}
</Button>
);
};