fix order scrolling

This commit is contained in:
NotII
2025-03-02 03:18:19 +00:00
parent 550a755970
commit bb0258c5c6

View File

@@ -62,6 +62,17 @@ interface Order {
trackingNumber?: string;
}
interface OrderInList extends Order {
_id: string;
}
interface OrdersResponse {
orders: OrderInList[];
page: number;
totalPages: number;
totalOrders: number;
}
const getStatusVariant = (status: string) => {
switch (status) {
case 'acknowledged':
@@ -292,43 +303,48 @@ export default function OrderDetailsPage() {
if (!order?.orderId) return;
// Fetch previous and next orders in parallel
const [prevOrdersRes, nextOrdersRes] = await Promise.all([
// Get one order before the current one (newer orders)
fetchData(
`${process.env.NEXT_PUBLIC_API_URL}/orders?limit=1&after=${order.orderId}`,
{
method: "GET",
headers: { Authorization: `Bearer ${authToken}` },
}
),
// Get one order after the current one (older orders)
fetchData(
`${process.env.NEXT_PUBLIC_API_URL}/orders?limit=1&before=${order.orderId}`,
{
method: "GET",
headers: { Authorization: `Bearer ${authToken}` },
}
)
]);
// Get the current numerical orderId
const currentOrderId = parseInt(order.orderId);
console.log('Current orderId (numerical):', currentOrderId);
// Set previous (newer) order ID if exists
if (prevOrdersRes?.orders?.length > 0) {
setPrevOrderId(prevOrdersRes.orders[0]._id);
} else {
setPrevOrderId(null);
// Use the new optimized backend endpoint to get adjacent orders
const adjacentOrdersUrl = `${process.env.NEXT_PUBLIC_API_URL}/orders/adjacent/${currentOrderId}`;
console.log('Fetching adjacent orders:', adjacentOrdersUrl);
const adjacentOrdersRes = await fetchData(
adjacentOrdersUrl,
{
method: "GET",
headers: { Authorization: `Bearer ${authToken}` },
}
);
console.log('Adjacent orders response:', adjacentOrdersRes);
if (!adjacentOrdersRes) {
console.error("Invalid response from adjacent orders endpoint");
return;
}
// Set next (older) order ID if exists
if (nextOrdersRes?.orders?.length > 0) {
setNextOrderId(nextOrdersRes.orders[0]._id);
// Set the next and previous order IDs
const { newer, older } = adjacentOrdersRes;
// Set IDs for navigation
setPrevOrderId(newer?._id || null);
setNextOrderId(older?._id || null);
if (newer) {
console.log(`Newer order: ${newer.orderId} (ID: ${newer._id})`);
} else {
setNextOrderId(null);
console.log('No newer order found');
}
if (older) {
console.log(`Older order: ${older.orderId} (ID: ${older._id})`);
} else {
console.log('No older order found');
}
console.log('Current order:', order.orderId);
console.log('Newer order:', prevOrdersRes?.orders?.[0]?._id);
console.log('Older order:', nextOrdersRes?.orders?.[0]?._id);
} catch (error) {
console.error("Failed to fetch adjacent orders:", error);
}