import React, { useState, useEffect } from "react"; import { KEY } from "../../utils/helpers/keyCodes"; import { CodingContract, CodingContractTypes } from "../../CodingContracts"; import { CopyableText } from "./CopyableText"; import { Modal } from "./Modal"; import { EventEmitter } from "../../utils/EventEmitter"; import Typography from "@mui/material/Typography"; import TextField from "@mui/material/TextField"; import Button from "@mui/material/Button"; interface IProps { c: CodingContract; onClose: () => void; onAttempt: (answer: string) => void; } export const CodingContractEvent = new EventEmitter<[IProps]>(); export function CodingContractModal(): React.ReactElement { const [props, setProps] = useState(null); const [answer, setAnswer] = useState(""); useEffect(() => { CodingContractEvent.subscribe((props) => setProps(props)); }); if (props === null) return <>; function onChange(event: React.ChangeEvent): void { setAnswer(event.target.value); } function onKeyDown(event: React.KeyboardEvent): void { if (props === null) return; // React just won't cooperate on this one. // "React.KeyboardEvent" seems like the right type but // whatever ... const value = (event.target as any).value; if (event.keyCode === KEY.ENTER && value !== "") { event.preventDefault(); props.onAttempt(answer); close(); } } function close(): void { if (props === null) return; props.onClose(); setProps(null); } const contractType = CodingContractTypes[props.c.type]; const description = []; for (const [i, value] of contractType.desc(props.c.data).split("\n").entries()) description.push(" }}>); return ( You are attempting to solve a Coding Contract. You have {props.c.getMaxNumTries() - props.c.tries} tries remaining, after which the contract will self-destruct.
{description}
{ props.onAttempt(answer); close(); }} > Solve ), }} />
); }