From ee2faa870300b916db718c5c5fdd3a30a84960ac Mon Sep 17 00:00:00 2001 From: psvenk <45520974+psvenk@users.noreply.github.com> Date: Tue, 21 Apr 2020 10:56:25 -0400 Subject: [PATCH] Download terms on demand for export Instead of simply disabling terms that have not yet been downloaded from export, allow the user to select those terms and have their data automatically downloaded and included in the export. --- public/home.html | 1 + public/js/buttonFunctions.js | 56 ++++++++++++++++++++++++++++++++---- public/js/home.js | 20 +++++++++++-- 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/public/home.html b/public/home.html index e1be8030..6961633a 100644 --- a/public/home.html +++ b/public/home.html @@ -189,6 +189,7 @@

Export Data

+

Status:

diff --git a/public/js/buttonFunctions.js b/public/js/buttonFunctions.js index c0748832..ef1821bb 100755 --- a/public/js/buttonFunctions.js +++ b/public/js/buttonFunctions.js @@ -293,18 +293,64 @@ let exportTableData = async function(prefs) { if (prefs.recent) obj.recent = currentTableData.recent; if (prefs.schedule) obj.schedule = currentTableData.schedule; if (prefs.terms) { - obj.terms = {}; - termConverter.forEach(term => { - if (prefs.terms[term]) obj.terms[term] = currentTableData.terms[term]; - }); + const origCurrentTerm = currentTerm; + try { + terms = await Promise.all(termConverter.map(async term => { + // Term is selected by user and its data are already downloaded + if (prefs.terms[term] && currentTableData.terms[term].classes) { + return currentTableData.terms[term]; + } + // Term is selected by user and its data have not been downloaded + else if (prefs.terms[term] && !currentTableData.imported) { + try { + $("#export_status").html( + `Downloading quarter "${term}" from Aspen…` + ); + const response = await $.ajax({ + url: "/data", + method: "POST", + data: { quarter: parseInt(term.match(/\d+/)[0]) }, + dataType: "json json" + }); + currentTerm = term; + responseCallbackPartial(response); + $("#export_status").html(""); + return currentTableData.terms[term]; + } + catch (err) { + throw `Error while downloading quarter "${term}".`; + } + } + })); + obj.terms = {}; + // Add the term data to obj.terms + termConverter.forEach((term, i) => { + if (terms[i]) { + obj.terms[term] = terms[i]; + } + }); + } + catch (err) { + $("#export_status").html(err); + } + finally { + currentTerm = origCurrentTerm; + currentTableData.currentTermData = currentTableData.terms[currentTerm]; + classesTable.setData(currentTableData.currentTermData.classes); + classesTable.redraw(); + } } if (prefs.cumGPA) obj.cumGPA = currentTableData.cumGPA; let jsonString = JSON.stringify(obj); + const filename = `aspine-export-${new Date().toISOString()}.json`; + + $("#export_status").html(`Generated file "${filename}".`); + saveAs(new Blob([jsonString], { type: "application/json;charset=utf-8" - }), `aspine-export-${new Date().toISOString()}.json`); + }), filename); return jsonString; }; diff --git a/public/js/home.js b/public/js/home.js index 634bad50..68edf833 100644 --- a/public/js/home.js +++ b/public/js/home.js @@ -566,9 +566,25 @@ let classesTable = new Tabulator("#classesTable", { title: "Export Table Data", titleFormatter: () => '', headerClick: async () => { - // Disable checkboxes for terms whose data have not yet been downloaded + // Disable checkboxes for inaccessible terms termConverter.forEach(term => { - if (currentTableData.terms[term].classes) { + // Boolean storing whether or not this term is 'accessible' + let accessible = true; + // If no GPA is available for the term, it is inaccessible + // (the term was not included in Aspen's overview) + if (!currentTableData.terms[term].GPA.percent) { + accessible = false; + } + // If currentTableData is imported, we cannot scrape Aspen + // for more data, so any terms not included in the import + // are inaccessible + if ( + currentTableData.imported && + !currentTableData.terms[term].classes + ) accessible = false; + + // Disable the checkboxes + if (accessible) { $(`#export_checkbox_terms_${term}`).removeAttr("disabled"); } else {