added image for posts

This commit is contained in:
SlimusMinus
2026-06-01 01:55:26 +03:00
parent 825f86fb16
commit a2dd7363b5
6 changed files with 351 additions and 224 deletions

View File

@@ -9,32 +9,31 @@ import MenuIcon from "../components/icons/MenuIcon";
export default function FeedPage() {
const [sidebarOpen, setSidebarOpen] = useState(false);
const [activeNav, setActiveNav] = useState("Главная");
const [modalOpen, setModalOpen] = useState(false);
const [editAd, setEditAd] = useState(null);
const [ads, setAds] = useState([]);
const [liked, setLiked] = useState(new Set());
const [loading, setLoading] = useState(true);
const [error, setError] = useState("");
const [activeNav, setActiveNav] = useState("Главная");
const [modalOpen, setModalOpen] = useState(false);
const [editAd, setEditAd] = useState(null);
const [ads, setAds] = useState([]);
const [liked, setLiked] = useState(new Set());
const [loading, setLoading] = useState(true);
const [error, setError] = useState("");
useEffect(() => {
const load = async () => {
setLoading(true);
setError("");
try {
const res = await fetch(`${API_URL}/api/v1/posts`);
if (!res.ok) throw new Error(`Ошибка ${res.status}`);
const data = await res.json();
setAds(Array.isArray(data) ? data : (data.items ?? []));
} catch (err) {
setError(err.message || "Не удалось загрузить объявления");
} finally {
setLoading(false);
}
};
load();
const loadFeed = useCallback(async () => {
setLoading(true);
setError("");
try {
const res = await fetch(`${API_URL}/api/v1/posts`);
if (!res.ok) throw new Error(`Ошибка ${res.status}`);
const data = await res.json();
setAds(Array.isArray(data) ? data : (data.items ?? []));
} catch (err) {
setError(err.message || "Не удалось загрузить объявления");
} finally {
setLoading(false);
}
}, []);
useEffect(() => { loadFeed(); }, [loadFeed]);
const toggleLike = useCallback((id) => {
setLiked(prev => {
const next = new Set(prev);
@@ -43,13 +42,9 @@ export default function FeedPage() {
});
}, []);
const handleNewAd = useCallback((ad) => {
setAds(prev => [ad, ...prev]);
}, []);
const handleEditSave = useCallback((updated) => {
setAds(prev => prev.map(a => a.id === updated.id ? updated : a));
}, []);
// перезагружаем ленту чтобы получить images из БД
const handleNewAd = useCallback(() => loadFeed(), [loadFeed]);
const handleEditSave = useCallback(() => loadFeed(), [loadFeed]);
const renderContent = () => {
if (loading) {