doc: add methods to handleSubmit function
This commit is contained in:
1 parent
9f899183b7
commit
684b39c4fc
1 file changed
+29
-1
@@ -17,7 +17,35 @@ const CreatePost = () => {
|
||||
const [generatingImg, setGeneratingImg] = useState();
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const handleSubmit = () => {};
|
||||
const handleSubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (form.prompt && form.photo) {
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
"https://dalle-image-generator-server.onrender.com/api/v1/posts",
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ ...form }),
|
||||
}
|
||||
);
|
||||
|
||||
await response.json();
|
||||
navigate("/");
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
} else {
|
||||
alert("Please enter a prompt and generate a image");
|
||||
}
|
||||
};
|
||||
|
||||
const handleChange = (e) => {
|
||||
setForm({ ...form, [e.target.name]: e.target.value });
|
||||
|
||||
Reference in new issue
Block a user