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

219 lines
6.8 KiB
TypeScript
Raw Normal View History

2023-10-06 19:28:30 +00:00
import { useState } 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-29 11:21:20 +00:00
2023-08-18 11:16:56 +00:00
2023-10-17 15:20:45 +00:00
import type { DroppableProvided } from "react-beautiful-dnd";
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-10-17 15:20:45 +00:00
provided: DroppableProvided;
2023-09-28 12:29:48 +00:00
additionalContent?: ReactNode;
additionalMobile?: 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-10-17 15:20:45 +00:00
provided,
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
2023-10-29 11:21:20 +00:00
console.log(provided)
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);
};
2023-10-29 11:21:20 +00:00
2023-08-18 11:16:56 +00:00
const addNewAnswer = () => {
const answerNew = variants.slice();
2023-10-29 11:21:20 +00:00
answerNew.push({ answer: "", extendedText: "", hints: "" });
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
});
};
2023-10-29 11:21:20 +00:00
2023-08-18 11:16:56 +00:00
return (
<Draggable draggableId={String(index)} index={index}>
2023-10-29 11:21:20 +00:00
{(provided) => (
<Box ref={provided.innerRef} {...provided.draggableProps}>
<FormControl
key={index}
2023-10-17 15:20:45 +00:00
fullWidth
2023-10-29 11:21:20 +00:00
variant="standard"
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-10-17 15:20:45 +00:00
}}
2023-10-29 11:21:20 +00:00
>
<TextField
defaultValue={variant.answer}
2023-08-18 11:16:56 +00:00
fullWidth
2023-10-29 11:21:20 +00:00
focused={false}
placeholder={"Добавьте ответ"}
multiline={question.content.largeCheck}
onChange={({ target }) => debounced(target.value)}
onKeyDown={(event: KeyboardEvent<HTMLInputElement>) => {
if (event.code === "Enter" && !question.content.largeCheck) {
addNewAnswer();
}
2023-09-25 10:57:58 +00:00
}}
2023-10-29 11:21:20 +00:00
InputProps={{
startAdornment: (
<>
<InputAdornment
{...provided.dragHandleProps}
position="start"
>
<PointsIcon
style={{ color: "#9A9AAF", fontSize: "30px" }}
/>
2023-08-18 11:16:56 +00:00
</InputAdornment>
2023-10-29 11:21:20 +00:00
{additionalContent}
</>
),
endAdornment: (
<InputAdornment position="end">
<IconButton
sx={{ padding: "0" }}
aria-describedby="my-popover-id"
onClick={handleClick}
>
<MessageIcon
style={{
color: "#9A9AAF",
fontSize: "30px",
marginRight: "6.5px",
}}
/>
</IconButton>
<Popover
id="my-popover-id"
open={isOpen}
anchorEl={anchorEl}
onClose={handleClose}
anchorOrigin={{ vertical: "bottom", horizontal: "left" }}
>
<TextareaAutosize
style={{ margin: "10px" }}
placeholder="Подсказка для этого ответа"
value={variant.hints}
onChange={changeAnswerHint}
onKeyDown={(
event: KeyboardEvent<HTMLTextAreaElement>
) => event.stopPropagation()}
/>
</Popover>
<IconButton sx={{ padding: "0" }} onClick={deleteAnswer}>
<DeleteIcon
style={{
color: theme.palette.grey2.main,
marginRight: "-1px",
}}
/>
</IconButton>
2023-10-17 15:20:45 +00:00
</InputAdornment>
2023-10-29 11:21:20 +00:00
),
}}
sx={{
"& .MuiInputBase-root": {
padding: additionalContent ? "5px 13px" : "13px",
borderRadius: "10px",
background: "#ffffff",
"& input.MuiInputBase-input": {
height: "22px",
},
"& textarea.MuiInputBase-input": {
marginTop: "1px",
},
"& .MuiOutlinedInput-notchedOutline": {
border: "none",
2023-08-18 11:16:56 +00:00
},
2023-10-17 15:20:45 +00:00
},
2023-10-29 11:21:20 +00:00
}}
inputProps={{
sx: { fontSize: "18px", lineHeight: "21px", py: 0, ml: "13px" },
2023-11-03 13:10:47 +00:00
"data-cy": "quiz-variant-question-answer",
2023-10-29 11:21:20 +00:00
}}
/>
{additionalMobile}
</FormControl>
</Box>
)}
</Draggable>
2023-08-18 11:16:56 +00:00
);
};