Skip to content

Commit

Permalink
Reset fetch result when retrying useFetch (#6552)
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya authored Aug 12, 2024
1 parent adf4c6a commit 613ffc8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lms/static/scripts/frontend_apps/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ export function useFetch<T = unknown>(
controller.abort();
setResult(r => ({ ...r, error: null, isLoading: false, data: newValue }));
};
setResult({
data: null,
error: null,
isLoading: key !== null,
mutate,
retry: () => null,
});
const resetResult = () =>
setResult({
data: null,
error: null,
isLoading: key !== null,
mutate,
retry: () => null,
});
resetResult();

if (!key) {
return undefined;
Expand All @@ -87,6 +89,7 @@ export function useFetch<T = unknown>(

const fetcher = lastFetcher.current;
const doFetch = () => {
resetResult();
fetcher(controller.signal)
.then(data => {
if (!controller.signal.aborted) {
Expand Down
1 change: 1 addition & 0 deletions lms/static/scripts/frontend_apps/utils/test/fetch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ describe('useFetch', () => {
fail = false;
retry(wrapper);

assert.equal(getResultText(wrapper), 'Loading');
await waitForFetch(wrapper);
assert.equal(getResultText(wrapper), 'Data: OK');
});
Expand Down

0 comments on commit 613ffc8

Please sign in to comment.