import { Button, FormControl, FormErrorMessage, FormHelperText, FormLabel, Input, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay, } from "@chakra-ui/react"; import { t, Trans } from "@lingui/macro"; import { useRouter } from "next/router"; import { useState } from "react"; import { sendData } from "utils/postData"; import { makeNameUrlFriendly } from "utils/strings"; const CreateNewTeamModal = () => { const router = useRouter(); const [isOpen, setIsOpen] = useState(false); const [sending, setSending] = useState(false); const [buttonClicked, setButtonClicked] = useState(false); const [name, setName] = useState(""); const getError = () => { if (name.length < 2 || name.length > 32) { return t`Team name needs to be between 2 and 32 characters long.`; } if (/[^a-z0-9 ]/i.test(name) || name === "join") { return t`Team name can only contain letters and numbers.`; } return ""; }; const onClick = async () => { setButtonClicked(true); if (getError()) return; setSending(true); const success = await sendData("POST", "/api/teams", { name }); if (!success) return setSending(false); router.push(`/t/${makeNameUrlFriendly(name)}`); }; return ( <> {isOpen && ( setIsOpen(false)} size="xl"> Creating a new team Team name setName(e.target.value)} /> Name can't be changed after making the team so choose wisely. {getError()} )} ); }; export default CreateNewTeamModal;