AuthorizeRejectButtons: fix onReject never being called

This commit is contained in:
Alex Gleason 2023-03-27 15:36:55 -05:00
parent 9367b16200
commit 3b8f43f02d
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 8 additions and 4 deletions

View File

@ -14,7 +14,11 @@ const AuthorizeRejectButtons: React.FC<IAuthorizeRejectButtons> = ({ onAuthorize
const [state, setState] = useState<'authorizing' | 'rejecting' | 'authorized' | 'rejected' | 'pending'>('pending'); const [state, setState] = useState<'authorizing' | 'rejecting' | 'authorized' | 'rejected' | 'pending'>('pending');
const timeout = useRef<NodeJS.Timeout>(); const timeout = useRef<NodeJS.Timeout>();
function handleAction(present: 'authorizing' | 'rejecting', past: 'authorized' | 'rejected'): void { function handleAction(
present: 'authorizing' | 'rejecting',
past: 'authorized' | 'rejected',
action: () => Promise<unknown> | unknown,
): void {
if (state === present) { if (state === present) {
if (timeout.current) { if (timeout.current) {
clearTimeout(timeout.current); clearTimeout(timeout.current);
@ -24,7 +28,7 @@ const AuthorizeRejectButtons: React.FC<IAuthorizeRejectButtons> = ({ onAuthorize
setState(present); setState(present);
timeout.current = setTimeout(async () => { timeout.current = setTimeout(async () => {
try { try {
await onAuthorize(); await action();
setState(past); setState(past);
} catch (e) { } catch (e) {
console.error(e); console.error(e);
@ -33,8 +37,8 @@ const AuthorizeRejectButtons: React.FC<IAuthorizeRejectButtons> = ({ onAuthorize
} }
} }
const handleAuthorize = async () => handleAction('authorizing', 'authorized'); const handleAuthorize = async () => handleAction('authorizing', 'authorized', onAuthorize);
const handleReject = async () => handleAction('rejecting', 'rejected'); const handleReject = async () => handleAction('rejecting', 'rejected', onReject);
useEffect(() => { useEffect(() => {
return () => { return () => {