Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/routes/workbooks/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as taskResultsCrud from '$lib/services/task_results';
import * as workBooksCrud from '$features/workbooks/services/workbooks';

import { Roles } from '$lib/types/user';
import type { TaskGrade, TaskResult } from '$lib/types/task';
import {
WorkBookTab,
type WorkBookTab as WorkBookTabType,
Expand Down Expand Up @@ -58,7 +59,25 @@ export async function load({ locals, url }) {

const selectedGrade = parseWorkBookGrade(params);
const selectedCategory = parseWorkBookCategory(params);
const adminUser = loggedInUser && isAdmin(loggedInUser.role as Roles);

// All tab content is gated by {#if loggedInUser}, so anonymous users render
// nothing. Skip the heavy workbook/category fetches and return a
// display-equivalent empty shape (keys preserved for the page's $effect/$derived).
if (!loggedInUser) {
return {
workbooks: [],
availableCategories: [],
solutionCategoryMap: new Map<number, SolutionCategory>(),
tasksMapByIds: new Map<string, { grade: TaskGrade }>(),
taskResultsByTaskId: new Map<string, TaskResult>(),
loggedInUser: null,
tab,
selectedGrade,
selectedCategory,
};
}

const adminUser = isAdmin(loggedInUser.role as Roles);

try {
const [workbooks, availableCategories, solutionCategoryMap, taskResultsByTaskId] =
Expand All @@ -70,16 +89,13 @@ export async function load({ locals, url }) {
tab === WorkBookTab.SOLUTION && selectedCategory === ALL_SOLUTION_CATEGORIES
? getSolutionCategoryMapByWorkbookId(!!adminUser)
: Promise.resolve(new Map<number, SolutionCategory>()),
loggedInUser
? taskResultsCrud.getTaskResultsOnlyResultExists(loggedInUser.id, true)
: Promise.resolve(new Map()),
taskResultsCrud.getTaskResultsOnlyResultExists(loggedInUser.id, true),
]);

// Grade modes are only displayed on the CURRICULUM tab for logged-in users.
// For other tabs / anonymous, the id list is empty and getTasksWithSelectedTaskIds
// returns [] without a query (see tasks.ts guard), so tasksMapByIds becomes an empty Map.
// Grade modes are only displayed on the CURRICULUM tab.
// For other tabs the id list is empty so tasksMapByIds becomes an empty Map.
const referencedTaskIds =
tab === WorkBookTab.CURRICULUM && loggedInUser ? buildTaskIdsFromWorkbooks(workbooks) : [];
tab === WorkBookTab.CURRICULUM ? buildTaskIdsFromWorkbooks(workbooks) : [];
const referencedTasks = await taskCrud.getTasksWithSelectedTaskIds(referencedTaskIds);
const tasksMapByIds = new Map(
referencedTasks.map((task) => [task.task_id, { grade: task.grade }]),
Expand Down
3 changes: 3 additions & 0 deletions src/routes/workbooks/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
contentClass="bg-white dark:bg-gray-800 mt-0 p-0"
ulClass="flex flex-wrap md:flex-nowrap md:gap-2 rtl:space-x-reverse items-start"
>
<!-- +page.server.ts skips workbook/category fetches for anonymous users on the -->
<!-- assumption that all tab content lives inside this guard. Revisit that early -->
<!-- return before surfacing any content to anonymous users outside this block. -->
{#if loggedInUser}
<WorkbookTabItem
isOpen={data.tab === WorkBookTab.CURRICULUM}
Expand Down
Loading