bulk export

This commit is contained in:
2ManyProjects 2025-05-05 16:11:07 -05:00
parent 3bab4e4c56
commit a06863562c

View file

@ -218,6 +218,105 @@ const AdminProductsPage = () => {
XLSX.writeFile(workbook, 'products_upload_template.xlsx'); XLSX.writeFile(workbook, 'products_upload_template.xlsx');
}; };
// Handle export all products
const handleExportAllProducts = () => {
if (!products || products.length === 0) {
// Show notification if no products to export
alert("No products to export");
return;
}
// Create headers (same as template, for consistency)
const headers = [
'name',
'description',
'categoryName',
'price',
'stockQuantity',
'weightGrams',
'lengthCm',
'widthCm',
'heightCm',
'origin',
'age',
'materialType',
'color',
'stockNotification_enabled',
'stockNotification_email',
'stockNotification_threshold',
'tags'
];
// Format the product data for Excel
const formattedData = products.map(product => {
// Extract stock notification values if they exist
let stockNotificationEnabled = 'FALSE';
let stockNotificationEmail = '';
let stockNotificationThreshold = '';
if (product.stock_notification) {
try {
const notification =
typeof product.stock_notification === 'string'
? JSON.parse(product.stock_notification)
: product.stock_notification;
stockNotificationEnabled = notification.enabled ? 'TRUE' : 'FALSE';
stockNotificationEmail = notification.email || '';
stockNotificationThreshold = notification.threshold || '';
} catch (e) {
console.error('Error parsing stock notification:', e);
}
}
// Convert tags array to comma-separated string if it exists
const tagsString = Array.isArray(product.tags)
? product.tags.join(',')
: (product.tags || '');
// Return array in the same order as headers
return [
product.name || '',
product.description || '',
product.category_name || '',
product.price || '',
product.stock_quantity || '',
product.weight_grams || '',
product.length_cm || '',
product.width_cm || '',
product.height_cm || '',
product.origin || '',
product.age || '',
product.material_type || '',
product.color || '',
stockNotificationEnabled,
stockNotificationEmail,
stockNotificationThreshold,
tagsString
];
});
// Add headers as first row
const data = [headers, ...formattedData];
// Create Excel workbook and add the data
const worksheet = XLSX.utils.aoa_to_sheet(data);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, 'Products');
// Add column widths for better readability
const colWidths = [];
headers.forEach(() => colWidths.push({ wch: 15 }));
worksheet['!cols'] = colWidths;
// Generate filename with current date
const date = new Date().toISOString().split('T')[0]; // YYYY-MM-DD format
const filename = `products_export_${date}.xlsx`;
// Write Excel file and trigger download
XLSX.writeFile(workbook, filename);
};
// Handle open upload dialog // Handle open upload dialog
const handleOpenUploadDialog = () => { const handleOpenUploadDialog = () => {
resetUploadState(); resetUploadState();
@ -434,7 +533,16 @@ const AdminProductsPage = () => {
Download Template Download Template
</Button> </Button>
</Tooltip> </Tooltip>
<Tooltip title="Export all current products to Excel">
<Button
variant="outlined"
color="primary"
startIcon={<DownloadIcon />}
onClick={handleExportAllProducts}
>
Export All Products
</Button>
</Tooltip>
<Tooltip title="Upload products in bulk from a CSV or Excel file"> <Tooltip title="Upload products in bulk from a CSV or Excel file">
<Button <Button
variant="outlined" variant="outlined"
@ -442,7 +550,7 @@ const AdminProductsPage = () => {
startIcon={<UploadIcon />} startIcon={<UploadIcon />}
onClick={handleOpenUploadDialog} onClick={handleOpenUploadDialog}
> >
Upload Bulk Assets Upload Bulk Products
</Button> </Button>
</Tooltip> </Tooltip>