Skip to content

Commit

Permalink
added excel export
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgrammerDATCH committed Aug 13, 2024
1 parent f3ba034 commit 20bf105
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/pages/seller/SellerDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useNavigate } from "react-router-dom";
import productSlice from "../../store/features/product/productSlice";
import { FaFileExcel } from "react-icons/fa";
import exportToCSV from "../../utils/excel/exportToCSV";
import exportToExcel from "../../utils/excel/exportToExcel";

const SellerDashboard = () => {
const { OrderHistory, message, data, isError } = useAppSelector(
Expand Down Expand Up @@ -164,7 +165,7 @@ const SellerDashboard = () => {
);

const exportStats = () => {
exportToCSV(OrderHistory)
exportToExcel(OrderHistory)
}

return (
Expand Down
59 changes: 32 additions & 27 deletions src/utils/excel/exportToExcel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,46 @@
import * as XLSX from 'xlsx';

const exportToExcel = (orderHistory) => {
const orderData = orderHistory.order.map(order => {
// Parse the products string to JSON
const products = JSON.parse(order.products);
const orders = orderHistory.order;
const rows = [
[
'Order ID',
'Order Date',
'Expected Delivery Date',
'Order Status',
'Payment Method',
'Products',
'Shipping Status'
]
];

orders.forEach(order => {
let productsInfo = '';

return products.map(product => ({
orderId: order.id,
cartId: order.cartId,
shopId: order.shopId,
orderDate: order.orderDate,
paymentMethodId: order.paymentMethodId,
status: order.status,
shippingProcess: order.shippingProcess,
expectedDeliveryDate: order.expectedDeliveryDate,
createdAt: order.createdAt,
updatedAt: order.updatedAt,
productId: product.id,
productName: product.name,
productPrice: product.price,
productDiscount: product.discount,
productQuantity: product.quantity,
productTotalPrice: product.totalPrice,
productDescription: product.description,
productImage: product.image,
}));
}).flat(); // Flatten the array of arrays
if (Array.isArray(order.products)) {
productsInfo = order.products.map(p => `${p.productId}(${p.status})`).join(', ');
}

rows.push([
order.id,
new Date(order.orderDate).toLocaleString(),
new Date(order.expectedDeliveryDate).toLocaleString(),
order.status,
order.paymentMethodId,
productsInfo,
order.shippingProcess
]);
});

// Convert the JSON data to a worksheet
const worksheet = XLSX.utils.json_to_sheet(orderData);
// Create a worksheet from the rows
const worksheet = XLSX.utils.aoa_to_sheet(rows);

// Create a new workbook and append the worksheet
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, 'OrderHistory');

// Export the workbook to an Excel file
XLSX.writeFile(workbook, 'OrderHistory.xlsx');
XLSX.writeFile(workbook, 'seller_orders.xlsx');
};

export default exportToExcel;

0 comments on commit 20bf105

Please sign in to comment.