All files / app/components/Comments NewComment.tsx

0% Statements 0/16
0% Branches 0/4
0% Functions 0/4
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                             
import { Box, Button, Grid, Link } from "@material-ui/core";
import Markdown from "components/Markdown";
import TextField from "components/TextField";
import React, { useState } from "react";
import makeStyles from "utils/makeStyles";
 
const useStyles = makeStyles({
  reverseAlignment: {
    textAlign: "end",
  },
});
 
interface NewCommentProps {
  onComment: (comment: string) => Promise<void>;
}
 
export default function NewComment({ onComment }: NewCommentProps) {
  const classes = useStyles();
  const [preview, setPreview] = useState(false);
  const [comment, setComment] = useState("");
 
  const handleSubmit = async () => {
    await onComment(comment);
    setComment("");
  };
 
  return (
    <>
      <p>Write a comment:</p>
      <Grid container spacing={2}>
        <Grid item xs={12} md={preview ? 6 : 12}>
          <TextField
            id="new-comment"
            label="Text field"
            maxRows={5}
            multiline
            fullWidth
            onChange={(e) => setComment(e.target.value)}
            value={comment}
            margin="normal"
          />
          <Box className={classes.reverseAlignment}>
            <Button component={Link} href="https://www.markdowntutorial.com/">
              Formatting?
            </Button>
            <Button component={Link} onClick={() => setPreview(!preview)}>
              Preview?
            </Button>
          </Box>
        </Grid>
        {preview && (
          <Grid item xs={12} md={6}>
            <Markdown source={comment} />
          </Grid>
        )}
      </Grid>
      <Button onClick={handleSubmit} type="submit">
        Comment
      </Button>
    </>
  );
}