1import React, { useState, useRef } from 'react';
2
3import Button from '@material-ui/core/Button';
4import Paper from '@material-ui/core/Paper';
5import { makeStyles, Theme } from '@material-ui/core/styles';
6
7import CommentInput from '../../components/CommentInput/CommentInput';
8
9import { BugFragment } from './Bug.generated';
10import { useEditCommentMutation } from './EditCommentform.generated';
11import { AddCommentFragment } from './MessageCommentFragment.generated';
12import { CreateFragment } from './MessageCreateFragment.generated';
13
14type StyleProps = { loading: boolean };
15const useStyles = makeStyles<Theme, StyleProps>((theme) => ({
16 container: {
17 padding: theme.spacing(0, 2, 2, 2),
18 },
19 textarea: {},
20 tabContent: {
21 margin: theme.spacing(2, 0),
22 },
23 preview: {
24 borderBottom: `solid 3px ${theme.palette.grey['200']}`,
25 minHeight: '5rem',
26 },
27 actions: {
28 display: 'flex',
29 justifyContent: 'flex-end',
30 },
31 greenButton: {
32 marginLeft: '8px',
33 backgroundColor: '#2ea44fd9',
34 color: '#fff',
35 '&:hover': {
36 backgroundColor: '#2ea44f',
37 },
38 },
39}));
40
41type Props = {
42 bug: BugFragment;
43 comment: AddCommentFragment | CreateFragment;
44 onCancelClick?: () => void;
45 onPostSubmit?: () => void;
46};
47
48function EditCommentForm({ bug, comment, onCancelClick, onPostSubmit }: Props) {
49 const [editComment, { loading }] = useEditCommentMutation();
50 const [message, setMessage] = useState<string>(comment.message);
51 const [inputProp, setInputProp] = useState<any>('');
52 const classes = useStyles({ loading });
53 const form = useRef<HTMLFormElement>(null);
54
55 const submit = () => {
56 console.log('submit: ' + message + '\nTo: ' + comment.id);
57 editComment({
58 variables: {
59 input: {
60 prefix: bug.id,
61 message: message,
62 target: comment.id,
63 },
64 },
65 });
66 resetForm();
67 if (onPostSubmit) onPostSubmit();
68 };
69
70 function resetForm() {
71 setInputProp({
72 value: '',
73 });
74 }
75
76 const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
77 e.preventDefault();
78 if (message.length > 0) submit();
79 };
80
81 function getCancelButton() {
82 return (
83 <Button onClick={onCancelClick} variant="contained">
84 Cancel
85 </Button>
86 );
87 }
88
89 return (
90 <Paper className={classes.container}>
91 <form onSubmit={handleSubmit} ref={form}>
92 <CommentInput
93 inputProps={inputProp}
94 loading={loading}
95 onChange={(message: string) => setMessage(message)}
96 inputText={comment.message}
97 />
98 <div className={classes.actions}>
99 {onCancelClick ? getCancelButton() : ''}
100 <Button
101 className={classes.greenButton}
102 variant="contained"
103 color="primary"
104 type="submit"
105 disabled={loading || message.length === 0}
106 >
107 Update Comment
108 </Button>
109 </div>
110 </form>
111 </Paper>
112 );
113}
114
115export default EditCommentForm;