diff options
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 }; |
