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  },
 36}));
 37
 38/**
 39 * Form to create a new issue
 40 */
 41function NewBugPage() {
 42  const [newBug, { loading, error }] = useNewBugMutation();
 43  const [issueTitle, setIssueTitle] = useState('');
 44  const [issueComment, setIssueComment] = useState('');
 45  const classes = useStyles();
 46  let issueTitleInput: any;
 47
 48  function submitNewIssue(e: FormEvent) {
 49    e.preventDefault();
 50    if (!isFormValid()) return;
 51    newBug({
 52      variables: {
 53        input: {
 54          title: issueTitle,
 55          message: issueComment,
 56        },
 57      },
 58    });
 59    issueTitleInput.value = '';
 60  }
 61
 62  function isFormValid() {
 63    return issueTitle.length > 0 && issueComment.length > 0 ? true : false;
 64  }
 65
 66  if (loading) return <div>Loading...</div>;
 67  if (error) return <div>Error</div>;
 68
 69  return (
 70    <Paper className={classes.main}>
 71      <form className={classes.form} onSubmit={submitNewIssue}>
 72        <BugTitleInput
 73          inputRef={(node) => {
 74            issueTitleInput = node;
 75          }}
 76          label="Title"
 77          variant="outlined"
 78          fullWidth
 79          margin="dense"
 80          onChange={(event: any) => setIssueTitle(event.target.value)}
 81        />
 82        <CommentInput
 83          loading={false}
 84          onChange={(comment: string) => setIssueComment(comment)}
 85        />
 86        <div className={classes.actions}>
 87          <Button
 88            className={classes.greenButton}
 89            variant="contained"
 90            type="submit"
 91            disabled={isFormValid() ? false : true}
 92          >
 93            Submit new issue
 94          </Button>
 95        </div>
 96      </form>
 97    </Paper>
 98  );
 99}
100
101export default NewBugPage;