added edit card and get number
This commit is contained in:
28
src/hooks/useUpdatePost.js
Normal file
28
src/hooks/useUpdatePost.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import { useState } from "react";
|
||||
import { API_URL } from "../config/constants";
|
||||
|
||||
export function useUpdatePost() {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
const updateAd = async (id, payload) => {
|
||||
setLoading(true);
|
||||
setError("");
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/api/v1/posts/${id}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) throw new Error(`Ошибка ${res.status}`);
|
||||
return await res.json();
|
||||
} catch (err) {
|
||||
setError(err.message || "Не удалось сохранить");
|
||||
return null;
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return { updateAd, loading, error };
|
||||
}
|
||||
Reference in New Issue
Block a user