import Modal from "react-modal"; import { useAuthContext } from "../context/auth_context"; import { useEffect, useState } from "react"; Modal.setAppElement("#root"); const BotButton = ({ onSave, givenBot }) => { const [open, setOpen] = useState(false); const [name, setName] = useState(givenBot?.name || ""); const [webhook, setWebhook] = useState(givenBot?.webhook || ""); const [errors, setErrors] = useState(null); const [isPublic, setIsPublic] = useState(givenBot?.public || false); const setDefaults = () => { setName(""); setWebhook(""); setErrors(null); }; const close = () => { if (!givenBot) { setDefaults(); } setOpen(false); }; const updateBot = () => { fetch(givenBot ? `/api/player/bots/${givenBot.id}` : "/api/player/bots", { credentials: "same-origin", method: givenBot ? "PUT" : "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ webhook: webhook.trim(), name: name.trim(), public: isPublic, }), }) .then((r) => r.json()) .then((d) => { if (d.success) { if (onSave) { onSave(); } close(); } else if (d.errors) { if (typeof d.errors === "object") { setErrors( Object.keys(d.errors).map( (field) => `${field}: ${d.errors[field].join(",")}` ) ); } else { setErrors([d.errors]); } } }); }; return (
Bot Name *
setName(e.target.value)} required />Webhook *
setWebhook(e.target.value)} required />Public *{" "} setIsPublic(!isPublic)} required />
{error}
))}Loading...
; return ( <>Looks like you've got no bots, try adding one!
)}