frontPanel/src/pages/Questions/OptionsPicture/OptionsPicture.tsx

252 lines
11 KiB
TypeScript
Raw Normal View History

2023-08-28 11:03:25 +00:00
import { useState } from "react";
2023-09-06 13:20:12 +00:00
import { useParams } from "react-router-dom";
2023-09-20 17:39:17 +00:00
import {
2023-10-12 15:20:46 +00:00
Box,
Link,
Typography,
Button,
useTheme,
useMediaQuery
2023-09-20 17:39:17 +00:00
} from "@mui/material";
2023-08-28 11:03:25 +00:00
import ButtonsOptions from "../ButtonsOptions";
2023-10-01 12:10:12 +00:00
import { AnswerDraggableList } from "../AnswerDraggableList";
import { CropModal } from "@ui_kit/Modal/CropModal";
import { UploadImageModal } from "../UploadImage/UploadImageModal";
2023-08-28 11:03:25 +00:00
import { questionStore, updateQuestionsList } from "@root/questions";
2023-09-25 13:43:15 +00:00
import { EnterIcon } from "../../../assets/icons/questionsPage/enterIcon";
import AddImage from "../../../assets/icons/questionsPage/addImage";
2023-10-01 12:10:12 +00:00
import Image from "../../../assets/icons/questionsPage/image";
import SwitchAnswerOptionsPict from "./switchOptionsPict";
2023-10-01 12:10:12 +00:00
import PlusImage from "@icons/questionsPage/plus";
2023-09-21 07:00:08 +00:00
2023-10-03 14:03:57 +00:00
import type { QuizQuestionImages } from "../../../model/questionTypes/images";
2023-10-12 15:20:46 +00:00
import { produce } from "immer";
2023-10-03 14:03:57 +00:00
2023-08-24 11:09:47 +00:00
interface Props {
2023-10-12 15:20:46 +00:00
totalIndex: number;
2023-08-24 11:09:47 +00:00
}
2023-08-28 11:03:25 +00:00
2023-08-24 11:09:47 +00:00
export default function OptionsPicture({ totalIndex }: Props) {
2023-10-12 15:20:46 +00:00
const [open, setOpen] = useState(false);
const [opened, setOpened] = useState<boolean>(false);
const [currentIndex, setCurrentIndex] = useState<number>(0);
const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down(790));
const isTablet = useMediaQuery(theme.breakpoints.down(790));
const quizId = Number(useParams().quizId);
const [switchState, setSwitchState] = useState("setting");
const { listQuestions } = questionStore();
const question = listQuestions[quizId][totalIndex] as QuizQuestionImages;
2023-08-28 11:03:25 +00:00
2023-10-12 15:20:46 +00:00
const SSHC = (data: string) => {
setSwitchState(data);
};
2023-10-12 15:20:46 +00:00
const uploadImage = (files: FileList | null) => {
if (files?.length) {
const [file] = Array.from(files);
2023-08-28 11:03:25 +00:00
2023-10-12 15:20:46 +00:00
const clonedContent = { ...question.content };
clonedContent.variants[currentIndex].extendedText =
URL.createObjectURL(file);
updateQuestionsList<QuizQuestionImages>(quizId, totalIndex, {
content: clonedContent,
});
2023-10-01 12:10:12 +00:00
2023-10-12 15:20:46 +00:00
setOpen(false);
setOpened(true);
}
};
2023-08-28 11:03:25 +00:00
2023-10-12 15:20:46 +00:00
const addNewAnswer = () => {
const answerNew = question.content.variants.slice();
answerNew.push({ answer: "", hints: "", extendedText: "" });
2023-08-28 11:03:25 +00:00
2023-10-12 15:20:46 +00:00
updateQuestionsList<QuizQuestionImages>(quizId, totalIndex, {
content: { ...question.content, variants: answerNew },
});
};
2023-08-28 11:03:25 +00:00
2023-10-12 15:20:46 +00:00
return (
<>
<Box sx={{ padding: "20px" }}>
<AnswerDraggableList
variants={question.content.variants}
totalIndex={totalIndex}
additionalContent={(variant, index) => (
<>
{!isMobile && (
<Box
sx={{ cursor: "pointer" }}
onClick={() => {
setCurrentIndex(index);
setOpen(true);
}}
>
{variant.extendedText ? (
<Box
sx={{
overflow: "hidden",
width: "60px",
display: "flex",
alignItems: "center",
background: "#EEE4FC",
borderRadius: "3px",
margin: "0 10px",
height: "40px",
}}
>
<Box sx={{ display: "flex", width: "40px" }}>
<img
src={variant.extendedText}
alt=""
style={{ width: "100%" }}
/>
</Box>
<PlusImage />
</Box>
) : (
<Button component="label" sx={{ padding: "0px" }}>
<AddImage
sx={{ height: "40px", width: "60px", margin: "0 10px" }}
/>
</Button>
)}
</Box>
)}
</>
2023-10-01 12:10:12 +00:00
)}
2023-10-12 15:20:46 +00:00
additionalMobile={(variant, index) => (
<>
{isMobile && (
<Box
onClick={() => {
setCurrentIndex(index);
setOpen(true);
}}
sx={{
overflow: "hidden",
display: "flex",
alignItems: "center",
m: "8px",
position: "relative",
borderRadius: "3px",
}}
>
<Box
sx={{
width: "100%",
background: "#EEE4FC",
height: "40px",
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
{variant.extendedText ? (
<Box
sx={{
overflow: "hidden",
width: "40px",
display: "flex",
alignItems: "center",
background: "#EEE4FC",
height: "30px",
borderRadius: "3px",
}}
>
<img
src={variant.extendedText}
alt=""
style={{ width: "100%" }}
/>
</Box>
) : (
<Button component="label" sx={{ padding: "0px" }}>
<Image
sx={{
height: "40px",
width: "60px",
margin: "0 10px",
}}
/>
</Button>
)}
</Box>
<Box
sx={{
display: "flex",
justifyContent: "center",
alignItems: "center",
width: "20px",
background: "#EEE4FC",
height: "40px",
color: "white",
backgroundColor: "#7E2AEA",
}}
>
+
</Box>
</Box>
)}
</>
)}
/>
<UploadImageModal
open={open}
onClose={() => setOpen(false)}
imgHC={uploadImage}
/>
<CropModal
opened={opened}
onClose={() => setOpened(false)}
picture={question.content.variants[currentIndex]?.extendedText}
onCropPress={url => {
const content = produce(question.content, draft => {
draft.variants[currentIndex].extendedText = url;
});
updateQuestionsList<QuizQuestionImages>(quizId, totalIndex, {
content,
});
2023-10-01 12:10:12 +00:00
}}
2023-10-12 15:20:46 +00:00
/>
<Box sx={{ display: "flex", alignItems: "center", gap: "10px" }}>
<Link
component="button"
variant="body2"
sx={{ color: theme.palette.brightPurple.main }}
onClick={addNewAnswer}
>
Добавьте ответ
</Link>
{isMobile ? null : (
<>
<Typography
sx={{
fontWeight: 400,
lineHeight: "21.33px",
color: theme.palette.grey2.main,
fontSize: "16px",
}}
>
или нажмите Enter
</Typography>
<EnterIcon
style={{
color: "#7E2AEA",
fontSize: "24px",
marginLeft: "6px",
}}
/>
</>
)}
2023-09-21 07:00:08 +00:00
</Box>
2023-10-12 15:20:46 +00:00
</Box>
<ButtonsOptions switchState={switchState} SSHC={SSHC} totalIndex={totalIndex} />
<SwitchAnswerOptionsPict switchState={switchState} totalIndex={totalIndex} />
</>
);
2023-08-24 11:09:47 +00:00
}