From b796be7f804d15ed5fe1e95861bcfd7001dcc3db Mon Sep 17 00:00:00 2001 From: Tom Cooling Date: Fri, 17 Nov 2017 13:36:54 +0000 Subject: [PATCH 1/3] Add export to csv functionality - Using utils/export - Copied from the business-index-ui --- src/components/ResultsTable.js | 10 +++++++++- src/utils/export.js | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/utils/export.js diff --git a/src/components/ResultsTable.js b/src/components/ResultsTable.js index ff2b475..b46d6d5 100644 --- a/src/components/ResultsTable.js +++ b/src/components/ResultsTable.js @@ -5,6 +5,7 @@ import 'react-table/react-table.css'; import SummaryTable from '../components/SummaryTable'; import ChildRefTable from '../components/ChildRefTable'; import { employmentBands, legalStatusBands, tradingStatusBands, turnoverBands } from '../utils/convertBands'; +import { downloadCSV } from '../utils/export'; const ResultsTable = ({ results, showFilter, showPagination, defaultPageSize, convertBands }) => { return ( @@ -63,7 +64,14 @@ const ResultsTable = ({ results, showFilter, showPagination, defaultPageSize, co ); }} /> -

+
+
+
+

Export Results

+ +
+
+
Date: Fri, 17 Nov 2017 13:42:57 +0000 Subject: [PATCH 2/3] Add config/export with name of CSV --- src/config/export.js | 5 +++++ src/utils/export.js | 9 ++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 src/config/export.js diff --git a/src/config/export.js b/src/config/export.js new file mode 100644 index 0000000..f26b1ba --- /dev/null +++ b/src/config/export.js @@ -0,0 +1,5 @@ +const exportCsv = { + NAME: 'BusinessIndexResults', +}; + +export default exportCsv; diff --git a/src/utils/export.js b/src/utils/export.js index 74c5ca5..1f37bd5 100644 --- a/src/utils/export.js +++ b/src/utils/export.js @@ -1,3 +1,7 @@ +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 = ''; @@ -20,12 +24,11 @@ export function exportCSV(header, results) { export function downloadCSV(results) { const header = 'UBRN,Business Name,PostCode,Industry Code,Legal Status,Trading Status,Turnover,Employment,Company Reference Number'; - const name = 'CSV_Download'; - const csv = exportCSV(header, results, name); + 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`; + const filename = `${NAME}.csv`; link.download = filename; // Below append/remove child is to ensure the download button works on // Firefox, link.click() From 4a678259baebe8f66d4238144155b091599cef84 Mon Sep 17 00:00:00 2001 From: Tom Cooling Date: Fri, 17 Nov 2017 13:59:51 +0000 Subject: [PATCH 3/3] Add export to JSON button to ResultsTable --- src/components/ResultsTable.js | 4 +++- src/utils/export.js | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/components/ResultsTable.js b/src/components/ResultsTable.js index b46d6d5..2c18206 100644 --- a/src/components/ResultsTable.js +++ b/src/components/ResultsTable.js @@ -5,7 +5,7 @@ import 'react-table/react-table.css'; import SummaryTable from '../components/SummaryTable'; import ChildRefTable from '../components/ChildRefTable'; import { employmentBands, legalStatusBands, tradingStatusBands, turnoverBands } from '../utils/convertBands'; -import { downloadCSV } from '../utils/export'; +import { downloadCSV, downloadJSON } from '../utils/export'; const ResultsTable = ({ results, showFilter, showPagination, defaultPageSize, convertBands }) => { return ( @@ -69,6 +69,8 @@ const ResultsTable = ({ results, showFilter, showPagination, defaultPageSize, co

Export Results

+   +

diff --git a/src/utils/export.js b/src/utils/export.js index 1f37bd5..beab999 100644 --- a/src/utils/export.js +++ b/src/utils/export.js @@ -36,3 +36,12 @@ export function downloadCSV(results) { 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(); +}