import { useState } from "react"; import { useNavigate } from "react-router"; import { useLocation } from "react-router"; export default function CreatePostPage() { const location = useLocation(); const stateParentId = location.state?.parentId; const [postTitle, setPostTitle] = useState(""); const [postContent, setPostContent] = useState(""); const [parentId, setParentId] = useState(stateParentId || ""); const navigate = useNavigate(); console.log(stateParentId); console.log(parentId); async function createPost() { if (parentId) { const response = await fetch(`http://localhost:8787/posts/${parentId}/reply`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ title: postTitle, content: postContent // god bless HTTPS }), credentials: 'include' }); if (!response.ok) { throw new Error(`Got ${response.status} when trying to log in.`) } navigate("/"); } else { const response = await fetch("http://localhost:8787/posts/add", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ title: postTitle, content: postContent // god bless HTTPS }), credentials: 'include' }); if (!response.ok) { throw new Error(`Got ${response.status} when trying to log in.`) } navigate("/"); } } return (