Update order-table.tsx

This commit is contained in:
NotII
2025-02-21 13:30:50 +00:00
parent b316246313
commit c6a83e8725

View File

@@ -31,6 +31,17 @@ import Link from "next/link";
import { clientFetch } from '@/lib/client-utils'; import { clientFetch } from '@/lib/client-utils';
import { toast } from "sonner"; import { toast } from "sonner";
import { Checkbox } from "@/components/ui/checkbox"; import { Checkbox } from "@/components/ui/checkbox";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
interface Order { interface Order {
_id: string; _id: string;
@@ -61,6 +72,7 @@ export default function OrderTable() {
direction: "asc" | "desc"; direction: "asc" | "desc";
}>({ column: "orderDate", direction: "desc" }); }>({ column: "orderDate", direction: "desc" });
const [selectedOrders, setSelectedOrders] = useState<Set<string>>(new Set()); const [selectedOrders, setSelectedOrders] = useState<Set<string>>(new Set());
const [isShipping, setIsShipping] = useState(false);
const ITEMS_PER_PAGE = 10; const ITEMS_PER_PAGE = 10;
// Fetch orders with error handling // Fetch orders with error handling
@@ -140,23 +152,39 @@ export default function OrderTable() {
} }
try { try {
await clientFetch("/orders/mark-shipped", { setIsShipping(true);
const response = await clientFetch("/orders/mark-shipped", {
method: "POST", method: "POST",
body: JSON.stringify({ orderIds: Array.from(selectedOrders) }) body: JSON.stringify({ orderIds: Array.from(selectedOrders) })
}); });
setOrders(prev => // Only update orders that were successfully marked as shipped
prev.map(order => if (response.success && response.success.orders) {
selectedOrders.has(order._id) const successfulOrderIds = new Set(response.success.orders.map((o: any) => o.id));
? { ...order, status: "shipped" }
: order setOrders(prev =>
) prev.map(order =>
); successfulOrderIds.has(order._id)
? { ...order, status: "shipped" }
: order
)
);
if (response.failed && response.failed.count > 0) {
toast.warning(`${response.failed.count} orders could not be marked as shipped`);
}
if (response.success.count > 0) {
toast.success(`${response.success.count} orders marked as shipped`);
}
}
setSelectedOrders(new Set()); setSelectedOrders(new Set());
toast.success("Selected orders marked as shipped");
} catch (error) { } catch (error) {
toast.error("Failed to update orders"); toast.error("Failed to update orders");
console.error("Shipping error:", error); console.error("Shipping error:", error);
} finally {
setIsShipping(false);
} }
}; };
@@ -188,10 +216,33 @@ export default function OrderTable() {
</SelectContent> </SelectContent>
</Select> </Select>
<Button onClick={markAsShipped} disabled={selectedOrders.size === 0}> <AlertDialog>
<Truck className="mr-2 h-5 w-5" /> <AlertDialogTrigger asChild>
Mark Shipped ({selectedOrders.size}) <Button disabled={selectedOrders.size === 0 || isShipping}>
</Button> <Truck className="mr-2 h-5 w-5" />
{isShipping ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
`Mark Shipped (${selectedOrders.size})`
)}
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Mark Orders as Shipped</AlertDialogTitle>
<AlertDialogDescription>
Are you sure you want to mark {selectedOrders.size} order{selectedOrders.size !== 1 ? 's' : ''} as shipped?
This action cannot be undone.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction onClick={markAsShipped}>
Confirm
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div> </div>
{/* Table */} {/* Table */}
@@ -244,6 +295,7 @@ export default function OrderTable() {
<Checkbox <Checkbox
checked={selectedOrders.has(order._id)} checked={selectedOrders.has(order._id)}
onCheckedChange={() => toggleSelection(order._id)} onCheckedChange={() => toggleSelection(order._id)}
disabled={order.status !== "paid"}
/> />
</TableCell> </TableCell>
<TableCell>#{order.orderId}</TableCell> <TableCell>#{order.orderId}</TableCell>