implement pagination
This commit is contained in:
@@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user