added get and create posts with back

This commit is contained in:
SlimusMinus
2026-05-07 01:10:10 +03:00
parent 1c9c84fe0e
commit a5cdfdd8b4
3 changed files with 58 additions and 33 deletions

View File

@@ -0,0 +1,42 @@
import { useState } from "react";
import { API_URL } from "../config/constants";
export function useCreatePost() {
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const [success, setSuccess] = useState(false);
const createAd = async (adData) => {
setLoading(true);
setError(null);
try {
const token = localStorage.getItem("token");
const res = await fetch(`${API_URL}/api/v1/posts`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`,
},
body: JSON.stringify(adData),
});
const data = await res.json();
if (!res.ok) {
throw new Error(data.message || "Ошибка при создании объявления");
}
setSuccess(true);
return data; // возвращает созданное объявление
} catch (e) {
setError(e.message);
return null;
} finally {
setLoading(false);
}
};
return { createAd, loading, error, success };
}