enabled paging while fetching locations

This commit is contained in:
2024-01-09 10:58:48 +01:00
parent 6a6a3976a5
commit 3b88b78cdf
2 changed files with 9 additions and 7 deletions

View File

@@ -8,6 +8,7 @@ import { BillingLocation } from './db-types';
import { ObjectId } from 'mongodb';
import { auth, withUser } from '@/app/lib/auth';
import { AuthenticatedUser } from './types/next-auth';
import { NormalizedRouteManifest } from 'next/dist/server/base-server';
export type State = {
errors?: {
@@ -89,18 +90,20 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
});
export const fetchAllLocations = withUser(async (user:AuthenticatedUser, locationID:string) => {
export const fetchAllLocations = withUser(async (user:AuthenticatedUser, locationID:string, pageIx:number=0, pageSize:number=20) => {
const client = await clientPromise;
const db = client.db("rezije");
const { id: userId } = user;
// fetch `pageSize` locations for the given page index
const locations = await db.collection<BillingLocation>("lokacije")
.find({ userId })
.sort({ yearMonth: -1, name: 1 }) // sort by yearMonth descending
.limit(20)
.toArray();
.find({ userId })
.sort({ name: 1 })
.skip(pageIx * pageSize)
.limit(pageSize)
.toArray();
return(locations);
})