Skip to content

Commit

Permalink
Merge pull request #1833 from ResearchHub/csv-export-without-json
Browse files Browse the repository at this point in the history
Fetch CSV from endpoint instead of extracting from JSON
  • Loading branch information
gzurowski authored Oct 11, 2024
2 parents e378be8 + 8a1f381 commit 940e5c5
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions pages/user/[authorId]/[tabName]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,19 +372,14 @@ function AuthorPage(props) {
}
};

const downloadCSVFromJSON = (jsonData) => {
// Extract CSV data from the JSON object
const csvData = jsonData;
if (!csvData) {
console.error("No CSV data found in the provided JSON.");
const downloadCSVFromBlob = (data) => {
if (!data) {
console.error("No CSV data found.");
return;
}

// Convert the CSV data into a Blob
const blob = new Blob([csvData], { type: "text/csv" });

// Create a URL for the Blob
const url = URL.createObjectURL(blob);
const url = URL.createObjectURL(data);

// Create a temporary anchor element to trigger the download
const a = document.createElement("a");
Expand All @@ -402,8 +397,8 @@ function AuthorPage(props) {
setDownloading(true);
const url = generateApiUrl("transactions/list_csv");
const res = await fetch(url, api.GET_CONFIG());
const json = await res.json();
downloadCSVFromJSON(json);
const blob = await res.blob();
downloadCSVFromBlob(blob);
setDownloading(false);
};

Expand Down

0 comments on commit 940e5c5

Please sign in to comment.