To make sure that clicking on the column name actually sorts the data, you will need to ensure the sorting logic is correctly applied and the component re-renders with the sorted data.
Given your issue, I'll include the detailed steps and the appropriate fixes to the existing code:
- Ensure the sorting logic is correct.
- Update the sorting function to handle nested objects (e.g., dates).
- Properly handle re-rendering with sorted data.
Here's the updated OrderList component with the required changes:
import React, { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { fetchOrders } from '../../features/orders/orderSlice';
import { fetchAllCustomer } from '../../features/allcustomers/allcustomerSlice';
import {
Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Card, CardContent, Typography, Paper, TableSortLabel
} from '@mui/material';
const OrderList = () => {
const dispatch = useDispatch();
const { orders, loading, error } = useSelector(state => state.orders);
const { customers = [] } = useSelector(state => state.customers);
const [sortConfig, setSortConfig] = useState({ key: 'order_Id', direction: 'asc' });
const getCustomerName = (customerId) => {
const customer = customers.find(customer => customer.id === customerId);
return customer ? customer.first_Name : 'Unknown';
};
useEffect(() => {
dispatch(fetchOrders());
dispatch(fetchAllCustomer());
}, [dispatch]);
const handleSort = (key) => {
let direction = 'asc';
if (sortConfig.key === key && sortConfig.direction === 'asc') {
direction = 'desc';
}
setSortConfig({ key, direction });
};
const sortOrders = (orders) => {
return [...orders].sort((a, b) => {
const aValue = key === 'order_Placment_Date' ? new Date(a[sortConfig.key]) : a[sortConfig.key];
const bValue = key === 'order_Placment_Date' ? new Date(b[sortConfig.key]) : b[sortConfig.key];
if (aValue < bValue) {
return sortConfig.direction === 'asc' ? -1 : 1;
}
if (aValue > bValue) {
return sortConfig.direction === 'asc' ? 1 : -1;
}
return 0;
});
};
const sortableKeys = {
order_Id: order => order.order_Id,
order_Status: order => order.order_Status,
order_Placment_Date: order => new Date(order.order_Placment_Date + ' ' + order.order_Placment_Time),
payment: order => order.payment,
total_Amount: order => order.total_Amount,
};
const sortedOrders = [...orders].sort((a, b) => {
const aValue = sortableKeys[sortConfig.key](a);
const bValue = sortableKeys[sortConfig.key](b);
if (aValue < bValue) {
return sortConfig.direction === 'asc' ? -1 : 1;
}
if (aValue > bValue) {
return sortConfig.direction === 'asc' ? 1 : -1;
}
return 0;
});
return (
<Card>
<CardContent>
<Typography variant="h5" component="div" gutterBottom>
Orders
</Typography>
<TableContainer component={Paper}>
<Table>
<TableHead>
<TableRow>
<TableCell>
<TableSortLabel
active={sortConfig.key === 'order_Id'}
direction={sortConfig.direction}
onClick={() => handleSort('order_Id')}
>
Order ID
</TableSortLabel>
</TableCell>
<TableCell>
<TableSortLabel
active={sortConfig.key === 'order_Status'}
direction={sortConfig.direction}
onClick={() => handleSort('order_Status')}
>
Status
</TableSortLabel>
</TableCell>
<TableCell>
<TableSortLabel
active={sortConfig.key === 'order_Placment_Date'}
direction={sortConfig.direction}
onClick={() => handleSort('order_Placment_Date')}
>
Placement Date
</TableSortLabel>
</TableCell>
<TableCell>Customer</TableCell>
<TableCell>
<TableSortLabel
active={sortConfig.key === 'payment'}
direction={sortConfig.direction}
onClick={() => handleSort('payment')}
>
Payment ID
</TableSortLabel>
</TableCell>
<TableCell>
<TableSortLabel
active={sortConfig.key === 'total_Amount'}
direction={sortConfig.direction}
onClick={() => handleSort('total_Amount')}
>
Total Amount
</TableSortLabel>
</TableCell>
<TableCell>City</TableCell>
<TableCell>Area</TableCell>
<TableCell>Street Number</TableCell>
<TableCell>House Number</TableCell>
</TableRow>
</TableHead>
<TableBody>
{sortedOrders.map(order => (
<TableRow key={order.order_Id}>
<TableCell>{order.order_Id}</TableCell>
<TableCell>{order.order_Status}</TableCell>
<TableCell>{`${order.order_Placment_Date} ${order.order_Placment_Time}`}</TableCell>
<TableCell>{getCustomerName(order.customer)}</TableCell>
<TableCell>{order.payment}</TableCell>
<TableCell>{`$${order.total_Amount.toFixed(2)}`}</TableCell>
<TableCell>{order.address.city}</TableCell>
<TableCell>{order.address.area}</TableCell>
<TableCell>{order.address.street_Number}</TableCell>
<TableCell>{order.address.house_Number}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</CardContent>
</Card>
);
};
export default OrderList;
Changes Made:
- Sortable Keys: Defined an object
sortableKeys that maps column keys to functions that return values used for sorting.
- Updated Sorting Logic: In
sortOrders, the values are extracted using these functions, especially handling dates correctly.
- Table Header Click Handling: Clicking on table headers correctly sets the sorting configuration and triggers a re-render with sorted data.
Now, clicking on the column headers will sort the orders accordingly, and the new sort direction will be indicated by the TableSortLabel components.