frontPanel/src/pages/Questions/Emoji/Emoji.tsx

230 lines
7.5 KiB
TypeScript
Raw Normal View History

import { EmojiIcons } from "@icons/EmojiIocns";
import AddEmoji from "@icons/questionsPage/addEmoji";
import PlusImage from "@icons/questionsPage/plus";
2023-12-19 23:08:33 +00:00
import { Box, Link, Popover, Typography, useMediaQuery, useTheme } from "@mui/material";
2023-11-27 23:07:24 +00:00
import { addQuestionVariant, updateQuestion } from "@root/questions/actions";
import { EmojiPicker } from "@ui_kit/EmojiPicker";
import { useState } from "react";
2023-09-25 13:43:15 +00:00
import { EnterIcon } from "../../../assets/icons/questionsPage/enterIcon";
import type { QuizQuestionEmoji } from "../../../model/questionTypes/emoji";
import { AnswerDraggableList } from "../AnswerDraggableList";
import ButtonsOptions from "../ButtonsOptions";
import SwitchEmoji from "./switchEmoji";
2023-12-19 23:08:33 +00:00
import { useAddAnswer } from "../../../utils/hooks/useAddAnswer";
2023-10-02 19:43:07 +00:00
2023-08-24 11:09:47 +00:00
interface Props {
2023-12-19 23:08:33 +00:00
question: QuizQuestionEmoji;
2023-08-24 11:09:47 +00:00
}
2023-10-02 19:43:07 +00:00
export default function Emoji({ question }: Props) {
2023-12-19 23:08:33 +00:00
const [switchState, setSwitchState] = useState<string>("setting");
const onClickAddAnAnswer = useAddAnswer();
const [open, setOpen] = useState<boolean>(false);
const [anchorElement, setAnchorElement] = useState<HTMLDivElement | null>(null);
const [selectedVariant, setSelectedVariant] = useState<string | null>(null);
const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down(790));
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
2023-09-06 07:30:27 +00:00
2023-12-19 23:08:33 +00:00
const SSHC = (data: string) => {
setSwitchState(data);
};
2023-09-06 07:30:27 +00:00
2023-12-19 23:08:33 +00:00
return (
<>
<Box sx={{ padding: "20px" }}>
<AnswerDraggableList
question={question}
additionalContent={(variant) => (
<>
{!isTablet && (
<Box sx={{ cursor: "pointer" }}>
<Box
data-cy="choose-emoji-button"
onClick={({ currentTarget }) => {
setAnchorElement(currentTarget);
setSelectedVariant(variant.id);
setOpen(true);
2023-09-28 12:29:48 +00:00
}}
2023-12-19 23:08:33 +00:00
>
<Box
sx={{
display: "flex",
justifyContent: "center",
alignItems: "center",
gap: "5px",
}}
>
{variant.extendedText ? (
<Box
sx={{
height: "40px",
width: "60px",
display: "flex",
alignItems: "center",
justifyContent: "space-between",
background: "#EEE4FC",
borderRadius: "3px",
}}
>
<Box
sx={{
width: "100%",
display: "flex",
justifyContent: "center",
}}
>
{variant.extendedText}
</Box>
<Box>
<PlusImage />
</Box>
</Box>
) : (
<AddEmoji />
)}
</Box>
</Box>
</Box>
)}
</>
)}
additionalMobile={(variant) => (
<>
{isTablet && (
<Box
onClick={({ currentTarget }) => {
setAnchorElement(currentTarget);
setSelectedVariant(variant.id);
setOpen(true);
}}
sx={{
display: "flex",
alignItems: "center",
m: "8px",
position: "relative",
}}
>
<Box
2023-09-28 12:29:48 +00:00
sx={{
2023-12-19 23:08:33 +00:00
width: "100%",
background: "#EEE4FC",
height: "40px",
2023-09-28 12:29:48 +00:00
}}
2023-12-19 23:08:33 +00:00
/>
{variant.extendedText ? (
<Box
sx={{
position: "absolute",
color: "#7E2AEA",
fontSize: "20px",
left: "45%",
right: "55%",
}}
>
{variant.extendedText}
</Box>
) : (
<EmojiIcons
style={{
position: "absolute",
color: "#7E2AEA",
fontSize: "20px",
left: "45%",
right: "55%",
}}
2023-09-28 12:29:48 +00:00
/>
2023-12-19 23:08:33 +00:00
)}
<Box
2023-09-28 12:29:48 +00:00
sx={{
2023-12-19 23:08:33 +00:00
display: "flex",
justifyContent: "center",
alignItems: "center",
width: "20px",
background: "#EEE4FC",
height: "40px",
color: "white",
backgroundColor: "#7E2AEA",
2023-09-28 12:29:48 +00:00
}}
2023-12-19 23:08:33 +00:00
>
+
</Box>
2023-09-28 12:29:48 +00:00
</Box>
2023-12-19 23:08:33 +00:00
)}
</>
)}
/>
<Popover
open={open}
anchorEl={anchorElement}
onClick={(event) => event.stopPropagation()}
onClose={() => setOpen(false)}
anchorOrigin={{
vertical: "bottom",
horizontal: "right",
}}
sx={{
".MuiPaper-root.MuiPaper-rounded": {
borderRadius: "10px",
},
}}
>
<EmojiPicker
onEmojiSelect={({ native }) => {
setOpen(false);
updateQuestion(question.id, (question) => {
if (question.type !== "emoji") return;
const variant = question.content.variants.find((v) => v.id === selectedVariant);
if (!variant) return;
variant.extendedText = native;
});
}}
/>
</Popover>
<Box
sx={{
display: "flex",
alignItems: "center",
gap: "10px",
marginBottom: isMobile ? "17px" : "20px",
}}
>
<Link
component="button"
variant="body2"
sx={{ color: theme.palette.brightPurple.main }}
onClick={() => onClickAddAnAnswer(question)}
>
Добавьте ответ
</Link>
{!isTablet && (
<>
<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>
<ButtonsOptions switchState={switchState} SSHC={SSHC} question={question} />
<SwitchEmoji switchState={switchState} question={question} />
</>
);
}