Skip to content

Commit

Permalink
Refactor download_data function to filter out unnecessary fields
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Feb 28, 2024
1 parent 40974ac commit 4a21065
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 63 deletions.
2 changes: 1 addition & 1 deletion pages/datasets/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const Datasets: React.FC<Props> = ({ 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');
Expand Down
95 changes: 33 additions & 62 deletions utils/download_data.js
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 4a21065

Please sign in to comment.