Files
tripwithbonus/frontend/lib/search/fetchRoutes.ts
2025-05-28 12:30:08 +03:00

34 lines
910 B
TypeScript

import { SearchResponse } from '@/app/types'
// получаем все предложения по выбранному owner_type
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}${pageParam}`,
{
cache: 'no-store',
}
)
if (!response.ok) {
throw new Error('Failed to fetch search results')
}
const data = await response.json()
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, next: null, previous: null }
}
}