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

197 lines
6.3 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-08-18 11:16:56 +00:00
import type { Variants } from "@root/questions";
type AnswerItemProps = {
index: number;
totalIndex: number;
variants: Variants[];
variant: Variants;
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-29 04:24:06 +00:00
icon
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-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;
updateQuestionsList(quizId, totalIndex, {
content: {
...listQuestions[quizId][totalIndex].content,
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-09-21 10:07:30 +00:00
answerNew.push({ answer: "", hints: "", emoji: "" });
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-29 04:24:06 +00:00
sx={{ padding: isTablet ? " 20px 0 20px 0" : "0 0 18px 0" }}
// 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-09-07 08:02:15 +00:00
multiline={listQuestions[quizId][totalIndex].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-09-25 13:43:15 +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-25 13:43:15 +00:00
<InputAdornment {...provided.dragHandleProps} position="start">
<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-09-25 13:43:15 +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-25 13:43:15 +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}>
<DeleteIcon style={{ color: theme.palette.grey2.main, marginRight: "-1px" }} />
2023-08-18 11:16:56 +00:00
</IconButton>
</InputAdornment>
),
}}
sx={{
2023-09-28 12:29:48 +00:00
padding: isTablet ? "10px 0px" : 0,
2023-08-18 11:16:56 +00:00
"& .MuiInputBase-root": {
2023-09-25 13:43:15 +00:00
padding: icon ? "5px 13.5px" : "13.5px 13.5px 13.5px 9px",
borderRadius: "8px",
height: "48px",
2023-08-18 11:16:56 +00:00
background: "#ffffff",
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>
);
};