diff --git a/pages/datasets/index.tsx b/pages/datasets/index.tsx index d957eda..a3bb0d7 100644 --- a/pages/datasets/index.tsx +++ b/pages/datasets/index.tsx @@ -121,7 +121,7 @@ const Datasets: React.FC = ({ data, facets }) => { function handleDownloadClick() { setModalIsOpen(!modalIsOpen); if (downloadMethod === 'download-current') { - download_data(results, downloadType); + download_data(results); } else { if (downloadType === 'xlsx') window.open('/files/contracts-data.xlsx'); else window.open('/files/contracts-data.json'); diff --git a/utils/download_data.js b/utils/download_data.js index a42c028..6353440 100644 --- a/utils/download_data.js +++ b/utils/download_data.js @@ -1,63 +1,34 @@ -export function download_data(data, format) { - if (format === 'xlsx') { - const csvString = - 'data:text/csv;charset=utf-8,' + - [ - [ - 'ocid', - 'tender/id', - 'tender/externalReference', - 'tender/mainProcurementCategory', - 'tender/procurementMethod', - 'tender/contractType', - 'tenderclassification/description', - 'tender/participationFee/0/multiCurrencyAllowed', - 'tender/value/amount', - 'tender/datePublished', - 'tender/tenderPeriod/durationInDays', - 'Payment Mode', - 'tender/status', - 'tender/stage', - 'tender/numberOfTenderers', - 'tender/bidOpening/date', - 'buyer/name', - ], - ...data.map((item) => [ - item.ocid || '', - item.tender_id || '', - item.tender_externalreference || '', - item.tender_mainprocurementcategory || '', - item.tender_procurementmethod || '', - item.tender_contracttype || '', - item.tenderclassification_description || '', - item.tender_participationFee_0_multicurrencyallowed || '', - item.tender_value_amount || '', - item.tender_datepublished || '', - item.tender_tenderperiod_durationindays || '', - item.payment_mode || '', - item.tender_status || '', - item.tender_stage || '', - item.tender_numberoftenderers || '', - item.tender_bid_opening_date || '', - item.organization.title || '', - ]), - ] - .map((e) => e.join(',')) - .join('\n'); - var encodedUri = encodeURI(csvString); - window.open(encodedUri); - } else { - console.log(data); - // var dataStr = - // 'data:text/json;charset=utf-8,' + - // encodeURIComponent(JSON.stringify(data)); - // var downloadAnchorNode = document.createElement('a'); - // downloadAnchorNode.setAttribute('href', dataStr); - // downloadAnchorNode.setAttribute('download', 'contracts' + '.json'); - // document.body.appendChild(downloadAnchorNode); // required for firefox - // downloadAnchorNode.click(); - // downloadAnchorNode.remove(); - } -} +const acceptedFields = [ + 'bids', + 'buyer', + 'parties', + 'tender', + 'awards', + 'statistics', + 'ocid', + 'id_', + 'initiationType', + 'tags', +]; + +export function download_data(data) { + const filteredData = data.map((item) => { + const newItem = {}; + for (const key in item) { + if (acceptedFields.includes(key)) { + newItem[key] = item[key]; + } + } + return newItem; + }); -// bids, buyer, parties, tender, awards, statistics, ocid, id_, initiationType, tags + var dataStr = + 'data:text/json;charset=utf-8,' + + encodeURIComponent(JSON.stringify(filteredData)); + var downloadAnchorNode = document.createElement('a'); + downloadAnchorNode.setAttribute('href', dataStr); + downloadAnchorNode.setAttribute('download', 'contracts' + '.json'); + document.body.appendChild(downloadAnchorNode); // required for firefox + downloadAnchorNode.click(); + downloadAnchorNode.remove(); +}