diff --git a/frontend/src/pages/Admin/ProductsPage.jsx b/frontend/src/pages/Admin/ProductsPage.jsx index a397d57..2ca54eb 100644 --- a/frontend/src/pages/Admin/ProductsPage.jsx +++ b/frontend/src/pages/Admin/ProductsPage.jsx @@ -218,6 +218,105 @@ const AdminProductsPage = () => { 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 const handleOpenUploadDialog = () => { resetUploadState(); @@ -434,7 +533,16 @@ const AdminProductsPage = () => { Download Template - + + +