fixed search and add hints
This commit is contained in:
@@ -16,8 +16,22 @@ function extractPage(data) {
|
||||
return { items, last };
|
||||
}
|
||||
|
||||
/**
|
||||
* Собирает query-параметры фильтров ленты для GET /api/v1/posts.
|
||||
* @param {{ type?: string|null, categories?: string[], cities?: string[] }} filters
|
||||
*/
|
||||
function appendFilters(params, filters) {
|
||||
if (!filters) return;
|
||||
if (filters.type) params.set("type", filters.type);
|
||||
(filters.categories || []).forEach(c => params.append("category", c));
|
||||
(filters.cities || []).forEach(c => params.append("city", c));
|
||||
}
|
||||
|
||||
/**
|
||||
* Управляет основной лентой объявлений: загрузка, пагинация, поиск.
|
||||
* Поиск и фильтры (тип, категории, города) выполняет сервер — так бесконечная
|
||||
* прокрутка подгружает именно подходящие объявления, а не фильтрует страницы
|
||||
* «общей» ленты на клиенте.
|
||||
*/
|
||||
export function useFeedPosts() {
|
||||
const [ads, setAds] = useState([]);
|
||||
@@ -36,9 +50,13 @@ export function useFeedPosts() {
|
||||
// «устаревшим», и в ленте остаются только объявления со 2-й страницы.
|
||||
const firstPageLoadedRef = useRef(false);
|
||||
const resetInFlightRef = useRef(false);
|
||||
// Фильтры последней загрузки первой страницы: следующие страницы бесконечной
|
||||
// прокрутки обязаны запрашиваться с теми же фильтрами.
|
||||
const filtersRef = useRef(null);
|
||||
|
||||
const loadFeed = useCallback(async (pageNum = 0, search = "", append = false) => {
|
||||
const loadFeed = useCallback(async (pageNum = 0, search = "", append = false, filters) => {
|
||||
if (append && (!firstPageLoadedRef.current || resetInFlightRef.current)) return;
|
||||
if (!append) filtersRef.current = filters || null;
|
||||
if (!append) resetInFlightRef.current = true;
|
||||
const requestId = ++requestIdRef.current;
|
||||
if (pageNum === 0) {
|
||||
@@ -50,6 +68,7 @@ export function useFeedPosts() {
|
||||
try {
|
||||
const params = new URLSearchParams({ page: pageNum, size: PAGE_SIZE });
|
||||
if (search) params.set("search", search);
|
||||
appendFilters(params, append ? filtersRef.current : filters);
|
||||
|
||||
const res = await fetch(`${API_URL}/api/v1/posts?${params}`);
|
||||
if (!res.ok) throw new Error(`Error ${res.status}`);
|
||||
|
||||
Reference in New Issue
Block a user