2023-12-16 14:55:56 +00:00
|
|
|
|
import {
|
2024-01-27 21:55:27 +00:00
|
|
|
|
Box,
|
|
|
|
|
Typography,
|
|
|
|
|
ButtonBase,
|
|
|
|
|
useTheme,
|
|
|
|
|
IconButton, useMediaQuery, Modal,
|
2023-12-16 14:55:56 +00:00
|
|
|
|
} from "@mui/material";
|
2024-01-19 11:46:17 +00:00
|
|
|
|
import { useQuizViewStore, updateAnswer } from "@stores/quizView/store";
|
2023-12-17 18:15:59 +00:00
|
|
|
|
import { UPLOAD_FILE_TYPES_MAP } from "../tools/File";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
|
|
import UploadIcon from "@icons/UploadIcon";
|
|
|
|
|
import CloseBold from "@icons/CloseBold";
|
|
|
|
|
|
2024-01-27 21:55:27 +00:00
|
|
|
|
import { useState, type ChangeEvent } from "react";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
import type { QuizQuestionFile } from "../../../model/questionTypes/file";
|
|
|
|
|
import type { DragEvent } from "react";
|
|
|
|
|
import type { UploadFileType } from "@model/questionTypes/file";
|
2023-12-17 21:28:57 +00:00
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
2024-01-23 18:00:06 +00:00
|
|
|
|
import { sendAnswer, sendFile } from "@api/quizRelase";
|
2024-01-19 11:46:17 +00:00
|
|
|
|
import { useQuestionsStore } from "@stores/quizData/store"
|
2024-01-27 21:55:27 +00:00
|
|
|
|
import Info from "@icons/Info";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
|
|
type FileProps = {
|
|
|
|
|
currentQuestion: QuizQuestionFile;
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-28 01:42:19 +00:00
|
|
|
|
const CurrentModal = ({ status }: { status: "errorType" | "errorSize" | "picture" | "video" | "audio" | "document" | "" }) => {
|
2024-01-27 21:55:27 +00:00
|
|
|
|
switch (status) {
|
2024-01-28 01:42:19 +00:00
|
|
|
|
case 'errorType':
|
|
|
|
|
return (<>
|
|
|
|
|
<Typography>Выбран некорректный тип файла</Typography>
|
|
|
|
|
</>)
|
|
|
|
|
case 'errorSize':
|
|
|
|
|
return (<>
|
|
|
|
|
<Typography>Файл слишком большой. Максимальный размер 50 МБ</Typography>
|
|
|
|
|
</>)
|
|
|
|
|
default:
|
|
|
|
|
return (<>
|
|
|
|
|
<Typography>Допустимые расширения файлов:</Typography>
|
|
|
|
|
<Typography>{
|
|
|
|
|
//@ts-ignore
|
|
|
|
|
ACCEPT_SEND_FILE_TYPES_MAP[status].join(" ")}</Typography>
|
|
|
|
|
</>)
|
2024-01-27 21:55:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ACCEPT_SEND_FILE_TYPES_MAP = {
|
|
|
|
|
picture: [
|
|
|
|
|
".jpeg",
|
|
|
|
|
".jpg",
|
|
|
|
|
".png",
|
|
|
|
|
".ico",
|
|
|
|
|
".gif",
|
|
|
|
|
".tiff",
|
|
|
|
|
".webp",
|
|
|
|
|
".eps",
|
|
|
|
|
".svg"
|
|
|
|
|
],
|
|
|
|
|
video: [
|
|
|
|
|
".mp4",
|
|
|
|
|
".mov",
|
|
|
|
|
".wmv",
|
|
|
|
|
".avi",
|
|
|
|
|
".avchd",
|
|
|
|
|
".flv",
|
|
|
|
|
".f4v",
|
|
|
|
|
".swf",
|
|
|
|
|
".mkv",
|
|
|
|
|
".webm",
|
|
|
|
|
".mpeg-2"
|
|
|
|
|
],
|
|
|
|
|
audio: [
|
|
|
|
|
".aac",
|
|
|
|
|
".aiff",
|
|
|
|
|
".dsd",
|
|
|
|
|
".flac",
|
2024-01-28 01:42:19 +00:00
|
|
|
|
".mp3",
|
|
|
|
|
".mqa",
|
|
|
|
|
".ogg",
|
|
|
|
|
".wav",
|
|
|
|
|
".wma"
|
2024-01-27 21:55:27 +00:00
|
|
|
|
],
|
|
|
|
|
document: [
|
|
|
|
|
".doc",
|
|
|
|
|
".docx",
|
|
|
|
|
".dotx",
|
|
|
|
|
".rtf",
|
|
|
|
|
".odt",
|
|
|
|
|
".pdf",
|
|
|
|
|
".txt",
|
|
|
|
|
".xls",
|
|
|
|
|
".ppt",
|
|
|
|
|
".xlsx",
|
|
|
|
|
".pptx",
|
|
|
|
|
".pages",
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-01-30 16:49:33 +00:00
|
|
|
|
const UPLOAD_FILE_DESCRIPTIONS_MAP: Record<
|
2023-12-16 14:55:56 +00:00
|
|
|
|
UploadFileType,
|
|
|
|
|
{ title: string; description: string }
|
|
|
|
|
> = {
|
|
|
|
|
picture: {
|
|
|
|
|
title: "Добавить изображение",
|
|
|
|
|
description: "Принимает изображения",
|
|
|
|
|
},
|
|
|
|
|
video: {
|
|
|
|
|
title: "Добавить видео",
|
|
|
|
|
description: "Принимает .mp4 и .mov формат — максимум 100мб",
|
|
|
|
|
},
|
|
|
|
|
audio: { title: "Добавить аудиофайл", description: "Принимает аудиофайлы" },
|
|
|
|
|
document: { title: "Добавить документ", description: "Принимает документы" },
|
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
|
|
export const File = ({ currentQuestion }: FileProps) => {
|
2024-01-27 21:55:27 +00:00
|
|
|
|
const theme = useTheme();
|
2023-12-17 21:28:57 +00:00
|
|
|
|
const { settings } = useQuestionsStore()
|
2023-12-16 14:55:56 +00:00
|
|
|
|
const { answers } = useQuizViewStore();
|
2024-01-27 21:55:27 +00:00
|
|
|
|
|
2024-01-28 01:42:19 +00:00
|
|
|
|
const [statusModal, setStatusModal] = useState<"errorType" | "errorSize" | "picture" | "video" | "audio" | "document" | "">("")
|
2024-01-27 21:55:27 +00:00
|
|
|
|
|
2023-12-16 14:55:56 +00:00
|
|
|
|
const answer = answers.find(
|
2023-12-17 13:22:21 +00:00
|
|
|
|
({ questionId }) => questionId === currentQuestion.id
|
2023-12-16 14:55:56 +00:00
|
|
|
|
)?.answer as string;
|
2024-01-07 19:32:15 +00:00
|
|
|
|
const isMobile = useMediaQuery(theme.breakpoints.down(500));
|
2023-12-17 21:28:57 +00:00
|
|
|
|
const uploadFile = async ({ target }: ChangeEvent<HTMLInputElement>) => {
|
2024-01-30 16:49:33 +00:00
|
|
|
|
if (!settings) return;
|
|
|
|
|
|
2023-12-16 14:55:56 +00:00
|
|
|
|
const file = target.files?.[0];
|
|
|
|
|
if (file) {
|
2024-01-28 01:42:19 +00:00
|
|
|
|
if (file.size <= 52428800) {
|
|
|
|
|
//проверяем на соответствие
|
|
|
|
|
console.log(file.name.toLowerCase())
|
|
|
|
|
if (ACCEPT_SEND_FILE_TYPES_MAP[currentQuestion.content.type].find((ednding => {
|
|
|
|
|
console.log(ednding)
|
|
|
|
|
console.log(file.name.toLowerCase().endsWith(ednding))
|
|
|
|
|
return file.name.toLowerCase().endsWith(ednding)
|
|
|
|
|
}))) {
|
2024-01-27 21:55:27 +00:00
|
|
|
|
|
2024-01-28 01:42:19 +00:00
|
|
|
|
//Нужный формат
|
|
|
|
|
console.log(file)
|
|
|
|
|
try {
|
2023-12-17 21:28:57 +00:00
|
|
|
|
|
2024-01-28 01:42:19 +00:00
|
|
|
|
const data = await sendFile({
|
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
|
body: {
|
|
|
|
|
file: file,
|
|
|
|
|
name: file.name
|
|
|
|
|
},
|
2024-01-31 12:57:07 +00:00
|
|
|
|
qid: settings.qid
|
2024-01-28 01:42:19 +00:00
|
|
|
|
})
|
|
|
|
|
console.log(data)
|
2023-12-17 21:28:57 +00:00
|
|
|
|
|
2024-01-28 01:42:19 +00:00
|
|
|
|
await sendAnswer({
|
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
|
//@ts-ignore
|
|
|
|
|
body: `https://storage.yandexcloud.net/squizanswer/${settings.qid}/${currentQuestion.id}/${data.data.fileIDMap[currentQuestion.id]}`,
|
|
|
|
|
//@ts-ignore
|
|
|
|
|
qid: settings.qid
|
|
|
|
|
})
|
2023-12-17 21:28:57 +00:00
|
|
|
|
|
2024-01-28 01:42:19 +00:00
|
|
|
|
updateAnswer(
|
|
|
|
|
currentQuestion.id,
|
|
|
|
|
`${file.name}|${URL.createObjectURL(file)}`
|
|
|
|
|
);
|
2023-12-17 21:28:57 +00:00
|
|
|
|
|
2024-01-28 01:42:19 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e)
|
|
|
|
|
enqueueSnackbar("ответ не был засчитан")
|
|
|
|
|
}
|
2024-01-27 21:55:27 +00:00
|
|
|
|
|
2024-01-28 01:42:19 +00:00
|
|
|
|
} else {
|
2024-01-27 21:55:27 +00:00
|
|
|
|
|
2024-01-28 01:42:19 +00:00
|
|
|
|
//неподходящий формат
|
|
|
|
|
setStatusModal("errorType")
|
|
|
|
|
}
|
2024-01-27 21:55:27 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
2024-01-28 01:42:19 +00:00
|
|
|
|
setStatusModal("errorSize")
|
2024-01-27 21:55:27 +00:00
|
|
|
|
}
|
2023-12-17 21:28:57 +00:00
|
|
|
|
|
2023-12-16 14:55:56 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2024-01-27 21:55:27 +00:00
|
|
|
|
<>
|
|
|
|
|
<Box>
|
|
|
|
|
<Typography variant="h5" color={theme.palette.text.primary}>{currentQuestion.title}</Typography>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
width: "100%",
|
|
|
|
|
marginTop: "20px",
|
2024-01-29 00:03:47 +00:00
|
|
|
|
maxWidth: answer?.split("|")[0] ? "640px" : "600px",
|
2024-01-27 21:55:27 +00:00
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{answer?.split("|")[0] && (
|
|
|
|
|
<Box sx={{ display: "flex", alignItems: "center", gap: "15px" }}>
|
|
|
|
|
<Typography color={theme.palette.text.primary}>Вы загрузили:</Typography>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
padding: "5px 5px 5px 16px",
|
|
|
|
|
backgroundColor: theme.palette.primary.main,
|
|
|
|
|
borderRadius: "8px",
|
|
|
|
|
color: "#FFFFFF",
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
overflow: "hidden",
|
|
|
|
|
gap: "15px",
|
2023-12-16 14:55:56 +00:00
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Typography
|
|
|
|
|
sx={{
|
2024-01-27 21:55:27 +00:00
|
|
|
|
whiteSpace: "nowrap",
|
|
|
|
|
textOverflow: "ellipsis",
|
|
|
|
|
overflow: "hidden",
|
2023-12-16 14:55:56 +00:00
|
|
|
|
}}
|
|
|
|
|
>
|
2024-01-27 21:55:27 +00:00
|
|
|
|
{answer?.split("|")[0]}</Typography>
|
|
|
|
|
<IconButton
|
|
|
|
|
sx={{ p: 0 }}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
updateAnswer(currentQuestion.id, "");
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<CloseBold />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{!answer?.split("|")[0] && (
|
|
|
|
|
<Box sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center"
|
|
|
|
|
}}>
|
2024-01-29 00:03:47 +00:00
|
|
|
|
<ButtonBase component="label" sx={{ justifyContent: "flex-start", width: "100%" }}>
|
2024-01-27 21:55:27 +00:00
|
|
|
|
<input
|
|
|
|
|
onChange={uploadFile}
|
|
|
|
|
hidden
|
2024-01-28 01:07:48 +00:00
|
|
|
|
accept={ACCEPT_SEND_FILE_TYPES_MAP[currentQuestion.content.type].join(",")}
|
2024-01-27 21:55:27 +00:00
|
|
|
|
multiple
|
|
|
|
|
type="file"
|
|
|
|
|
/>
|
|
|
|
|
<Box
|
|
|
|
|
onDragOver={(event: DragEvent<HTMLDivElement>) =>
|
|
|
|
|
event.preventDefault()
|
2023-12-16 14:55:56 +00:00
|
|
|
|
}
|
|
|
|
|
sx={{
|
2024-01-27 21:55:27 +00:00
|
|
|
|
width: "100%",
|
|
|
|
|
height: isMobile ? undefined : "120px",
|
|
|
|
|
display: "flex",
|
|
|
|
|
gap: "50px",
|
|
|
|
|
justifyContent: "flex-start",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
padding: "33px 44px 33px 55px",
|
|
|
|
|
backgroundColor: theme.palette.background.default,
|
|
|
|
|
border: `1px solid #9A9AAF`,
|
|
|
|
|
// border: `1px solid ${theme.palette.grey2.main}`,
|
|
|
|
|
borderRadius: "8px",
|
2023-12-16 14:55:56 +00:00
|
|
|
|
}}
|
|
|
|
|
>
|
2024-01-27 21:55:27 +00:00
|
|
|
|
<UploadIcon />
|
|
|
|
|
<Box>
|
|
|
|
|
<Typography
|
|
|
|
|
sx={{
|
|
|
|
|
color: "#9A9AAF",
|
|
|
|
|
// color: theme.palette.grey2.main,
|
|
|
|
|
fontWeight: 500,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{
|
|
|
|
|
UPLOAD_FILE_DESCRIPTIONS_MAP[currentQuestion.content.type]
|
|
|
|
|
.title
|
|
|
|
|
}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography
|
|
|
|
|
sx={{
|
|
|
|
|
color: "#9A9AAF",
|
|
|
|
|
// color: theme.palette.grey2.main,
|
|
|
|
|
fontSize: "16px",
|
|
|
|
|
lineHeight: "19px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{
|
|
|
|
|
UPLOAD_FILE_DESCRIPTIONS_MAP[currentQuestion.content.type]
|
|
|
|
|
.description
|
|
|
|
|
}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
</ButtonBase>
|
2024-01-29 00:03:47 +00:00
|
|
|
|
<Info sx={{ width: "40px", height: "40px" }} color={theme.palette.primary.main} onClick={() => setStatusModal(currentQuestion.content.type)} />
|
2023-12-16 14:55:56 +00:00
|
|
|
|
</Box>
|
2024-01-27 21:55:27 +00:00
|
|
|
|
)}
|
|
|
|
|
{answer && currentQuestion.content.type === "picture" && (
|
|
|
|
|
<img
|
|
|
|
|
src={answer.split("|")[1]}
|
|
|
|
|
alt=""
|
|
|
|
|
style={{
|
|
|
|
|
marginTop: "15px",
|
|
|
|
|
maxWidth: "300px",
|
|
|
|
|
maxHeight: "300px",
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{answer && currentQuestion.content.type === "video" && (
|
|
|
|
|
<video
|
|
|
|
|
src={answer.split("|")[1]}
|
|
|
|
|
style={{
|
|
|
|
|
marginTop: "15px",
|
|
|
|
|
maxWidth: "300px",
|
|
|
|
|
maxHeight: "300px",
|
|
|
|
|
objectFit: "cover",
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</Box>
|
2023-12-16 14:55:56 +00:00
|
|
|
|
</Box>
|
2024-01-27 21:55:27 +00:00
|
|
|
|
<Modal
|
|
|
|
|
open={Boolean(statusModal)}
|
2024-01-28 01:42:19 +00:00
|
|
|
|
onClose={() => setStatusModal("")}
|
2024-01-27 21:55:27 +00:00
|
|
|
|
>
|
|
|
|
|
<Box sx={{
|
2024-01-31 12:57:07 +00:00
|
|
|
|
position: 'absolute',
|
2024-01-27 21:55:27 +00:00
|
|
|
|
top: '50%',
|
|
|
|
|
left: '50%',
|
|
|
|
|
transform: 'translate(-50%, -50%)',
|
2024-01-29 00:03:47 +00:00
|
|
|
|
width: isMobile ? 300 : 400,
|
2024-01-27 21:55:27 +00:00
|
|
|
|
bgcolor: 'background.paper',
|
|
|
|
|
borderRadius: 3,
|
|
|
|
|
boxShadow: 24,
|
|
|
|
|
p: 4,
|
|
|
|
|
}}>
|
2024-01-28 01:42:19 +00:00
|
|
|
|
<CurrentModal status={statusModal} />
|
2024-01-27 21:55:27 +00:00
|
|
|
|
</Box>
|
|
|
|
|
</Modal>
|
|
|
|
|
</>
|
2023-12-16 14:55:56 +00:00
|
|
|
|
);
|
2023-12-29 00:58:19 +00:00
|
|
|
|
|
2023-12-16 14:55:56 +00:00
|
|
|
|
};
|