2023-12-16 14:55:56 +00:00
|
|
|
|
import {
|
2024-01-07 19:32:15 +00:00
|
|
|
|
Box,
|
|
|
|
|
Typography,
|
|
|
|
|
ButtonBase,
|
|
|
|
|
useTheme,
|
|
|
|
|
IconButton, useMediaQuery,
|
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";
|
|
|
|
|
|
|
|
|
|
import type { ChangeEvent } from "react";
|
|
|
|
|
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";
|
|
|
|
|
import { sendFile } from "@api/quizRelase";
|
2024-01-19 11:46:17 +00:00
|
|
|
|
import { useQuestionsStore } from "@stores/quizData/store"
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
|
|
type FileProps = {
|
|
|
|
|
currentQuestion: QuizQuestionFile;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const UPLOAD_FILE_DESCRIPTIONS_MAP: Record<
|
|
|
|
|
UploadFileType,
|
|
|
|
|
{ title: string; description: string }
|
|
|
|
|
> = {
|
|
|
|
|
all: { title: "Добавить файл", description: "Принимает любые файлы" },
|
|
|
|
|
picture: {
|
|
|
|
|
title: "Добавить изображение",
|
|
|
|
|
description: "Принимает изображения",
|
|
|
|
|
},
|
|
|
|
|
video: {
|
|
|
|
|
title: "Добавить видео",
|
|
|
|
|
description: "Принимает .mp4 и .mov формат — максимум 100мб",
|
|
|
|
|
},
|
|
|
|
|
audio: { title: "Добавить аудиофайл", description: "Принимает аудиофайлы" },
|
|
|
|
|
document: { title: "Добавить документ", description: "Принимает документы" },
|
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
|
|
export const File = ({ currentQuestion }: FileProps) => {
|
2023-12-17 21:28:57 +00:00
|
|
|
|
const { settings } = useQuestionsStore()
|
2023-12-16 14:55:56 +00:00
|
|
|
|
const { answers } = useQuizViewStore();
|
|
|
|
|
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;
|
|
|
|
|
const theme = useTheme();
|
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>) => {
|
2023-12-16 14:55:56 +00:00
|
|
|
|
const file = target.files?.[0];
|
|
|
|
|
if (file) {
|
2023-12-17 21:28:57 +00:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
await sendFile({
|
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
|
body: {
|
|
|
|
|
file: `${file.name}|${URL.createObjectURL(file)}`,
|
|
|
|
|
name: file.name
|
|
|
|
|
},
|
|
|
|
|
//@ts-ignore
|
|
|
|
|
qid: settings.qid
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
updateAnswer(
|
|
|
|
|
currentQuestion.id,
|
|
|
|
|
`${file.name}|${URL.createObjectURL(file)}`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
enqueueSnackbar("ответ не был засчитан")
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-16 14:55:56 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Box>
|
2023-12-29 00:58:19 +00:00
|
|
|
|
<Typography variant="h5" color={theme.palette.text.primary}>{currentQuestion.title}</Typography>
|
2023-12-16 14:55:56 +00:00
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
width: "100%",
|
|
|
|
|
marginTop: "20px",
|
|
|
|
|
maxWidth: answer?.split("|")[0] ? "640px" : "550px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{answer?.split("|")[0] && (
|
|
|
|
|
<Box sx={{ display: "flex", alignItems: "center", gap: "15px" }}>
|
2023-12-29 00:58:19 +00:00
|
|
|
|
<Typography color={theme.palette.text.primary}>Вы загрузили:</Typography>
|
2023-12-16 14:55:56 +00:00
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
padding: "5px 5px 5px 16px",
|
2023-12-29 00:58:19 +00:00
|
|
|
|
backgroundColor: theme.palette.primary.main,
|
2023-12-16 14:55:56 +00:00
|
|
|
|
borderRadius: "8px",
|
|
|
|
|
color: "#FFFFFF",
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
2024-01-07 19:32:15 +00:00
|
|
|
|
overflow: "hidden",
|
2023-12-16 14:55:56 +00:00
|
|
|
|
gap: "15px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-01-07 19:32:15 +00:00
|
|
|
|
<Typography
|
|
|
|
|
sx={{
|
|
|
|
|
whiteSpace: "nowrap",
|
|
|
|
|
textOverflow: "ellipsis",
|
|
|
|
|
overflow: "hidden",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{answer?.split("|")[0]}</Typography>
|
2023-12-16 14:55:56 +00:00
|
|
|
|
<IconButton
|
|
|
|
|
sx={{ p: 0 }}
|
|
|
|
|
onClick={() => {
|
2023-12-29 00:58:19 +00:00
|
|
|
|
updateAnswer(currentQuestion.id, "");
|
2023-12-16 14:55:56 +00:00
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<CloseBold />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{!answer?.split("|")[0] && (
|
|
|
|
|
<ButtonBase component="label" sx={{ justifyContent: "flex-start" }}>
|
|
|
|
|
<input
|
|
|
|
|
onChange={uploadFile}
|
|
|
|
|
hidden
|
|
|
|
|
accept={UPLOAD_FILE_TYPES_MAP[currentQuestion.content.type]}
|
|
|
|
|
multiple
|
|
|
|
|
type="file"
|
|
|
|
|
/>
|
|
|
|
|
<Box
|
|
|
|
|
onDragOver={(event: DragEvent<HTMLDivElement>) =>
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
}
|
|
|
|
|
sx={{
|
|
|
|
|
width: "100%",
|
2024-01-07 19:32:15 +00:00
|
|
|
|
height: isMobile ? undefined : "120px",
|
2023-12-16 14:55:56 +00:00
|
|
|
|
display: "flex",
|
|
|
|
|
gap: "50px",
|
|
|
|
|
justifyContent: "flex-start",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
padding: "33px 44px 33px 55px",
|
|
|
|
|
backgroundColor: theme.palette.background.default,
|
2023-12-29 00:58:19 +00:00
|
|
|
|
border: `1px solid #9A9AAF`,
|
|
|
|
|
// border: `1px solid ${theme.palette.grey2.main}`,
|
2023-12-16 14:55:56 +00:00
|
|
|
|
borderRadius: "8px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<UploadIcon />
|
|
|
|
|
<Box>
|
|
|
|
|
<Typography
|
|
|
|
|
sx={{
|
2023-12-29 00:58:19 +00:00
|
|
|
|
color: "#9A9AAF",
|
|
|
|
|
// color: theme.palette.grey2.main,
|
2023-12-16 14:55:56 +00:00
|
|
|
|
fontWeight: 500,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{
|
|
|
|
|
UPLOAD_FILE_DESCRIPTIONS_MAP[currentQuestion.content.type]
|
|
|
|
|
.title
|
|
|
|
|
}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography
|
|
|
|
|
sx={{
|
2023-12-29 00:58:19 +00:00
|
|
|
|
color: "#9A9AAF",
|
|
|
|
|
// color: theme.palette.grey2.main,
|
2023-12-16 14:55:56 +00:00
|
|
|
|
fontSize: "16px",
|
|
|
|
|
lineHeight: "19px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{
|
|
|
|
|
UPLOAD_FILE_DESCRIPTIONS_MAP[currentQuestion.content.type]
|
|
|
|
|
.description
|
|
|
|
|
}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
</ButtonBase>
|
|
|
|
|
)}
|
|
|
|
|
{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>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
2023-12-29 00:58:19 +00:00
|
|
|
|
|
2023-12-16 14:55:56 +00:00
|
|
|
|
};
|