added image for posts
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -5,15 +5,21 @@ export function useUpdatePost() {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
const updateAd = async (id, payload) => {
|
||||
const updateAd = async (id, formData) => {
|
||||
setLoading(true);
|
||||
setError("");
|
||||
try {
|
||||
const token = localStorage.getItem("token");
|
||||
|
||||
const res = await fetch(`${API_URL}/api/v1/posts/${id}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
headers: {
|
||||
// Content-Type НЕ указываем — браузер сам выставит multipart/form-data
|
||||
"Authorization": `Bearer ${token}`,
|
||||
},
|
||||
body: formData, // FormData
|
||||
});
|
||||
|
||||
if (!res.ok) throw new Error(`Ошибка ${res.status}`);
|
||||
return await res.json();
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user