frontPanel/src/pages/Questions/AnswerDraggableList/AnswerItem.tsx

214 lines
6.9 KiB
TypeScript
Raw Normal View History

2023-09-21 10:07:30 +00:00
import { useState, useRef } from "react";
2023-09-06 13:20:12 +00:00
import { useParams } from "react-router-dom";
2023-08-18 11:16:56 +00:00
import { Draggable } from "react-beautiful-dnd";
2023-09-18 09:07:13 +00:00
import {
Box,
TextField,
FormControl,
InputAdornment,
IconButton,
2023-09-21 10:07:30 +00:00
Popover,
2023-09-18 09:07:13 +00:00
useTheme,
useMediaQuery,
} from "@mui/material";
2023-09-20 09:07:33 +00:00
import { useDebouncedCallback } from "use-debounce";
2023-08-18 11:16:56 +00:00
import { questionStore, updateQuestionsList } from "@root/questions";
2023-09-25 13:43:15 +00:00
import { PointsIcon } from "@icons/questionsPage/PointsIcon";
import { DeleteIcon } from "@icons/questionsPage/deleteIcon";
import { MessageIcon } from "@icons/messagIcon";
2023-08-18 11:16:56 +00:00
import TextareaAutosize from "@mui/base/TextareaAutosize";
2023-09-28 12:29:48 +00:00
import type { ChangeEvent, KeyboardEvent, ReactNode } from "react";
2023-10-04 09:07:59 +00:00
import type { QuizQuestionVariant } from "../../../model/questionTypes/variant";
import type { QuestionVariant } from "../../../model/questionTypes/shared";
2023-08-18 11:16:56 +00:00
type AnswerItemProps = {
index: number;
totalIndex: number;
2023-10-04 09:07:59 +00:00
variants: QuestionVariant[];
variant: QuestionVariant;
2023-09-28 12:29:48 +00:00
additionalContent?: ReactNode;
additionalMobile?: ReactNode;
2023-09-06 07:30:27 +00:00
icon?: ReactNode;
2023-08-18 11:16:56 +00:00
};
2023-09-18 09:07:13 +00:00
export const AnswerItem = ({
index,
totalIndex,
variants,
variant,
2023-09-28 12:29:48 +00:00
additionalContent,
additionalMobile,
2023-09-18 09:07:13 +00:00
}: AnswerItemProps) => {
2023-09-06 13:20:12 +00:00
const quizId = Number(useParams().quizId);
2023-08-18 11:16:56 +00:00
const { listQuestions } = questionStore();
const theme = useTheme();
2023-10-04 09:07:59 +00:00
const question = listQuestions[quizId][totalIndex] as QuizQuestionVariant;
2023-09-25 10:57:58 +00:00
const isTablet = useMediaQuery(theme.breakpoints.down(790));
2023-09-20 09:07:33 +00:00
const debounced = useDebouncedCallback((value) => {
const answerNew = variants.slice();
answerNew[index].answer = value;
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionVariant>(quizId, totalIndex, {
2023-09-20 09:07:33 +00:00
content: {
2023-10-04 09:07:59 +00:00
...question.content,
2023-09-20 09:07:33 +00:00
variants: answerNew,
},
});
}, 1000);
2023-08-18 11:16:56 +00:00
const [isOpen, setIsOpen] = useState(false);
const [anchorEl, setAnchorEl] = useState<HTMLButtonElement | null>(null);
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
setIsOpen(true);
};
const handleClose = () => {
setIsOpen(false);
};
const addNewAnswer = () => {
const answerNew = variants.slice();
2023-10-04 09:07:59 +00:00
answerNew.push({ answer: "", hints: "", extendedText: "" });
2023-08-18 11:16:56 +00:00
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionVariant>(quizId, totalIndex, {
2023-10-04 09:07:59 +00:00
content: { ...question.content, variants: answerNew },
2023-08-18 11:16:56 +00:00
});
};
const deleteAnswer = () => {
const answerNew = variants.slice();
answerNew.splice(index, 1);
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionVariant>(quizId, totalIndex, {
2023-10-04 09:07:59 +00:00
content: { ...question.content, variants: answerNew },
2023-08-18 11:16:56 +00:00
});
};
const changeAnswerHint = (event: ChangeEvent<HTMLTextAreaElement>) => {
const answerNew = variants.slice();
answerNew[index].hints = event.target.value;
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionVariant>(quizId, totalIndex, {
2023-10-04 09:07:59 +00:00
content: { ...question.content, variants: answerNew },
2023-08-18 11:16:56 +00:00
});
};
return (
<Draggable draggableId={String(index)} index={index}>
{(provided) => (
2023-08-18 15:39:41 +00:00
<Box ref={provided.innerRef} {...provided.draggableProps}>
2023-08-18 11:16:56 +00:00
<FormControl
key={index}
fullWidth
variant="standard"
2023-09-25 10:57:58 +00:00
sx={{
margin: isTablet ? " 15px 0 20px 0" : "0 0 20px 0",
borderRadius: "10px",
border: "1px solid rgba(0, 0, 0, 0.23)",
background: "white",
}}
2023-08-18 11:16:56 +00:00
>
<TextField
2023-09-20 09:07:33 +00:00
defaultValue={variant.answer}
2023-08-18 11:16:56 +00:00
fullWidth
2023-08-18 15:44:29 +00:00
focused={false}
2023-08-18 11:16:56 +00:00
placeholder={"Добавьте ответ"}
2023-10-04 09:07:59 +00:00
multiline={question.content.largeCheck}
2023-09-20 09:07:33 +00:00
onChange={({ target }) => debounced(target.value)}
2023-09-07 08:02:15 +00:00
onKeyDown={(event: KeyboardEvent<HTMLInputElement>) => {
2023-10-04 09:07:59 +00:00
if (event.code === "Enter" && !question.content.largeCheck) {
2023-09-07 08:02:15 +00:00
addNewAnswer();
}
}}
2023-08-18 11:16:56 +00:00
InputProps={{
startAdornment: (
2023-09-06 07:30:27 +00:00
<>
2023-09-18 09:07:13 +00:00
<InputAdornment
{...provided.dragHandleProps}
position="start"
>
2023-10-01 12:14:34 +00:00
<PointsIcon
style={{ color: "#9A9AAF", fontSize: "30px" }}
/>
2023-09-06 07:30:27 +00:00
</InputAdornment>
2023-09-28 12:29:48 +00:00
{additionalContent}
2023-09-06 07:30:27 +00:00
</>
2023-08-18 11:16:56 +00:00
),
endAdornment: (
<InputAdornment position="end">
2023-10-01 12:14:34 +00:00
<IconButton
sx={{ padding: "0" }}
aria-describedby="my-popover-id"
onClick={handleClick}
>
<MessageIcon
style={{
color: "#9A9AAF",
fontSize: "30px",
marginRight: "6.5px",
}}
/>
2023-08-18 11:16:56 +00:00
</IconButton>
<Popover
open={isOpen}
anchorEl={anchorEl}
onClose={handleClose}
2023-09-25 10:57:58 +00:00
anchorOrigin={{
vertical: "bottom",
horizontal: "left",
}}
2023-08-18 11:16:56 +00:00
>
<TextareaAutosize
style={{ margin: "10px" }}
placeholder="Подсказка для этого ответа"
value={variant.hints}
onChange={changeAnswerHint}
2023-09-18 09:07:13 +00:00
onKeyDown={(
event: KeyboardEvent<HTMLTextAreaElement>
) => event.stopPropagation()}
2023-08-18 11:16:56 +00:00
/>
</Popover>
2023-09-25 13:43:15 +00:00
<IconButton sx={{ padding: "0" }} onClick={deleteAnswer}>
2023-10-01 12:14:34 +00:00
<DeleteIcon
style={{
color: theme.palette.grey2.main,
marginRight: "-1px",
}}
/>
2023-08-18 11:16:56 +00:00
</IconButton>
</InputAdornment>
),
}}
sx={{
"& .MuiInputBase-root": {
2023-10-05 14:03:54 +00:00
padding: additionalContent ? "5px 13px" : "13px",
2023-08-18 11:16:56 +00:00
borderRadius: "10px",
background: "#ffffff",
2023-10-04 09:07:59 +00:00
"& input.MuiInputBase-input": {
height: "22px",
},
"& textarea.MuiInputBase-input": {
marginTop: "1px",
},
2023-09-25 10:57:58 +00:00
"& .MuiOutlinedInput-notchedOutline": {
border: "none",
},
2023-08-18 11:16:56 +00:00
},
}}
inputProps={{
2023-09-25 13:43:15 +00:00
sx: { fontSize: "18px", lineHeight: "21px", py: 0, ml: "13px" },
2023-08-18 11:16:56 +00:00
}}
/>
2023-09-28 12:29:48 +00:00
{additionalMobile}
2023-08-18 11:16:56 +00:00
</FormControl>
2023-08-18 15:39:41 +00:00
</Box>
2023-08-18 11:16:56 +00:00
)}
</Draggable>
);
};