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

@@ -6,7 +6,7 @@ export function useCreatePost() {
const [error, setError] = useState(null);
const [success, setSuccess] = useState(false);
const createAd = async (adData) => {
const createAd = async (formData) => { // ← принимаем FormData
setLoading(true);
setError(null);
@@ -16,20 +16,20 @@ export function useCreatePost() {
const res = await fetch(`${API_URL}/api/v1/posts`, {
method: "POST",
headers: {
"Content-Type": "application/json",
// Content-Type НЕ указываем — браузер сам добавит multipart/form-data с boundary
"Authorization": `Bearer ${token}`,
},
body: JSON.stringify(adData),
body: formData, // ← передаём FormData
});
const data = await res.json();
const data = await res.json(); // ← объявляем ПОСЛЕ await res
if (!res.ok) {
throw new Error(data.message || "Ошибка при создании объявления");
}
setSuccess(true);
return data; // возвращает созданное объявление
return data;
} catch (e) {
setError(e.message);
return null;