Skip to content

Commit

Permalink
Fix loaded being set to true before manual refresh had finished
Browse files Browse the repository at this point in the history
  • Loading branch information
PurelyAnecdotal committed Dec 30, 2024
1 parent 407df34 commit 38e7e11
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
20 changes: 9 additions & 11 deletions src/routes/(authed)/grades/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
const currentGradebookState = $derived(getCurrentGradebookState(gradebooksState));
const activeReportingPeriodName = $derived(
gradebooksState.records && gradebooksState.activeIndex
? gradebooksState.records[gradebooksState.activeIndex]?.data?.ReportingPeriod._GradePeriod
: undefined
);
loadGradebooks();
</script>

Expand All @@ -27,21 +33,13 @@
/>
{/if}

{#if gradebooksState.overrideIndex && gradebooksState.records && gradebooksState.activeIndex}
{#if gradebooksState.overrideIndex && gradebooksState.records && gradebooksState.activeIndex && currentGradebookState?.data}
<Alert class="mx-4 flex items-center justify-between" color="light" border>
<span class="text-white">
Viewing reporting period {currentGradebookState?.data?.ReportingPeriod._GradePeriod}
Viewing reporting period {currentGradebookState.data.ReportingPeriod._GradePeriod}
</span>

<Button
onclick={async () => {
gradebooksState.overrideIndex = undefined;
showGradebook();
}}
color="light"
>Return to {gradebooksState.records[gradebooksState.activeIndex]?.data?.ReportingPeriod
._GradePeriod}</Button
>
<Button onclick={() => showGradebook()} color="light">Return to {activeReportingPeriodName}</Button>
</Alert>
{/if}

Expand Down
11 changes: 6 additions & 5 deletions src/routes/(authed)/grades/gradebook.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,20 @@ export const showGradebook = async (overrideIndex?: number, forceRefresh = false
// Set the state of the requested gradebook to loading in preparation for possible cache refresh
gradebooksState.records[index] ??= { loaded: false };

if (gradebooksState.records[index].loaded !== false)
gradebooksState.records[index].loaded = false;
if (gradebooksState.records[index].loaded) gradebooksState.records[index].loaded = false;

// Check if cache is expired
let refresh = true;

if (Date.now() - (gradebooksState.records[index].lastRefresh ?? 0) < cacheExpirationTime) {
if (
!forceRefresh &&
Date.now() - (gradebooksState.records[index].lastRefresh ?? 0) < cacheExpirationTime
) {
gradebooksState.records[index].loaded = true;
refresh = false;
}

// If expired or refreshing manually, refresh
if (refresh || forceRefresh) {
if (refresh) {
const newGradebook = await studentAccount.gradebook(overrideIndex);

gradebooksState.records[index].data = newGradebook;
Expand Down

0 comments on commit 38e7e11

Please sign in to comment.