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