EditCommentForm.tsx

  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?: () => 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    console.log('submit: ' + message + '\nTo: ' + comment.id);
 58    editComment({
 59      variables: {
 60        input: {
 61          prefix: bug.id,
 62          message: message,
 63          target: comment.id,
 64        },
 65      },
 66    });
 67    resetForm();
 68    if (onPostSubmit) onPostSubmit();
 69  };
 70
 71  function resetForm() {
 72    setInputProp({
 73      value: '',
 74    });
 75  }
 76
 77  const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
 78    e.preventDefault();
 79    if (message.length > 0) submit();
 80  };
 81
 82  function getCancelButton() {
 83    return (
 84      <Button onClick={onCancelClick} variant="contained">
 85        Cancel
 86      </Button>
 87    );
 88  }
 89
 90  return (
 91    <Paper className={classes.container}>
 92      <form onSubmit={handleSubmit} ref={form}>
 93        <CommentInput
 94          inputProps={inputProp}
 95          loading={loading}
 96          onChange={(message: string) => setMessage(message)}
 97          inputText={comment.message}
 98        />
 99        <div className={classes.actions}>
100          {onCancelClick ? getCancelButton() : ''}
101          <Button
102            className={classes.greenButton}
103            variant="contained"
104            color="primary"
105            type="submit"
106            disabled={loading || message.length === 0}
107          >
108            Update Comment
109          </Button>
110        </div>
111      </form>
112    </Paper>
113  );
114}
115
116export default EditCommentForm;