import { SyntheticEvent, useState } from "react"; import { Link, useHistory } from "react-router-dom"; import Nav from "../../sections/Nav"; import { useAuth } from "../../useAuth"; import { authenticatedEndpoint } from "../../util"; export default function CreateOrganisation() { const auth = useAuth(); const router = useHistory(); if (!auth) { return <>; } const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [publicOrg, setPublicOrg] = useState(false); const createOrganisation = async (evt: SyntheticEvent) => { evt.preventDefault(); setError(""); setLoading(true); try { let res = await fetch(authenticatedEndpoint(auth, "organisations"), { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ name, description, public: publicOrg }), }); let json = await res.json(); if (json.error) { throw new Error(json.error); } setName(""); setDescription(""); router.push(`/crates/${name}`); } catch (e: any) { setError(e.message); } finally { setLoading(false); } }; return (