import { useState } from "react"; import { Form, useActionData, useNavigate } from "react-router"; import { useLocation } from "react-router"; import { redirect, type ActionFunctionArgs } from "react-router"; // server-side action so we don't have to use clearnet for front-backend comms // praise cloudflare export async function action({ request, context }: ActionFunctionArgs) { const formData = await request.formData(); const env = context.cloudflare.env as { API: Fetcher }; const postTitle = formData.get("title") as string; const postContent = formData.get("content") as string; const parentId = formData.get("parentId") as string; // internal url uses ternary operator const path = parentId ? `/posts/${parentId}/reply` : "/posts/add"; const response = await env.API.fetch(new Request(`http://internal${path}`, { method: "POST", headers: { "Content-Type": "application/json", // forward cookies or auth won't work "Cookie": request.headers.get("Cookie") || "", }, body: JSON.stringify({ title: postTitle, content: postContent, }), })); if (!response.ok) { const error = await response.text(); return { error: `Failed to create post: ${response.status} - ${error}` }; } return redirect("/"); } export default function CreatePostPage() { const location = useLocation(); const stateParentId = location.state?.parentId; const [parentId, setParentId] = useState(stateParentId || ""); console.log(stateParentId); console.log(parentId); return (

Create Post