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

166 lines
5.4 KiB
TypeScript
Raw Normal View History

2023-08-18 11:16:56 +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-15 12:37:12 +00:00
import { Box, TextField, FormControl, InputAdornment, IconButton, useTheme, useMediaQuery } from "@mui/material";
2023-08-18 11:16:56 +00:00
import { questionStore, updateQuestionsList } from "@root/questions";
import PointsIcon from "@icons/questionsPage/PointsIcon";
import DeleteIcon from "@icons/questionsPage/deleteIcon";
import MessageIcon from "@icons/messagIcon";
import Popover from "@mui/material/Popover";
import TextareaAutosize from "@mui/base/TextareaAutosize";
import type { ChangeEvent, KeyboardEvent } from "react";
import type { Variants } from "@root/questions";
2023-09-06 07:30:27 +00:00
import { ReactNode } from "react";
2023-08-18 11:16:56 +00:00
type AnswerItemProps = {
index: number;
totalIndex: number;
variants: Variants[];
variant: Variants;
2023-09-06 07:30:27 +00:00
icon?: ReactNode;
2023-08-18 11:16:56 +00:00
};
2023-09-15 12:37:12 +00:00
export const AnswerItem = ({ index, totalIndex, variants, variant, icon }: 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-09-15 12:37:12 +00:00
const isMobile = useMediaQuery(theme.breakpoints.down(790));
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 onChangeText = (event: ChangeEvent<HTMLInputElement>) => {
const answerNew = variants.slice();
answerNew[index].answer = event.target.value;
2023-09-06 13:20:12 +00:00
updateQuestionsList(quizId, totalIndex, {
content: {
...listQuestions[quizId][totalIndex].content,
variants: answerNew,
},
2023-08-18 11:16:56 +00:00
});
};
const addNewAnswer = () => {
const answerNew = variants.slice();
2023-09-07 14:14:48 +00:00
answerNew.push({ answer: "", hints: "" });
2023-08-18 11:16:56 +00:00
2023-09-06 13:20:12 +00:00
updateQuestionsList(quizId, totalIndex, {
content: {
...listQuestions[quizId][totalIndex].content,
variants: answerNew,
},
2023-08-18 11:16:56 +00:00
});
};
const deleteAnswer = () => {
const answerNew = variants.slice();
answerNew.splice(index, 1);
2023-09-06 13:20:12 +00:00
updateQuestionsList(quizId, totalIndex, {
content: {
...listQuestions[quizId][totalIndex].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-09-06 13:20:12 +00:00
updateQuestionsList(quizId, totalIndex, {
content: {
...listQuestions[quizId][totalIndex].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-15 12:37:12 +00:00
sx={{ padding: isMobile ? " 15px 0 20px 0" : "0 0 20px 0" }}
2023-08-18 11:16:56 +00:00
>
<TextField
value={variant.answer}
fullWidth
2023-08-18 15:44:29 +00:00
focused={false}
2023-08-18 11:16:56 +00:00
placeholder={"Добавьте ответ"}
2023-09-07 08:02:15 +00:00
multiline={listQuestions[quizId][totalIndex].content.largeCheck}
2023-08-18 11:16:56 +00:00
onChange={onChangeText}
2023-09-07 08:02:15 +00:00
onKeyDown={(event: KeyboardEvent<HTMLInputElement>) => {
2023-09-15 12:37:12 +00:00
if (event.code === "Enter" && !listQuestions[quizId][totalIndex].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-15 12:37:12 +00:00
<InputAdornment {...provided.dragHandleProps} position="start">
2023-09-06 07:30:27 +00:00
<PointsIcon />
</InputAdornment>
{icon && icon}
</>
2023-08-18 11:16:56 +00:00
),
endAdornment: (
<InputAdornment position="end">
2023-09-15 12:37:12 +00:00
<IconButton aria-describedby="my-popover-id" onClick={handleClick}>
2023-08-18 11:16:56 +00:00
<MessageIcon />
</IconButton>
<Popover
id="my-popover-id"
open={isOpen}
anchorEl={anchorEl}
onClose={handleClose}
2023-08-18 15:39:41 +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-15 12:37:12 +00:00
onKeyDown={(event: KeyboardEvent<HTMLTextAreaElement>) => event.stopPropagation()}
2023-08-18 11:16:56 +00:00
/>
</Popover>
<IconButton onClick={deleteAnswer}>
<DeleteIcon color={theme.palette.grey2.main} />
</IconButton>
</InputAdornment>
),
}}
sx={{
"& .MuiInputBase-root": {
2023-09-08 13:42:52 +00:00
padding: icon ? "5px 13.5px" : "13.5px",
2023-08-18 11:16:56 +00:00
borderRadius: "10px",
background: "#ffffff",
},
}}
inputProps={{
sx: { fontSize: "18px", lineHeight: "21px", py: 0 },
}}
/>
</FormControl>
2023-08-18 15:39:41 +00:00
</Box>
2023-08-18 11:16:56 +00:00
)}
</Draggable>
);
};