Spaces:
Paused
Paused
File size: 3,238 Bytes
ab0678b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | /**
* Utility function to export JSON data to CSV and trigger a download
*/
export const exportToCSV = (data: any[], type: string) => {
if (!data || data.length === 0) {
alert('No data available to export');
return;
}
let headers: string[] = [];
let rows: any[] = [];
switch (type) {
case 'products':
headers = ['ID', 'Name', 'Chinese Name', 'Price (IQD)', 'Price (RMB)', 'Featured', 'Active', 'Created At'];
rows = data.map(p => [
p.id,
p.name,
p.chineseName || '',
p.price,
p.basePriceRMB || '',
p.isFeatured ? 'Yes' : 'No',
p.isActive ? 'Yes' : 'No',
new Date(p.createdAt).toLocaleDateString()
]);
break;
case 'orders':
headers = ['Order ID', 'Customer', 'Phone', 'Total', 'Status', 'Items Count', 'Created At'];
rows = data.map(o => [
o.id,
o.user?.name || 'N/A',
o.user?.phone || 'N/A',
o.total,
o.status,
o.items?.length || 0,
new Date(o.createdAt).toLocaleDateString()
]);
break;
case 'users':
headers = ['User ID', 'Name', 'Phone', 'Role', 'Orders Count', 'Created At'];
rows = data.map(u => [
u.id,
u.name,
u.phone,
u.role,
u._count?.orders || 0,
new Date(u.createdAt).toLocaleDateString()
]);
break;
case 'activity':
headers = ['Admin', 'Action', 'Target', 'Details', 'Date'];
rows = data.map(log => [
log.adminName,
log.action,
log.targetType,
log.details,
new Date(log.createdAt).toLocaleString()
]);
break;
case 'reviews':
headers = ['User', 'Product', 'Rating', 'Comment', 'Date'];
rows = data.map(r => [
r.user?.name || 'N/A',
r.product?.name || 'N/A',
r.rating,
r.comment,
new Date(r.createdAt).toLocaleDateString()
]);
break;
case 'coupons':
headers = ['Code', 'Type', 'Value', 'Min Order', 'Max Discount', 'Expiry', 'Usage', 'Active'];
rows = data.map(c => [
c.code,
c.discountType,
c.discountValue,
c.minOrderAmount,
c.maxDiscount || 'N/A',
c.endDate ? new Date(c.endDate).toLocaleDateString() : 'Never',
`${c.usageCount} / ${c.maxUsage || '∞'}`,
c.isActive ? 'Yes' : 'No'
]);
break;
default:
// Generic export
headers = Object.keys(data[0]);
rows = data.map(row => headers.map(header => row[header]));
}
const csvContent = [
headers.join(','),
...rows.map(row => row.map((val: any) => {
const stringVal = val === null || val === undefined ? '' : String(val);
return `"${stringVal.replace(/"/g, '""')}"`;
}).join(','))
].join('\n');
const blob = new Blob(["\ufeff" + csvContent], { type: 'text/csv;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.setAttribute('href', url);
link.setAttribute('download', `${type}_export_${new Date().toISOString().split('T')[0]}.csv`);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
|