All files / app/components/MarkdownInput UploadImage.tsx

100% Statements 18/18
100% Branches 0/0
100% Functions 3/3
100% Lines 18/18

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 64 65 66 67 68 69 70 71  7x           7x 7x 7x 7x 7x 7x 7x             7x               7x         7x 7x 7x 7x 1x       1x   7x                             11x                        
import { Emitter } from "@toast-ui/editor";
import Button from "components/Button";
import {
  Dialog,
  DialogActions,
  DialogContent,
  DialogTitle,
} from "components/Dialog";
import ImageInput from "components/ImageInput";
import TextField from "components/TextField";
import { useTranslation } from "i18n";
import { GLOBAL } from "i18n/namespaces";
import React, { useRef } from "react";
import { useForm } from "react-hook-form";
import { ImageInputValues } from "service/api";
 
import {
  IMAGE_DESCRIPTION,
  IMAGE_UPLOAD_INPUT_ALT,
  UPLOAD_IMAGE,
} from "./constants";
 
interface UploadImageProps {
  emitter?: Emitter;
  open: boolean;
  onClose: () => void;
}
 
export default function UploadImage({
  emitter,
  open,
  onClose,
}: UploadImageProps) {
  const { t } = useTranslation(GLOBAL);
  const alt = useRef("");
  const { control } = useForm();
  const handleSuccess = async (data: ImageInputValues) => {
    emitter?.emit("command", "addImage", {
      imageUrl: data.full_url,
      altText: alt.current,
    });
    onClose();
  };
  return (
    <Dialog aria-labelledby="upload-dialog-title" open={open} onClose={onClose}>
      <DialogTitle id="upload-dialog-title">{UPLOAD_IMAGE}</DialogTitle>
      <DialogContent>
        <ImageInput
          type="rect"
          id="image-upload-imput"
          alt={IMAGE_UPLOAD_INPUT_ALT}
          control={control}
          name="image"
          onSuccess={handleSuccess}
        />
        <TextField
          id="alt-textfield"
          label={IMAGE_DESCRIPTION}
          onChange={(e) => (alt.current = e.target.value)}
          fullWidth
        />
      </DialogContent>
      <DialogActions>
        <Button variant="outlined" onClick={onClose}>
          {t("cancel")}
        </Button>
      </DialogActions>
    </Dialog>
  );
}