From 945f3bdceffed7d3bb20ee1849f5f166b3c467bc Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Fri, 25 Sep 2026 20:19:08 +0200 Subject: utils: introduce course local storage helper class --- src/database-prod.ts | 6 +++--- src/student-prod.ts | 15 +++++++++++---- src/utils-mock.js | 10 ++++++++++ src/utils-prod.js | 10 ++++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/database-prod.ts b/src/database-prod.ts index 6712adb..7f50a35 100644 --- a/src/database-prod.ts +++ b/src/database-prod.ts @@ -1,5 +1,5 @@ import { assert } from "./assert.ts"; -import { getCourseId, getSesskey } from "./utils-ENV.js"; +import { getCourseId, CourseLocalStorage, getSesskey } from "./utils-ENV.js"; const DB_TEMPLATE_LIST_HEADER = ` @@ -31,7 +31,7 @@ let graduationDb = findGraduationDb(); const ID_QUERY_PARAM_REGEX = /[?&]id=(\d+)/; async function findGraduationDb() { - let graduationDbId = localStorage.getItem(`gruaduationDbId-${getCourseId()}`); + let graduationDbId = CourseLocalStorage.getItem("bycs-sol-gruaduationDbId"); if (graduationDbId !== null) { return new DB(Number(graduationDbId)); } @@ -46,7 +46,7 @@ async function findGraduationDb() { const match = anchor.href.match(ID_QUERY_PARAM_REGEX); if (match === null) continue; const dbId = match[1]; - localStorage.setItem("graduationDbId", dbId); + CourseLocalStorage.setItem("bycs-sol-graduationDbId", dbId); return new DB(Number(dbId)); } } diff --git a/src/student-prod.ts b/src/student-prod.ts index 1addf32..ea258c7 100644 --- a/src/student-prod.ts +++ b/src/student-prod.ts @@ -1,6 +1,11 @@ import type { Graduation } from "./graduation-common.ts"; import { Student } from "./student-common.ts"; -import { getCourseId, getUserIconUrl, getCourseParticipantsUrl } from "./utils-prod.js"; +import { + getCourseId, + CourseLocalStorage, + getUserIconUrl, + getCourseParticipantsUrl, +} from "./utils-prod.js"; import { fetchGraduation } from "./graduation-prod.ts"; declare module "./student-common.ts" { @@ -58,16 +63,18 @@ async function _fetchStudents() { async function fetchStudents(idFilter?: number, nameFilter?: string) { if (students === null) { const studentsPromise = _fetchStudents(); - const storedStudents = localStorage.getItem(`students-${getCourseId()}`); + const storedStudents = CourseLocalStorage.getItem(`students-${getCourseId()}`); if (storedStudents !== null && storedStudents.length > 0) { - studentsPromise.then((fetched) => localStorage.setItem("students", JSON.stringify(fetched))); + studentsPromise.then((fetched) => + CourseLocalStorage.setItem("students", JSON.stringify(fetched)), + ); students = []; for (const o of JSON.parse(storedStudents)) { students.push(new Student(o.id, o.name)); } } else { students = await studentsPromise; - localStorage.setItem("students", JSON.stringify(students)); + CourseLocalStorage.setItem("students", JSON.stringify(students)); } } diff --git a/src/utils-mock.js b/src/utils-mock.js index 5457700..563f685 100644 --- a/src/utils-mock.js +++ b/src/utils-mock.js @@ -59,3 +59,13 @@ export function getCourseParticipantsUrl() { console.error("Try to get mocked participations URL"); return ""; } + +export class CourseLocalStorage { + static getItem(key) { + return localStorage.getItem(`${key}-mock`); + } + + static setItem(key, val) { + return localStorage.setItem(`${key}-mock`, val); + } +} diff --git a/src/utils-prod.js b/src/utils-prod.js index bc5868f..b53f535 100644 --- a/src/utils-prod.js +++ b/src/utils-prod.js @@ -62,3 +62,13 @@ export function getSesskey() { export function getCourseParticipantsUrl() { return `https://lernplattform.bycs.de/user/index.php?id=${getCourseId()}&perpage=5000&tifirst&tilast`; } + +export class CourseLocalStorage { + static getItem(key) { + return localStorage.getItem(`${key}-${getCourseId()}`); + } + + static setItem(key, val) { + return localStorage.setItem(`${key}-${getCourseId()}`, val); + } +} -- cgit v1.2.3