-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor download_data function to filter out unnecessary fields
- Loading branch information
=
committed
Feb 28, 2024
1 parent
40974ac
commit 4a21065
Showing
2 changed files
with
34 additions
and
63 deletions.
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 |
---|---|---|
@@ -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(); | ||
} |