Skip to content

Commit

Permalink
Remove inaccurate error return (#1244)
Browse files Browse the repository at this point in the history
This no-data code path was hit incorrectly and would produce false errors when data was loading. I debugged this a bit and didn't get to the bottom of this path, but I believe it's due to the asynchronous request within the hook and some sort of in-between state where the request should be sent, but the return path with !data was hit first preventing further execution.
  • Loading branch information
djfarrelly committed Mar 21, 2024
1 parent 4686547 commit b95f4ed
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions ui/apps/dashboard/src/utils/useRestAPIRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { useEffect, useState } from 'react';
import { useAuth } from '@clerk/nextjs';
import {
baseFetchSkipped,
baseFetchSucceeded,
baseInitialFetchFailed,
baseInitialFetchLoading,
Expand All @@ -16,8 +17,8 @@ export function useRestAPIRequest<T>({
}: {
url: string | URL | null;
method: string;
pause: boolean;
}): FetchResult<T> {
pause?: boolean;
}): FetchResult<T, { skippable: true }> {
const { getToken } = useAuth();
const [data, setData] = useState<any>();
const [isLoading, setIsLoading] = useState<boolean>(false);
Expand All @@ -26,10 +27,12 @@ export function useRestAPIRequest<T>({
useEffect(() => {
async function request() {
if (!url || pause) return;
const sessionToken = await getToken();
if (!sessionToken) return; // TODO - Handle no auth

setIsLoading(true);
const sessionToken = await getToken();
if (!sessionToken) {
setIsLoading(false);
return; // TODO - Handle no auth
}
const response = await fetch(url, {
method,
headers: {
Expand Down Expand Up @@ -63,11 +66,9 @@ export function useRestAPIRequest<T>({
};
}

if (!data) {
// Should be unreachable.
if (!!pause) {
return {
...baseInitialFetchFailed,
error: new Error('finished loading but missing data'),
...baseFetchSkipped,
};
}

Expand Down

0 comments on commit b95f4ed

Please sign in to comment.