enabled paging while fetching locations
This commit is contained in:
@@ -2,7 +2,6 @@ import NextAuth, { NextAuthConfig } from 'next-auth';
|
|||||||
import GoogleProvider from 'next-auth/providers/google';
|
import GoogleProvider from 'next-auth/providers/google';
|
||||||
import { Session } from 'next-auth';
|
import { Session } from 'next-auth';
|
||||||
import { AuthenticatedUser } from './types/next-auth';
|
import { AuthenticatedUser } from './types/next-auth';
|
||||||
import { BillingLocation } from './db-types';
|
|
||||||
|
|
||||||
const authConfig: NextAuthConfig = {
|
const authConfig: NextAuthConfig = {
|
||||||
callbacks: {
|
callbacks: {
|
||||||
@@ -60,7 +59,7 @@ export const isAuthErrorMessage = (obj: any): obj is AuthErrorMessage => {
|
|||||||
return (obj.message && obj.errors && obj.errors.message);
|
return (obj.message && obj.errors && obj.errors.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const withUser = (fn: (user: AuthenticatedUser, ...args:any) => Promise<Array<BillingLocation>>) => async (...args:any) => {
|
export const withUser = <T>(fn: (user: AuthenticatedUser, ...args:any) => Promise<T>) => async (...args:any) => {
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
|
|
||||||
if(!session) {
|
if(!session) {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { BillingLocation } from './db-types';
|
|||||||
import { ObjectId } from 'mongodb';
|
import { ObjectId } from 'mongodb';
|
||||||
import { auth, withUser } from '@/app/lib/auth';
|
import { auth, withUser } from '@/app/lib/auth';
|
||||||
import { AuthenticatedUser } from './types/next-auth';
|
import { AuthenticatedUser } from './types/next-auth';
|
||||||
|
import { NormalizedRouteManifest } from 'next/dist/server/base-server';
|
||||||
|
|
||||||
export type State = {
|
export type State = {
|
||||||
errors?: {
|
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 client = await clientPromise;
|
||||||
const db = client.db("rezije");
|
const db = client.db("rezije");
|
||||||
|
|
||||||
const { id: userId } = user;
|
const { id: userId } = user;
|
||||||
|
|
||||||
|
// fetch `pageSize` locations for the given page index
|
||||||
const locations = await db.collection<BillingLocation>("lokacije")
|
const locations = await db.collection<BillingLocation>("lokacije")
|
||||||
.find({ userId })
|
.find({ userId })
|
||||||
.sort({ yearMonth: -1, name: 1 }) // sort by yearMonth descending
|
.sort({ name: 1 })
|
||||||
.limit(20)
|
.skip(pageIx * pageSize)
|
||||||
.toArray();
|
.limit(pageSize)
|
||||||
|
.toArray();
|
||||||
|
|
||||||
return(locations);
|
return(locations);
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user