Posts API stage 2: one post at /posts/{id}, no legacy user-posts fallback
- Post detail and map focus load GET /api/v1/posts/{id}
- My posts use only GET /api/v1/users/{id}/posts
- README: requires the stage 2 backend (deploy backend first)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,20 +8,15 @@ const PAGE_SIZE = 50;
|
||||
const MAX_PAGES = 20;
|
||||
|
||||
/**
|
||||
* Объявления пользователя: новый маршрут GET /api/v1/users/{userId}/posts
|
||||
* (Page<PostResponse>). Подгружаем все страницы — «Мои объявления» показываются
|
||||
* целиком, а своих объявлений у пользователя немного.
|
||||
*
|
||||
* @returns {Promise<object[]|null>} null — маршрута нет (старый бэкенд)
|
||||
* Объявления пользователя: GET /api/v1/users/{userId}/posts (Page<PostResponse>).
|
||||
* Подгружаем все страницы — «Мои объявления» показываются целиком, а своих
|
||||
* объявлений у пользователя немного.
|
||||
*/
|
||||
async function fetchUserPostsPaged(userId) {
|
||||
async function fetchUserPosts(userId) {
|
||||
const all = [];
|
||||
for (let page = 0; page < MAX_PAGES; page++) {
|
||||
const params = new URLSearchParams({ page, size: PAGE_SIZE, sort: "createdAt,desc" });
|
||||
const res = await apiFetch(`${API_URL}/api/v1/users/${userId}/posts?${params}`);
|
||||
// Старый бэкенд: маршрута нет (404) либо /users/** закрыт без токена и
|
||||
// неизвестный путь падает в 500 — откатываемся на прежний эндпоинт
|
||||
if (page === 0 && (res.status === 404 || res.status === 405 || res.status >= 500)) return null;
|
||||
if (!res.ok) throw new Error(`Error ${res.status}`);
|
||||
const data = await res.json();
|
||||
const items = Array.isArray(data) ? data : (data.content ?? data.items ?? []);
|
||||
@@ -32,15 +27,6 @@ async function fetchUserPostsPaged(userId) {
|
||||
return all;
|
||||
}
|
||||
|
||||
/** Прежний маршрут: GET /api/v1/posts/{userId} — список без пагинации. */
|
||||
async function fetchUserPostsLegacy(userId) {
|
||||
const res = await apiFetch(`${API_URL}/api/v1/posts/${userId}`);
|
||||
if (res.status === 404) return [];
|
||||
if (!res.ok) throw new Error(`Error ${res.status}`);
|
||||
const data = await res.json();
|
||||
return Array.isArray(data) ? data : (data.items ?? data.content ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Загружает объявления текущего пользователя по userId.
|
||||
*
|
||||
@@ -61,7 +47,7 @@ export function useMyPosts(userId) {
|
||||
setLoading(true);
|
||||
setError("");
|
||||
try {
|
||||
const posts = (await fetchUserPostsPaged(userId)) ?? (await fetchUserPostsLegacy(userId));
|
||||
const posts = await fetchUserPosts(userId);
|
||||
if (requestId !== requestIdRef.current) return;
|
||||
setMyAds(posts.map(ad => ({
|
||||
...ad,
|
||||
|
||||
Reference in New Issue
Block a user