frontPanel/src/pages/Questions/OptionsAndPicture/OptionsAndPicture.tsx

282 lines
8.8 KiB
TypeScript
Raw Normal View History

2023-10-01 12:48:39 +00:00
import { useState } from "react";
2023-09-20 17:39:17 +00:00
import {
2023-10-17 10:53:19 +00:00
Box,
Link,
Typography,
useTheme,
useMediaQuery,
InputAdornment,
IconButton,
Button,
Popover,
TextareaAutosize,
TextField,
2023-09-20 17:39:17 +00:00
} from "@mui/material";
2023-09-06 13:20:12 +00:00
import { useParams } from "react-router-dom";
2023-10-01 12:48:39 +00:00
import { AnswerDraggableList } from "../AnswerDraggableList";
import { CropModal } from "@ui_kit/Modal/CropModal";
import { UploadImageModal } from "../UploadImage/UploadImageModal";
import AddImage from "@icons/questionsPage/addImage";
import Image from "@icons/questionsPage/image";
2023-09-25 13:43:15 +00:00
import { EnterIcon } from "../../../assets/icons/questionsPage/enterIcon";
import ButtonsOptionsAndPict from "../ButtonsOptionsAndPict";
import SwitchOptionsAndPict from "./switchOptionsAndPict";
import React from "react";
2023-09-05 16:16:34 +00:00
import { questionStore, updateQuestionsList } from "@root/questions";
2023-09-21 07:00:08 +00:00
import { ImageAddIcons } from "@icons/ImageAddIcons";
2023-09-25 13:43:15 +00:00
import { PointsIcon } from "@icons/questionsPage/PointsIcon";
import { MessageIcon } from "@icons/messagIcon";
import { DeleteIcon } from "@icons/questionsPage/deleteIcon";
2023-10-01 12:48:39 +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 { QuizQuestionVarImg } from "../../../model/questionTypes/varimg";
2023-10-12 15:20:46 +00:00
import { produce } from "immer";
2023-10-03 14:03:57 +00:00
interface Props {
2023-10-17 10:53:19 +00:00
totalIndex: number;
}
2023-08-12 08:31:21 +00:00
export default function OptionsAndPicture({ totalIndex }: Props) {
2023-10-17 10:53:19 +00:00
const [open, setOpen] = useState(false);
const [opened, setOpened] = useState<boolean>(false);
const [switchState, setSwitchState] = useState("setting");
const [currentIndex, setCurrentIndex] = useState<number>(0);
const quizId = Number(useParams().quizId);
const { listQuestions } = questionStore();
const theme = useTheme();
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
const isMobile = useMediaQuery(theme.breakpoints.down(790));
const question = listQuestions[quizId][totalIndex] as QuizQuestionVarImg;
2023-09-15 12:37:12 +00:00
2023-10-17 10:53:19 +00:00
const SSHC = (data: string) => {
setSwitchState(data);
};
2023-09-05 16:16:34 +00:00
2023-10-17 10:53:19 +00:00
const uploadImage = (files: FileList | null) => {
if (files?.length) {
const [file] = Array.from(files);
2023-10-01 12:48:39 +00:00
2023-10-17 10:53:19 +00:00
const clonedContent = { ...question.content };
clonedContent.variants[currentIndex].extendedText =
URL.createObjectURL(file);
updateQuestionsList<QuizQuestionVarImg>(quizId, totalIndex, {
content: clonedContent,
});
2023-10-01 12:48:39 +00:00
2023-10-17 10:53:19 +00:00
setOpen(false);
setOpened(true);
}
};
2023-10-01 12:48:39 +00:00
2023-10-17 10:53:19 +00:00
return (
<>
<Box sx={{ pl: "20px", pr: "20px" }}>
<AnswerDraggableList
variants={question.content.variants}
totalIndex={totalIndex}
additionalContent={(variant, index) => (
<>
{!isMobile && (
2023-09-25 13:43:15 +00:00
<Box
2023-10-17 10:53:19 +00:00
sx={{ cursor: "pointer" }}
onClick={() => {
setCurrentIndex(index);
setOpen(true);
}}
2023-10-12 15:20:46 +00:00
>
2023-10-17 10:53:19 +00:00
{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
2023-09-25 13:43:15 +00:00
sx={{
2023-10-17 10:53:19 +00:00
height: "40px",
width: "60px",
margin: "0 10px",
2023-10-01 12:48:39 +00:00
}}
2023-10-17 10:53:19 +00:00
/>
</Button>
)}
2023-10-12 15:20:46 +00:00
</Box>
2023-10-17 10:53:19 +00:00
)}
</>
)}
additionalMobile={(variant, index) => (
<>
{isMobile && (
2023-09-21 07:00:08 +00:00
<Box
2023-10-17 10:53:19 +00:00
onClick={() => {
setCurrentIndex(index);
setOpen(true);
}}
sx={{
overflow: "hidden",
display: "flex",
alignItems: "center",
m: "8px",
position: "relative",
borderRadius: "3px",
}}
>
<Box
2023-10-12 15:20:46 +00:00
sx={{
2023-10-17 10:53:19 +00:00
width: "100%",
background: "#EEE4FC",
height: "40px",
display: "flex",
alignItems: "center",
justifyContent: "center",
2023-10-12 15:20:46 +00:00
}}
2023-10-17 10:53:19 +00:00
>
{variant.extendedText ? (
<Box
2023-10-12 15:20:46 +00:00
sx={{
2023-10-17 10:53:19 +00:00
overflow: "hidden",
width: "40px",
display: "flex",
alignItems: "center",
background: "#EEE4FC",
height: "30px",
borderRadius: "3px",
2023-10-12 15:20:46 +00:00
}}
2023-10-17 10:53:19 +00:00
>
<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>
2023-10-12 15:20:46 +00:00
)}
2023-10-17 10:53:19 +00:00
</Box>
<Box
sx={{
display: "flex",
justifyContent: "center",
alignItems: "center",
width: "20px",
background: "#EEE4FC",
height: "40px",
color: "white",
backgroundColor: "#7E2AEA",
}}
>
+
</Box>
2023-09-21 07:00:08 +00:00
</Box>
2023-10-17 10:53:19 +00:00
)}
</>
)}
/>
<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<QuizQuestionVarImg>(quizId, totalIndex, {
content,
});
}}
/>
<Box
sx={{
display: "flex",
alignItems: "center",
marginBottom: "17px",
}}
>
<Link
component="button"
variant="body2"
sx={{
color: theme.palette.brightPurple.main,
fontWeight: "400",
fontSize: "16px",
mr: "4px",
height: "19px",
}}
onClick={() => {
const clonedContent = { ...question.content };
clonedContent.variants.push({
answer: "",
extendedText: "",
});
updateQuestionsList<QuizQuestionVarImg>(quizId, totalIndex, {
content: clonedContent,
});
}}
>
Добавьте ответ
</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",
}}
/>
</>
)}
</Box>
</Box>
<ButtonsOptionsAndPict
switchState={switchState}
SSHC={SSHC}
totalIndex={totalIndex}
/>
<SwitchOptionsAndPict switchState={switchState} totalIndex={totalIndex} />
</>
);
2023-08-12 08:31:21 +00:00
}