NewBugPage.tsx

  1import React, { FormEvent, useState } from 'react';
  2
  3import { Button } from '@material-ui/core';
  4import Paper from '@material-ui/core/Paper';
  5import TextField from '@material-ui/core/TextField/TextField';
  6import { fade, makeStyles, Theme } from '@material-ui/core/styles';
  7
  8import CommentInput from '../bug/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  titleInput: {
 25    borderRadius: theme.shape.borderRadius,
 26    borderColor: fade(theme.palette.primary.main, 0.2),
 27    borderStyle: 'solid',
 28    borderWidth: '1px',
 29    backgroundColor: fade(theme.palette.primary.main, 0.05),
 30    padding: theme.spacing(0, 0),
 31    transition: theme.transitions.create([
 32      'width',
 33      'borderColor',
 34      'backgroundColor',
 35    ]),
 36  },
 37  form: {
 38    display: 'flex',
 39    flexDirection: 'column',
 40  },
 41  actions: {
 42    display: 'flex',
 43    justifyContent: 'flex-end',
 44  },
 45}));
 46
 47/**
 48 * Form to create a new issue
 49 */
 50function NewBugPage() {
 51  const [newBug, { loading, error }] = useNewBugMutation();
 52  const [issueTitle, setIssueTitle] = useState('');
 53  const [issueComment, setIssueComment] = useState('');
 54  const classes = useStyles();
 55  let issueTitleInput: any;
 56
 57  function submitNewIssue(e: FormEvent) {
 58    e.preventDefault();
 59    if (!isFormValid()) return;
 60    console.log('submitNewISsue');
 61    console.log('title: ', issueTitle);
 62    console.log('comment: ', issueComment);
 63    newBug({
 64      variables: {
 65        input: {
 66          title: issueTitle,
 67          message: issueComment,
 68        },
 69      },
 70    });
 71    issueTitleInput.value = '';
 72  }
 73
 74  function isFormValid() {
 75    return issueTitle.length > 0 && issueComment.length > 0 ? true : false;
 76  }
 77
 78  if (loading) return <div>Loading</div>;
 79  if (error) return <div>Error</div>;
 80
 81  return (
 82    <Paper className={classes.main}>
 83      <form className={classes.form} onSubmit={submitNewIssue}>
 84        <TextField
 85          inputRef={(node) => {
 86            issueTitleInput = node;
 87          }}
 88          label="Title"
 89          className={classes.titleInput}
 90          variant="outlined"
 91          fullWidth
 92          margin="dense"
 93          onChange={(event: any) => setIssueTitle(event.target.value)}
 94        />
 95        <CommentInput
 96          loading={false}
 97          onChange={(comment: string) => setIssueComment(comment)}
 98        />
 99        <div className={classes.actions}>
100          <Button
101            variant="contained"
102            color="primary"
103            type="submit"
104            disabled={isFormValid() ? false : true}
105          >
106            Submit new issue
107          </Button>
108        </div>
109      </form>
110    </Paper>
111  );
112}
113
114export default NewBugPage;