2023-09-06 13:20:12 +00:00
|
|
|
|
import { useParams } from "react-router-dom";
|
2023-09-05 13:54:41 +00:00
|
|
|
|
import { useState } from "react";
|
|
|
|
|
import { Typography, Box, useTheme, ButtonBase } from "@mui/material";
|
|
|
|
|
import UploadBox from "@ui_kit/UploadBox";
|
2023-09-13 14:14:27 +00:00
|
|
|
|
import { CropModal } from "@ui_kit/Modal/CropModal";
|
2023-09-05 13:54:41 +00:00
|
|
|
|
import UploadIcon from "../../../assets/icons/UploadIcon";
|
|
|
|
|
import * as React from "react";
|
|
|
|
|
import { questionStore, updateQuestionsList } from "@root/questions";
|
|
|
|
|
import { UploadImageModal } from "./UploadImageModal";
|
|
|
|
|
|
|
|
|
|
import type { DragEvent } from "react";
|
2023-10-03 14:03:57 +00:00
|
|
|
|
import type { QuizQuestionImages } from "../../../model/questionTypes/images";
|
2023-09-05 13:54:41 +00:00
|
|
|
|
|
|
|
|
|
type UploadImageProps = {
|
|
|
|
|
totalIndex: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function UploadImage({ totalIndex }: UploadImageProps) {
|
2023-09-06 13:20:12 +00:00
|
|
|
|
const quizId = Number(useParams().quizId);
|
2023-09-05 13:54:41 +00:00
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const [open, setOpen] = React.useState(false);
|
|
|
|
|
const { listQuestions } = questionStore();
|
2023-10-03 14:03:57 +00:00
|
|
|
|
const question = listQuestions[quizId][totalIndex] as QuizQuestionImages;
|
2023-09-05 13:54:41 +00:00
|
|
|
|
|
|
|
|
|
const handleOpen = () => setOpen(true);
|
|
|
|
|
const handleClose = () => setOpen(false);
|
|
|
|
|
const imgHC = (files: FileList | null) => {
|
|
|
|
|
if (files?.length) {
|
|
|
|
|
const [file] = Array.from(files);
|
|
|
|
|
|
2023-10-03 14:03:57 +00:00
|
|
|
|
updateQuestionsList(quizId, totalIndex, {
|
|
|
|
|
...question,
|
|
|
|
|
back: URL.createObjectURL(file),
|
|
|
|
|
});
|
2023-09-05 13:54:41 +00:00
|
|
|
|
|
|
|
|
|
handleClose();
|
|
|
|
|
setOpened(true);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const [opened, setOpened] = useState<boolean>(false);
|
|
|
|
|
|
|
|
|
|
const handleDrop = (event: DragEvent<HTMLDivElement>) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
|
|
imgHC(event.dataTransfer.files);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Box sx={{ padding: "20px" }}>
|
|
|
|
|
<Typography
|
|
|
|
|
sx={{
|
|
|
|
|
fontWeight: 500,
|
|
|
|
|
color: theme.palette.grey3.main,
|
|
|
|
|
mt: "11px",
|
|
|
|
|
mb: "14px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Загрузить изображение
|
|
|
|
|
</Typography>
|
|
|
|
|
<ButtonBase
|
|
|
|
|
onClick={handleOpen}
|
|
|
|
|
sx={{ width: "100%", maxWidth: "260px" }}
|
|
|
|
|
>
|
|
|
|
|
<UploadBox
|
|
|
|
|
handleDrop={handleDrop}
|
|
|
|
|
sx={{ maxWidth: "260px" }}
|
|
|
|
|
icon={<UploadIcon />}
|
|
|
|
|
text="5 MB максимум"
|
|
|
|
|
/>
|
|
|
|
|
</ButtonBase>
|
|
|
|
|
<UploadImageModal open={open} onClose={handleClose} imgHC={imgHC} />
|
2023-09-13 14:14:27 +00:00
|
|
|
|
<CropModal
|
2023-09-05 13:54:41 +00:00
|
|
|
|
opened={opened}
|
|
|
|
|
onClose={() => setOpened(false)}
|
2023-10-03 14:03:57 +00:00
|
|
|
|
picture={question.content.back}
|
2023-09-05 13:54:41 +00:00
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|