Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 81 additions & 61 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,101 @@
import { useState, useMemo } from "react";
import { useState, useMemo, useDeferredValue, memo } from "react";
import "./App.css";
import { generateOrders, type Order } from "./generateOrderData";

interface OrderTableProps {
orders: Order[];
}

const OrderTable = memo(function OrderTable({ orders }: OrderTableProps) {
const formatDate = (date: Date) => {
return date.toLocaleDateString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
});
};

const formatCurrency = (amount: number, currency: string) => {
return `${currency} ${amount.toFixed(2)}`;
};
return (
<div className="table-container">
<table>
<thead>
<tr>
<th>Order #</th>
<th>Customer</th>
<th>Email</th>
<th>Date</th>
<th>Status</th>
<th>Total</th>
<th>Items</th>
<th>Payment</th>
<th>Address</th>
<th>City</th>
<th>Country</th>
<th>Tracking</th>
<th>Est. Delivery</th>
</tr>
</thead>
<tbody>
{orders.map((order: Order) => (
<tr key={order.id}>
<td>{order.orderNumber}</td>
<td>{order.customerName}</td>
<td>{order.customerEmail}</td>
<td>{formatDate(order.orderDate)}</td>
<td className={`status status-${order.status}`}>
{order.status}
</td>
<td>{formatCurrency(order.totalAmount, order.currency)}</td>
<td>{order.itemsCount}</td>
<td>{order.paymentMethod.replace("_", " ")}</td>
<td>{order.shippingAddress}</td>
<td>{order.shippingCity}</td>
<td>{order.shippingCountry}</td>
<td>{order.trackingNumber}</td>
<td>{formatDate(order.estimatedDelivery)}</td>
</tr>
))}
</tbody>
</table>
</div>
);
});

function App() {
const [searchTerm, setSearchTerm] = useState("");
const deferredSearchTerm = useDeferredValue(searchTerm);
const [activeStatuses, setActiveStatuses] = useState<Set<Order["status"]>>(
new Set(["pending", "processing", "shipped", "delivered", "cancelled"])
);

// orders could come from a server, in this example it does not matter.
const orders = useMemo(() => generateOrders(5000), []);

const filteredOrders = useMemo(() => {
return orders.filter((order) => {
const matchesSearch =
searchTerm === "" ||
order.orderNumber.toLowerCase().includes(searchTerm.toLowerCase()) ||
order.customerName.toLowerCase().includes(searchTerm.toLowerCase()) ||
order.customerEmail.toLowerCase().includes(searchTerm.toLowerCase()) ||
order.trackingNumber.toLowerCase().includes(searchTerm.toLowerCase());
deferredSearchTerm === "" ||
order.orderNumber
.toLowerCase()
.includes(deferredSearchTerm.toLowerCase()) ||
order.customerName
.toLowerCase()
.includes(deferredSearchTerm.toLowerCase()) ||
order.customerEmail
.toLowerCase()
.includes(deferredSearchTerm.toLowerCase()) ||
order.trackingNumber
.toLowerCase()
.includes(deferredSearchTerm.toLowerCase());

const matchesStatus = activeStatuses.has(order.status);

return matchesSearch && matchesStatus;
});
}, [orders, searchTerm, activeStatuses]);
}, [orders, deferredSearchTerm, activeStatuses]);

const toggleStatus = (status: Order["status"]) => {
setActiveStatuses((prev) => {
Expand All @@ -37,18 +109,6 @@ function App() {
});
};

const formatDate = (date: Date) => {
return date.toLocaleDateString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
});
};

const formatCurrency = (amount: number, currency: string) => {
return `${currency} ${amount.toFixed(2)}`;
};

const statuses: Order["status"][] = [
"pending",
"processing",
Expand All @@ -63,6 +123,7 @@ function App() {
<div className="controls">
<div className="search-container">
<input
autoComplete="off"
type="text"
placeholder="Search by order #, customer name, email, or tracking..."
value={searchTerm}
Expand Down Expand Up @@ -91,48 +152,7 @@ function App() {
</p>
</div>

<div className="table-container">
<table>
<thead>
<tr>
<th>Order #</th>
<th>Customer</th>
<th>Email</th>
<th>Date</th>
<th>Status</th>
<th>Total</th>
<th>Items</th>
<th>Payment</th>
<th>Address</th>
<th>City</th>
<th>Country</th>
<th>Tracking</th>
<th>Est. Delivery</th>
</tr>
</thead>
<tbody>
{filteredOrders.map((order: Order) => (
<tr key={order.id}>
<td>{order.orderNumber}</td>
<td>{order.customerName}</td>
<td>{order.customerEmail}</td>
<td>{formatDate(order.orderDate)}</td>
<td className={`status status-${order.status}`}>
{order.status}
</td>
<td>{formatCurrency(order.totalAmount, order.currency)}</td>
<td>{order.itemsCount}</td>
<td>{order.paymentMethod.replace("_", " ")}</td>
<td>{order.shippingAddress}</td>
<td>{order.shippingCity}</td>
<td>{order.shippingCountry}</td>
<td>{order.trackingNumber}</td>
<td>{formatDate(order.estimatedDelivery)}</td>
</tr>
))}
</tbody>
</table>
</div>
<OrderTable orders={filteredOrders} />
</div>
);
}
Expand Down
4 changes: 0 additions & 4 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ a:hover {

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
Expand Down