-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add export to csv functionality - Using utils/export - Copied from the business-index-ui * Add config/export with name of CSV * Add export to JSON button to ResultsTable
- Loading branch information
1 parent
ce98ca0
commit 8424764
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const exportCsv = { | ||
NAME: 'BusinessIndexResults', | ||
}; | ||
|
||
export default exportCsv; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import config from '../config/export'; | ||
|
||
const { NAME } = config; | ||
|
||
export function exportCSV(header, results) { | ||
const columnNames = ['id', 'businessName', 'postCode', 'industryCode', 'legalStatus', 'tradingStatus', 'turnover', 'employmentBands', 'companyNo']; | ||
let CSV = ''; | ||
CSV += `${header}\r\n`; // Firstly, insert the header | ||
// Go through the results, putting them in the CSV string | ||
for (let i = 0; i < results.length; i += 1) { | ||
let row = ''; | ||
// If there is missing data, insert an empty string | ||
for (let j = 0; j < columnNames.length; j += 1) { | ||
if (results[i][columnNames[j]] === undefined) { | ||
row += '"",'; | ||
} else { | ||
row += `"${results[i][columnNames[j]]}",`; | ||
} | ||
} | ||
CSV += `${row}\r\n`; | ||
} | ||
return CSV; | ||
} | ||
|
||
export function downloadCSV(results) { | ||
const header = 'UBRN,Business Name,PostCode,Industry Code,Legal Status,Trading Status,Turnover,Employment,Company Reference Number'; | ||
const csv = exportCSV(header, results, NAME); | ||
const uri = `data:text/csv;charset=utf-8,${escape(csv)}`; | ||
const link = document.createElement('a'); | ||
link.href = uri; | ||
const filename = `${NAME}.csv`; | ||
link.download = filename; | ||
// Below append/remove child is to ensure the download button works on | ||
// Firefox, link.click() | ||
document.body.appendChild(link); | ||
link.click(); | ||
document.body.removeChild(link); | ||
} | ||
|
||
export function downloadJSON(results) { | ||
const jsonStr = JSON.stringify(results, null, 2); | ||
const uri = `data:text/json;charset=utf-8,${escape(jsonStr)}`; | ||
const link = document.createElement('a'); | ||
link.href = uri; | ||
link.download = `${NAME}.json`; | ||
link.click(); | ||
} |