diff --git a/src/components/PostModal.jsx b/src/components/PostModal.jsx
index 1b03ae4..d37b895 100644
--- a/src/components/PostModal.jsx
+++ b/src/components/PostModal.jsx
@@ -1,19 +1,17 @@
import {useCallback, useState} from "react";
-import {API_URL} from "../config/constants";
import PinIcon from "./icons/PinIcon";
import CloseIcon from "./icons/CloseIcon";
import { EMPTY_FORM, CATEGORIES } from "../config/feedConstants";
+import { useCreatePost } from "../hooks/useCreatePost";
export default function PostModal({ onClose, onSuccess }) {
const [form, setForm] = useState(EMPTY_FORM);
const [errors, setErrors] = useState({});
- const [loading, setLoading] = useState(false);
- const [apiError, setApiError] = useState("");
+ const { createAd, loading, error: apiError } = useCreatePost();
const set = useCallback((k, v) => {
setForm(p => ({ ...p, [k]: v }));
setErrors(p => ({ ...p, [k]: "" }));
- setApiError("");
}, []);
const validate = () => {
@@ -28,25 +26,20 @@ export default function PostModal({ onClose, onSuccess }) {
const handleSubmit = async () => {
if (!validate()) return;
- setLoading(true);
- setApiError("");
- try {
- const res = await fetch(`${API_URL}/api/ads`, {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify(form),
- });
- if (!res.ok) {
- const data = await res.json().catch(() => ({}));
- throw new Error(data.message || `Ошибка ${res.status}`);
- }
- const newAd = await res.json();
+
+ const payload = {
+ type: form.type, // "loss" | "found"
+ title: form.title.trim(),
+ description: form.description.trim(),
+ category: form.category,
+ date: form.date || null,
+ phone: form.phone.trim(),
+ };
+
+ const newAd = await createAd(payload);
+ if (newAd) {
onSuccess(newAd);
onClose();
- } catch (err) {
- setApiError(err.message || "Не удалось разместить объявление");
- } finally {
- setLoading(false);
}
};
@@ -107,15 +100,7 @@ export default function PostModal({ onClose, onSuccess }) {
{errors.category &&
{errors.category}
}
-
-
- set("date", e.target.value)}
- />
-
+
diff --git a/src/hooks/useCreatePost.js b/src/hooks/useCreatePost.js
new file mode 100644
index 0000000..ca2be25
--- /dev/null
+++ b/src/hooks/useCreatePost.js
@@ -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 };
+}
\ No newline at end of file
diff --git a/src/pages/FeedPage.jsx b/src/pages/FeedPage.jsx
index 2e097ed..434581d 100644
--- a/src/pages/FeedPage.jsx
+++ b/src/pages/FeedPage.jsx
@@ -7,7 +7,6 @@ import AdCard from "../components/AdCard";
import PostModal from "../components/PostModal";
import MenuIcon from "../components/icons/MenuIcon"
-// ─── FEED PAGE ────────────────────────────────────────────────────────────────
export default function FeedPage() {
const [sidebarOpen, setSidebarOpen] = useState(false);
const [activeNav, setActiveNav] = useState("Главная");
@@ -17,13 +16,12 @@ export default function FeedPage() {
const [loading, setLoading] = useState(true);
const [error, setError] = useState("");
- // Загрузка объявлений с бэка
useEffect(() => {
const load = async () => {
setLoading(true);
setError("");
try {
- const res = await fetch(`${API_URL}/api/ads`);
+ const res = await fetch(`${API_URL}/api/v1/posts`);
if (!res.ok) {
throw new Error(`Ошибка ${res.status}`);
}