feat: Add CSV download functionality for filtered dataset entries
This commit is contained in:
@@ -44,3 +44,42 @@ export async function downloadFilteredEntriesAsZip(
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function downloadFilteredEntriesAsCsv(
|
||||
datasetId: number,
|
||||
filters: FilterParams
|
||||
) {
|
||||
try {
|
||||
const response = await fetch('/api/download-metadata-csv', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
datasetId,
|
||||
filters,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
throw new Error(errorData.error || 'Failed to download CSV');
|
||||
}
|
||||
|
||||
// Get the CSV blob from response
|
||||
const blob = await response.blob();
|
||||
|
||||
// Trigger download
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `dataset-${datasetId}-metadata-${new Date().toISOString().split('T')[0]}.csv`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
} catch (error) {
|
||||
console.error('Error downloading CSV:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user