diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2026-07-19 11:55:51 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2026-07-19 12:07:12 +0200 |
| commit | 369fbaa84bdb86d91752c53824bb3b13c45b483f (patch) | |
| tree | 5c1dca9eb4581a1de8c0862ba776528a4bedcf41 /js/student-common.js | |
| parent | dd52c913c454b9a6cf9ab7e7b102f9e7ede31754 (diff) | |
| download | bycs-sol-369fbaa84bdb86d91752c53824bb3b13c45b483f.tar.gz bycs-sol-369fbaa84bdb86d91752c53824bb3b13c45b483f.zip | |
misc commit
Included changes:
* Cache the fetched students
* Add level icons
* Improve mass-graduation interface
* Move utility functions from config.js to utils-ENV.js
Diffstat (limited to 'js/student-common.js')
| -rw-r--r-- | js/student-common.js | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/js/student-common.js b/js/student-common.js index 778ff8d..2dfa35d 100644 --- a/js/student-common.js +++ b/js/student-common.js @@ -1,4 +1,4 @@ -import { getCourseId, getUserIconUrl, getCourseParticipantsUrl } from "./config.js"; +import { getCourseId, getUserIconUrl, getCourseParticipantsUrl } from "./utils-ENV.js"; import { fetchGraduation } from "./graduation-common.js"; /** * Class to represent Students. @@ -49,23 +49,15 @@ class Student { } } -async function fetchStudent(idFilter, nameFilter, groupFilter) { - const students = await fetchStudents(idFilter, nameFilter, groupFilter); - return students[0]; -} - // Cache the fetched students. let students = []; const idRegex = /id=(\d+)/; -async function fetchStudents(idFilter, nameFilter, groupFilter) { - if (students.size) { - return students; - } - +async function _fetchStudents() { const res = await fetch(getCourseParticipantsUrl()); const doc = new DOMParser().parseFromString(await res.text(), "text/html"); const table = doc.querySelector("tbody"); + let fetchedStudents = []; table.querySelectorAll("tr").forEach(row => { if (row.classList.contains("emptyrow")) return; @@ -90,14 +82,34 @@ async function fetchStudents(idFilter, nameFilter, groupFilter) { const groupCell = row.querySelector(".c5"); const group = groupCell.querySelector("a.quickeditlink").textContent; - if ((!idFilter || id === idFilter) && - (!nameFilter || name.includes(nameFilter)) && - (!groupFilter || group.includes(groupFilter))) - { - students.push(new Student(id, name, group)); - } + fetchedStudents.push(new Student(id, name, group)); }); - return students; + return fetchedStudents; +} + +async function fetchStudents(idFilter, nameFilter, groupFilter) { + if (!students.size) { + const studentsPromise = _fetchStudents() + const storedStudents = localStorage.getItem("students"); + if (storedStudents) { + studentsPromise.then((fetched) => localStorage.setItem("students", JSON.stringify(fetched))); + for (const o of JSON.parse(storedStudents)) { + students.push(new Student(o.id, o.name, o.group)); + } + } else { + students = await studentsPromise; + localStorage.setItem("students", JSON.stringify(students)); + } + } + + return students.filter((s) => (!idFilter || s.id === idFilter) && + (!nameFilter || s.name.includes(nameFilter)) && + (!groupFilter || s.group.includes(groupFilter))); +} + +async function fetchStudent(idFilter, nameFilter, groupFilter) { + const students = await fetchStudents(idFilter, nameFilter, groupFilter); + return students[0]; } export { Student, fetchStudents, fetchStudent }; |
