import React, { FormEvent, useState } from 'react'; import { useHistory } from 'react-router-dom'; import { Button, Paper } from '@material-ui/core'; import { makeStyles, Theme } from '@material-ui/core/styles'; import BugTitleInput from '../../components/BugTitleForm/BugTitleInput'; import CommentInput from '../../components/CommentInput/CommentInput'; import { useNewBugMutation } from './NewBug.generated'; /** * Css in JS styles */ const useStyles = makeStyles((theme: Theme) => ({ main: { maxWidth: 800, margin: 'auto', marginTop: theme.spacing(4), marginBottom: theme.spacing(4), padding: theme.spacing(2), overflow: 'hidden', }, form: { display: 'flex', flexDirection: 'column', }, actions: { display: 'flex', justifyContent: 'flex-end', }, greenButton: { backgroundColor: theme.palette.success.main, color: theme.palette.success.contrastText, '&:hover': { backgroundColor: theme.palette.success.dark, color: theme.palette.primary.contrastText, }, }, })); /** * Form to create a new issue */ function NewBugPage() { const [newBug, { loading, error }] = useNewBugMutation(); const [issueTitle, setIssueTitle] = useState(''); const [issueComment, setIssueComment] = useState(''); const classes = useStyles(); let issueTitleInput: any; let history = useHistory(); function submitNewIssue(e: FormEvent) { e.preventDefault(); if (!isFormValid()) return; newBug({ variables: { input: { title: issueTitle, message: issueComment, }, }, }).then(function (data) { const id = data.data?.newBug.bug.id; history.push('/bug/' + id); }); issueTitleInput.value = ''; } function isFormValid() { return issueTitle.length > 0; } if (loading) return
Loading...
; if (error) return
Error
; return (
{ issueTitleInput = node; }} label="Title" variant="outlined" fullWidth margin="dense" onChange={(event: any) => setIssueTitle(event.target.value)} /> setIssueComment(comment)} />
); } export default NewBugPage;