implement pagination

This commit is contained in:
2025-05-28 12:30:08 +03:00
parent 9eaaff9eb2
commit a673210f9f
7 changed files with 215 additions and 30 deletions

View File

@@ -1,10 +1,15 @@
import { SearchResponse } from '@/app/types'
// получаем все предложения по выбранному owner_type
export async function fetchRoutes(category: string, query: string = ''): Promise<SearchResponse> {
export async function fetchRoutes(
category: string,
query: string = '',
page: number = 1
): Promise<SearchResponse> {
try {
const pageParam = page > 1 ? `${query ? '&' : '?'}page=${page}` : ''
const response = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/search/${category}/${query ? `?${query}` : ''}`,
`${process.env.NEXT_PUBLIC_API_URL}/search/${category}/${query}${pageParam}`,
{
cache: 'no-store',
}
@@ -15,9 +20,14 @@ export async function fetchRoutes(category: string, query: string = ''): Promise
}
const data = await response.json()
return data
return {
results: data.results,
count: data.count,
next: data.next,
previous: data.previous,
}
} catch (error) {
console.error('Error fetching search results:', error)
return { results: [], count: 0 }
return { results: [], count: 0, next: null, previous: null }
}
}