Add CSV export functionality for admin orders
- Created exportCSV utility with convertToCSV, downloadCSV, formatExportDate, formatExportIDR - Added export button to AdminOrders page with loading state - Export includes all order fields: ID, email, total, status, payment method, date, refund info - CSV format compatible with Excel and Google Sheets 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
62
src/lib/exportCSV.ts
Normal file
62
src/lib/exportCSV.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Export utility functions for CSV export
|
||||
*/
|
||||
|
||||
/**
|
||||
* Convert data array to CSV format
|
||||
*/
|
||||
export const convertToCSV = (data: Record<string, any>[], headers: string[]): string => {
|
||||
// Add headers
|
||||
const csvRows = [headers.join(',')];
|
||||
|
||||
// Add data rows
|
||||
data.forEach((row) => {
|
||||
const values = headers.map((header) => {
|
||||
const value = row[header];
|
||||
// Escape values that contain commas, quotes, or newlines
|
||||
if (value === null || value === undefined) {
|
||||
return '';
|
||||
}
|
||||
const stringValue = String(value);
|
||||
if (stringValue.includes(',') || stringValue.includes('"') || stringValue.includes('\n')) {
|
||||
return `"${stringValue.replace(/"/g, '""')}"`;
|
||||
}
|
||||
return stringValue;
|
||||
});
|
||||
csvRows.push(values.join(','));
|
||||
});
|
||||
|
||||
return csvRows.join('\n');
|
||||
};
|
||||
|
||||
/**
|
||||
* Trigger CSV download in browser
|
||||
*/
|
||||
export const downloadCSV = (csv: string, filename: string) => {
|
||||
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
|
||||
const link = document.createElement('a');
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
link.setAttribute('href', url);
|
||||
link.setAttribute('download', filename);
|
||||
link.style.visibility = 'hidden';
|
||||
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
};
|
||||
|
||||
/**
|
||||
* Format date for export (YYYY-MM-DD HH:mm:ss)
|
||||
*/
|
||||
export const formatExportDate = (date: string | Date): string => {
|
||||
const d = typeof date === 'string' ? new Date(date) : date;
|
||||
return d.toISOString().replace('T', ' ').substring(0, 19);
|
||||
};
|
||||
|
||||
/**
|
||||
* Format IDR for export (without "Rp" prefix for easier Excel processing)
|
||||
*/
|
||||
export const formatExportIDR = (amount: number): string => {
|
||||
return (amount / 100).toLocaleString('id-ID');
|
||||
};
|
||||
Reference in New Issue
Block a user