NewBugPage.tsx

  1import React, { FormEvent, useState } from 'react';
  2
  3import { Button } from '@material-ui/core';
  4import Paper from '@material-ui/core/Paper';
  5import { makeStyles, Theme } from '@material-ui/core/styles';
  6
  7import BugTitleInput from '../../components/BugTitleForm/BugTitleInput';
  8import CommentInput from '../../components/CommentInput/CommentInput';
  9
 10import { useNewBugMutation } from './NewBug.generated';
 11
 12/**
 13 * Css in JS styles
 14 */
 15const useStyles = makeStyles((theme: Theme) => ({
 16  main: {
 17    maxWidth: 800,
 18    margin: 'auto',
 19    marginTop: theme.spacing(4),
 20    marginBottom: theme.spacing(4),
 21    padding: theme.spacing(2),
 22    overflow: 'hidden',
 23  },
 24  form: {
 25    display: 'flex',
 26    flexDirection: 'column',
 27  },
 28  actions: {
 29    display: 'flex',
 30    justifyContent: 'flex-end',
 31  },
 32  greenButton: {
 33    backgroundColor: theme.palette.success.main,
 34    color: theme.palette.success.contrastText,
 35    '&:hover': {
 36      backgroundColor: theme.palette.success.dark,
 37      color: theme.palette.primary.contrastText,
 38    },
 39  },
 40}));
 41
 42/**
 43 * Form to create a new issue
 44 */
 45function NewBugPage() {
 46  const [newBug, { loading, error }] = useNewBugMutation();
 47  const [issueTitle, setIssueTitle] = useState('');
 48  const [issueComment, setIssueComment] = useState('');
 49  const classes = useStyles();
 50  let issueTitleInput: any;
 51
 52  function submitNewIssue(e: FormEvent) {
 53    e.preventDefault();
 54    if (!isFormValid()) return;
 55    newBug({
 56      variables: {
 57        input: {
 58          title: issueTitle,
 59          message: issueComment,
 60        },
 61      },
 62    });
 63    issueTitleInput.value = '';
 64  }
 65
 66  function isFormValid() {
 67    return issueTitle.length > 0 && issueComment.length > 0 ? true : false;
 68  }
 69
 70  if (loading) return <div>Loading...</div>;
 71  if (error) return <div>Error</div>;
 72
 73  return (
 74    <Paper className={classes.main}>
 75      <form className={classes.form} onSubmit={submitNewIssue}>
 76        <BugTitleInput
 77          inputRef={(node) => {
 78            issueTitleInput = node;
 79          }}
 80          label="Title"
 81          variant="outlined"
 82          fullWidth
 83          margin="dense"
 84          onChange={(event: any) => setIssueTitle(event.target.value)}
 85        />
 86        <CommentInput
 87          loading={false}
 88          onChange={(comment: string) => setIssueComment(comment)}
 89        />
 90        <div className={classes.actions}>
 91          <Button
 92            className={classes.greenButton}
 93            variant="contained"
 94            type="submit"
 95            disabled={isFormValid() ? false : true}
 96          >
 97            Submit new bug
 98          </Button>
 99        </div>
100      </form>
101    </Paper>
102  );
103}
104
105export default NewBugPage;