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