NewBugPage.tsx

  1import React, { FormEvent, useState } from 'react';
  2import { useHistory } from 'react-router-dom';
  3
  4import { Button, Paper } from '@material-ui/core';
  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
 51  let issueTitleInput: any;
 52  let history = useHistory();
 53
 54  function submitNewIssue(e: FormEvent) {
 55    e.preventDefault();
 56    if (!isFormValid()) return;
 57    newBug({
 58      variables: {
 59        input: {
 60          title: issueTitle,
 61          message: issueComment,
 62        },
 63      },
 64    }).then(function (data) {
 65      const id = data.data?.newBug.bug.humanId;
 66      history.push('/bug/' + id);
 67    });
 68    issueTitleInput.value = '';
 69  }
 70
 71  function isFormValid() {
 72    return issueTitle.length > 0;
 73  }
 74
 75  if (loading) return <div>Loading...</div>;
 76  if (error) return <div>Error</div>;
 77
 78  return (
 79    <Paper className={classes.main}>
 80      <form className={classes.form} onSubmit={submitNewIssue}>
 81        <BugTitleInput
 82          inputRef={(node) => {
 83            issueTitleInput = node;
 84          }}
 85          label="Title"
 86          variant="outlined"
 87          fullWidth
 88          margin="dense"
 89          onChange={(event: any) => setIssueTitle(event.target.value)}
 90        />
 91        <CommentInput
 92          loading={false}
 93          onChange={(comment: string) => setIssueComment(comment)}
 94        />
 95        <div className={classes.actions}>
 96          <Button
 97            className={classes.greenButton}
 98            variant="contained"
 99            type="submit"
100            disabled={isFormValid() ? false : true}
101          >
102            Submit new bug
103          </Button>
104        </div>
105      </form>
106    </Paper>
107  );
108}
109
110export default NewBugPage;