Skip to content

Commit

Permalink
Download terms on demand for export
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
psvenk committed Apr 21, 2020
1 parent 1cfbfcc commit ee2faa8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
1 change: 1 addition & 0 deletions public/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ <h3 id="export_modal_title">Export Data</h3>
</label>
</li>
</ul>
<p>Status: <span id="export_status"></span></p>
<button id="export_button">Export</button>
</div>
</div>
Expand Down
56 changes: 51 additions & 5 deletions public/js/buttonFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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&hellip;`
);
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;
};

Expand Down
20 changes: 18 additions & 2 deletions public/js/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,25 @@ let classesTable = new Tabulator("#classesTable", {
title: "Export Table Data",
titleFormatter: () => '<i class="fa fa-file-download header-icon" aria-hidden="true"></i>',
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 {
Expand Down

0 comments on commit ee2faa8

Please sign in to comment.