aboutsummaryrefslogtreecommitdiffstats
path: root/webui/src/pages/new/NewBugPage.tsx
blob: f313ac24e7c3b513e913e29632a69b5fba3ac280 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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 BackToListButton from '../../components/BackToListButton';
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: 1200,
    margin: 'auto',
    marginTop: theme.spacing(4),
    marginBottom: theme.spacing(4),
    padding: theme.spacing(2),
  },
  container: {
    display: 'flex',
    marginBottom: theme.spacing(1),
    marginRight: theme.spacing(2),
    marginLeft: theme.spacing(2),
  },
  form: {
    display: 'flex',
    flexDirection: 'column',
  },
  actions: {
    display: 'flex',
    justifyContent: 'flex-end',
  },
  greenButton: {
    backgroundColor: theme.palette.success.main,
    color: theme.palette.success.contrastText,
  },
  leftSidebar: {
    marginTop: theme.spacing(4),
    marginRight: theme.spacing(2),
  },
  rightSidebar: {
    marginTop: theme.spacing(2),
    flex: '0 0 200px',
  },
  timeline: {
    flex: 1,
    marginTop: theme.spacing(2),
    marginRight: theme.spacing(2),
    minWidth: 400,
    padding: theme.spacing(1),
  },
}));

/**
 * 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.humanId;
      history.push('/bug/' + id);
    });
    issueTitleInput.value = '';
  }

  function isFormValid() {
    return issueTitle.length > 0;
  }

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error</div>;

  return (
    <main className={classes.main}>
      <div className={classes.container}>
        <div className={classes.leftSidebar}>
          <BackToListButton />
        </div>
        <Paper className={classes.timeline}>
          <form className={classes.form} onSubmit={submitNewIssue}>
            <BugTitleInput
              inputRef={(node) => {
                issueTitleInput = node;
              }}
              label="Title"
              variant="outlined"
              fullWidth
              margin="dense"
              onChange={(event: any) => setIssueTitle(event.target.value)}
            />
            <CommentInput
              loading={false}
              onChange={(comment: string) => setIssueComment(comment)}
            />
            <div className={classes.actions}>
              <Button
                className={classes.greenButton}
                variant="contained"
                type="submit"
                disabled={!isFormValid()}
              >
                Submit new issue
              </Button>
            </div>
          </form>
        </Paper>
        <div className={classes.rightSidebar} />
      </div>
    </main>
  );
}

export default NewBugPage;