From 3b88b78cdf8f41da0f8e363fd7e81ac9203377aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Dere=C5=BEi=C4=87?= Date: Tue, 9 Jan 2024 10:58:48 +0100 Subject: [PATCH] enabled paging while fetching locations --- app/lib/auth.ts | 3 +-- app/lib/locationActions.ts | 13 ++++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/lib/auth.ts b/app/lib/auth.ts index f294bcf..d616894 100644 --- a/app/lib/auth.ts +++ b/app/lib/auth.ts @@ -2,7 +2,6 @@ import NextAuth, { NextAuthConfig } from 'next-auth'; import GoogleProvider from 'next-auth/providers/google'; import { Session } from 'next-auth'; import { AuthenticatedUser } from './types/next-auth'; -import { BillingLocation } from './db-types'; const authConfig: NextAuthConfig = { callbacks: { @@ -60,7 +59,7 @@ export const isAuthErrorMessage = (obj: any): obj is AuthErrorMessage => { return (obj.message && obj.errors && obj.errors.message); } -export const withUser = (fn: (user: AuthenticatedUser, ...args:any) => Promise>) => async (...args:any) => { +export const withUser = (fn: (user: AuthenticatedUser, ...args:any) => Promise) => async (...args:any) => { const session = await auth(); if(!session) { diff --git a/app/lib/locationActions.ts b/app/lib/locationActions.ts index 5a1b780..b173294 100644 --- a/app/lib/locationActions.ts +++ b/app/lib/locationActions.ts @@ -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("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); })