refactor parallel?

This commit is contained in:
Gaspard Jankowiak 2026-06-05 08:53:20 +02:00
commit ace00a0f14

View file

@ -19,11 +19,7 @@ async function getXSRFToken() {
return decodeURIComponent(tokenCookie.split("=").slice(1).join("="))
}
async function getApplicant(applicant, token) {
return getApplicantDetailsWithProgress(applicant, token)
}
async function getApplicantDetailsWithProgress(applicant, token, onProgress = () => { }) {
async function fetchApplicantDetails(applicant, token) {
return fetch(`https://personal.uni-graz.at/api/erec/job-applications/${applicant.id}`,
{
credentials: "same-origin",
@ -35,8 +31,10 @@ async function getApplicantDetailsWithProgress(applicant, token, onProgress = ()
}
throw `Failed to get applicant ${applicant.id}`
})
.then(async (jsonData) => {
const files = jsonData.application_files ?? []
}
async function downloadApplicantFilesWithProgress(applicantDetails, token, onProgress = () => { }) {
const files = applicantDetails.application_files ?? []
let completedDownloads = 0
let downloadedBytes = 0
@ -52,7 +50,7 @@ async function getApplicantDetailsWithProgress(applicant, token, onProgress = ()
)
if (!response.ok) {
throw `Failed to download file ${afile.file_id} for applicant ${applicant.id}`
throw `Failed to download file ${afile.file_id} for applicant ${applicantDetails.id}`
}
afile.blob = await response.blob()
@ -61,8 +59,12 @@ async function getApplicantDetailsWithProgress(applicant, token, onProgress = ()
onProgress({ downloaded: completedDownloads, total: files.length, bytes: downloadedBytes })
}))
return jsonData
})
return applicantDetails
}
async function getApplicant(applicant, token, onProgress = () => { }) {
const applicantDetails = await fetchApplicantDetails(applicant, token)
return downloadApplicantFilesWithProgress(applicantDetails, token, onProgress)
}
async function getApplicants() {
@ -137,8 +139,8 @@ function formatMegabytes(totalBytes) {
function createProgressDialog() {
const dialog = document.createElement("dialog")
const title = document.createElement("h2")
const status = document.createElement("p")
const title = document.createElement("h4")
const status = document.createElement("h2")
const list = document.createElement("ul")
const summary = document.createElement("div")
const elapsed = document.createElement("p")
@ -153,9 +155,9 @@ function createProgressDialog() {
let timerId = null
dialog.classList.add("ripper-progress-dialog")
title.textContent = "Download progress"
title.textContent = "EPAS Ripper"
status.classList.add("ripper-progress-status")
status.textContent = "Retrieving applicants list..."
status.textContent = "Retrieving applicants list (will take some time)..."
list.classList.add("ripper-progress-list")
summary.classList.add("ripper-progress-summary")
elapsed.classList.add("ripper-progress-elapsed")
@ -281,7 +283,7 @@ function rip(event) {
const applicantDetailsResults = await Promise.allSettled(applicants.map(async (applicant) => {
try {
const applicantDetails = await getApplicantDetailsWithProgress(applicant, token, ({ downloaded, total, bytes }) => {
const applicantDetails = await getApplicant(applicant, token, ({ downloaded, total, bytes }) => {
progressDialog.updateApplicant(applicant, downloaded, total, bytes)
})
return applicantDetails
@ -300,7 +302,6 @@ function rip(event) {
}
const applicantDetails = result.value
// const dirName = `${applicantDetails.last_name}_${applicantDetails.first_name}`
const dirName = `${sanitizeZipPathSegment(applicantDetails.last_name)}_${sanitizeZipPathSegment(applicantDetails.first_name)}`
const applicantDir = zip.folder(dirName)
const files = applicantDetails.application_files ?? []