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: theme.palette.success.main,
34 color: theme.palette.success.contrastText,
35 '&:hover': {
36 backgroundColor: theme.palette.success.dark,
37 color: theme.palette.success.contrastText,
38 },
39 },
40}));
41
42type Props = {
43 bug: BugFragment;
44 comment: AddCommentFragment | CreateFragment;
45 onCancelClick?: () => void;
46 onPostSubmit?: (comments: any) => void;
47};
48
49function EditCommentForm({ bug, comment, onCancelClick, onPostSubmit }: Props) {
50 const [editComment, { loading }] = useEditCommentMutation();
51 const [message, setMessage] = useState<string>(comment.message);
52 const [inputProp, setInputProp] = useState<any>('');
53 const classes = useStyles({ loading });
54 const form = useRef<HTMLFormElement>(null);
55
56 const submit = () => {
57 editComment({
58 variables: {
59 input: {
60 prefix: bug.id,
61 message: message,
62 target: comment.id,
63 },
64 },
65 }).then((result) => {
66 const comments = result.data?.editComment.bug.timeline.comments as (
67 | AddCommentFragment
68 | CreateFragment
69 )[];
70 // NOTE Searching for the changed comment could be dropped if GraphQL get
71 // filter by id argument for timelineitems
72 const modifiedComment = comments.find((elem) => elem.id === comment.id);
73 if (onPostSubmit) onPostSubmit(modifiedComment);
74 });
75 resetForm();
76 };
77
78 function resetForm() {
79 setInputProp({
80 value: '',
81 });
82 }
83
84 const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
85 e.preventDefault();
86 if (message.length > 0) submit();
87 };
88
89 function getCancelButton() {
90 return (
91 <Button onClick={onCancelClick} variant="contained">
92 Cancel
93 </Button>
94 );
95 }
96
97 return (
98 <Paper className={classes.container}>
99 <form onSubmit={handleSubmit} ref={form}>
100 <CommentInput
101 inputProps={inputProp}
102 loading={loading}
103 onChange={(message: string) => setMessage(message)}
104 inputText={comment.message}
105 />
106 <div className={classes.actions}>
107 {onCancelClick ? getCancelButton() : ''}
108 <Button
109 className={classes.greenButton}
110 variant="contained"
111 color="primary"
112 type="submit"
113 disabled={loading || message.length === 0}
114 >
115 Update Comment
116 </Button>
117 </div>
118 </form>
119 </Paper>
120 );
121}
122
123export default EditCommentForm;