import React, { useState, useContext, useCallback, useRef, } from 'react'; import { Button, Container, FormControl, FormControlLabel, FormLabel, Link, Paper, Radio, RadioGroup, Stack, Typography, TextField, ToggleButtonGroup, ToggleButton, } from '@mui/material'; import LoadingContext from './loading-context'; import SnackbarContext from './snackbar-context'; import { Parsing } from './adf-details'; const PLACEHOLDER = `s(a). s(b). s(c). s(d). ac(a,c(v)). ac(b,b). ac(c,and(a,b)). ac(d,neg(b)).`; function AdfNewForm({ fetchProblems }: { fetchProblems: () => void; }) { const { setLoading } = useContext(LoadingContext); const { setStatus: setSnackbarInfo } = useContext(SnackbarContext); const [isFileUpload, setFileUpload] = useState(false); const [code, setCode] = useState(PLACEHOLDER); const [filename, setFilename] = useState(''); const [parsing, setParsing] = useState('Naive'); const [name, setName] = useState(''); const fileRef = useRef(null); const addAdf = useCallback( () => { setLoading(true); const formData = new FormData(); if (isFileUpload && fileRef.current) { const file = fileRef.current.files?.[0]; if (file) { formData.append('file', file); } } else { formData.append('code', code); } formData.append('parsing', parsing); formData.append('name', name); fetch(`${process.env.NODE_ENV === 'development' ? '//localhost:8080' : ''}/adf/add`, { method: 'POST', credentials: process.env.NODE_ENV === 'development' ? 'include' : 'same-origin', body: formData, }) .then((res) => { switch (res.status) { case 200: setSnackbarInfo({ message: 'Successfully added ADF problem!', severity: 'success', potentialUserChange: true }); fetchProblems(); break; default: setSnackbarInfo({ message: 'An error occured while adding the ADF problem.', severity: 'error', potentialUserChange: true }); break; } }) .finally(() => setLoading(false)); }, [isFileUpload, code, filename, parsing, name, fileRef.current], ); return ( Add a new Problem { setFileUpload(newValue); setFilename(''); }} > Write by Hand Upload File {isFileUpload ? ( ) : ( For more info on the syntax, have a look {' '} here . )} multiline fullWidth variant="filled" value={code} onChange={(event) => { setCode(event.target.value); }} /> )} Parsing Strategy setParsing(((e.target as HTMLInputElement).value) as Parsing)} > } label="Naive" /> } label="Hybrid" /> { setName(event.target.value); }} /> ); } export default AdfNewForm;