2024-03-04 16:40:53 +00:00
|
|
|
import {
|
2024-04-03 12:42:12 +00:00
|
|
|
Box,
|
|
|
|
TextField as MuiTextField,
|
|
|
|
TextFieldProps,
|
|
|
|
Typography,
|
|
|
|
useTheme,
|
2024-03-04 16:40:53 +00:00
|
|
|
} from "@mui/material";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
|
|
|
import CustomTextField from "@ui_kit/CustomTextField";
|
|
|
|
|
2024-04-03 12:42:12 +00:00
|
|
|
import { useQuizViewStore } from "@stores/quizView";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2023-12-17 21:28:57 +00:00
|
|
|
import { sendAnswer } from "@api/quizRelase";
|
2024-02-19 15:04:52 +00:00
|
|
|
import { useQuizData } from "@contexts/QuizDataContext";
|
|
|
|
import { useRootContainerSize } from "@contexts/RootContainerWidthContext";
|
2024-02-08 13:42:31 +00:00
|
|
|
import { enqueueSnackbar } from "notistack";
|
2024-03-04 16:40:53 +00:00
|
|
|
import { ChangeEvent, FC, useEffect, useState } from "react";
|
2023-12-17 22:20:52 +00:00
|
|
|
import { useDebouncedCallback } from "use-debounce";
|
2024-03-05 14:29:52 +00:00
|
|
|
import { quizThemes } from "@utils/themes/Publication/themePublication";
|
2024-02-08 13:42:31 +00:00
|
|
|
import type { QuizQuestionText } from "../../../model/questionTypes/text";
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2024-02-21 10:23:25 +00:00
|
|
|
const TextField = MuiTextField as unknown as FC<TextFieldProps>; // temporary fix ts(2590)
|
|
|
|
|
2023-12-16 14:55:56 +00:00
|
|
|
type TextProps = {
|
2024-04-03 12:42:12 +00:00
|
|
|
currentQuestion: QuizQuestionText;
|
|
|
|
stepNumber: number | null;
|
2023-12-16 14:55:56 +00:00
|
|
|
};
|
|
|
|
|
2024-02-20 21:24:27 +00:00
|
|
|
const Orientation = [
|
2024-04-03 12:42:12 +00:00
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: false },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: false },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: false },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: false },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: false },
|
|
|
|
{ horizontal: false },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: true },
|
|
|
|
{ horizontal: true },
|
2024-03-04 16:40:53 +00:00
|
|
|
];
|
2024-02-20 21:24:27 +00:00
|
|
|
|
|
|
|
export const Text = ({ currentQuestion, stepNumber }: TextProps) => {
|
2024-04-03 12:42:12 +00:00
|
|
|
const { settings, preview } = useQuizData();
|
|
|
|
const spec = settings.cfg.spec;
|
|
|
|
const { quizId } = useQuizData();
|
|
|
|
const answers = useQuizViewStore(state => state.answers);
|
|
|
|
const { answer } =
|
|
|
|
answers.find(({ questionId }) => questionId === currentQuestion.id) ?? {};
|
|
|
|
const [isSending, setIsSending] = useState<boolean>(false);
|
2023-12-16 14:55:56 +00:00
|
|
|
|
2024-04-03 12:42:12 +00:00
|
|
|
const inputHC = useDebouncedCallback(async (text) => {
|
|
|
|
setIsSending(true);
|
|
|
|
try {
|
|
|
|
await sendAnswer({
|
|
|
|
questionId: currentQuestion.id,
|
|
|
|
body: text,
|
|
|
|
qid: quizId,
|
|
|
|
preview
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
enqueueSnackbar("ответ не был засчитан");
|
|
|
|
}
|
2024-03-04 16:40:53 +00:00
|
|
|
|
2024-04-03 12:42:12 +00:00
|
|
|
setIsSending(false);
|
|
|
|
}, 400);
|
|
|
|
useEffect(
|
|
|
|
() => () => {
|
|
|
|
inputHC.flush();
|
|
|
|
},
|
|
|
|
[inputHC]
|
|
|
|
);
|
|
|
|
switch (spec) {
|
|
|
|
case true:
|
|
|
|
return (
|
|
|
|
<TextSpecial
|
|
|
|
currentQuestion={currentQuestion}
|
|
|
|
answer={answer}
|
|
|
|
inputHC={inputHC}
|
|
|
|
stepNumber={stepNumber}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
case undefined:
|
|
|
|
return (
|
|
|
|
<TextNormal
|
|
|
|
currentQuestion={currentQuestion}
|
|
|
|
answer={answer}
|
|
|
|
inputHC={inputHC}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
return (
|
|
|
|
<TextNormal
|
|
|
|
currentQuestion={currentQuestion}
|
|
|
|
answer={answer}
|
|
|
|
inputHC={inputHC}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2024-02-20 21:24:27 +00:00
|
|
|
};
|
|
|
|
|
2024-02-20 23:28:34 +00:00
|
|
|
interface Props {
|
2024-04-03 12:42:12 +00:00
|
|
|
currentQuestion: QuizQuestionText;
|
|
|
|
answer: any;
|
|
|
|
inputHC: (a: string) => void;
|
|
|
|
stepNumber?: number | null;
|
2024-02-20 23:28:34 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 16:40:53 +00:00
|
|
|
const TextNormal = ({ currentQuestion, answer, inputHC }: Props) => {
|
2024-04-03 12:42:12 +00:00
|
|
|
const isMobile = useRootContainerSize() < 650;
|
|
|
|
const theme = useTheme();
|
|
|
|
const { settings } = useQuizData();
|
|
|
|
const updateAnswer = useQuizViewStore(state => state.updateAnswer);
|
2024-03-04 16:40:53 +00:00
|
|
|
|
2024-04-03 12:42:12 +00:00
|
|
|
return (
|
|
|
|
<Box>
|
|
|
|
<Typography
|
|
|
|
variant="h5"
|
|
|
|
color={theme.palette.text.primary}
|
|
|
|
sx={{ wordBreak: "break-word" }}
|
|
|
|
>
|
|
|
|
{currentQuestion.title}
|
|
|
|
</Typography>
|
2024-02-02 14:35:02 +00:00
|
|
|
<Box
|
2024-04-03 12:42:12 +00:00
|
|
|
sx={{
|
|
|
|
display: "flex",
|
|
|
|
width: "100%",
|
|
|
|
marginTop: "20px",
|
|
|
|
flexDirection: isMobile ? "column-reverse" : undefined,
|
|
|
|
alignItems: "center",
|
|
|
|
}}
|
2024-02-02 14:35:02 +00:00
|
|
|
>
|
2024-04-03 12:42:12 +00:00
|
|
|
<CustomTextField
|
|
|
|
placeholder={currentQuestion.content.placeholder}
|
|
|
|
value={answer || ""}
|
|
|
|
onChange={async ({ target }) => {
|
|
|
|
updateAnswer(currentQuestion.id, target.value, 0);
|
|
|
|
inputHC(target.value);
|
|
|
|
}}
|
|
|
|
sx={{
|
|
|
|
"& .MuiOutlinedInput-root": {
|
|
|
|
background: settings.cfg.design
|
|
|
|
? quizThemes[settings.cfg.theme].isLight
|
|
|
|
? "#F2F3F7"
|
|
|
|
: "rgba(255,255,255, 0.3)"
|
|
|
|
: "transparent",
|
|
|
|
},
|
|
|
|
"& .MuiOutlinedInput-notchedOutline": {
|
|
|
|
borderColor: "#9A9AAF"
|
|
|
|
},
|
|
|
|
"&:focus-visible": { borderColor: theme.palette.primary.main },
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{currentQuestion.content.back &&
|
|
|
|
currentQuestion.content.back !== " " && (
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
maxWidth: "400px",
|
|
|
|
width: "100%",
|
|
|
|
height: "300px",
|
|
|
|
margin: "15px",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<img
|
|
|
|
key={currentQuestion.id}
|
|
|
|
src={currentQuestion.content.back}
|
|
|
|
style={{ width: "100%", height: "100%", objectFit: "cover" }}
|
|
|
|
alt=""
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
)}
|
2024-02-02 14:35:02 +00:00
|
|
|
</Box>
|
2024-04-03 12:42:12 +00:00
|
|
|
</Box>
|
|
|
|
);
|
2023-12-16 14:55:56 +00:00
|
|
|
};
|
2024-02-20 21:24:27 +00:00
|
|
|
|
2024-03-04 16:40:53 +00:00
|
|
|
const TextSpecial = ({
|
2024-04-03 12:42:12 +00:00
|
|
|
currentQuestion,
|
|
|
|
answer,
|
|
|
|
inputHC,
|
|
|
|
stepNumber,
|
2024-03-04 16:40:53 +00:00
|
|
|
}: Props) => {
|
2024-04-03 12:42:12 +00:00
|
|
|
const theme = useTheme();
|
|
|
|
const isMobile = useRootContainerSize() < 650;
|
|
|
|
const isHorizontal = Orientation[Number(stepNumber) - 1].horizontal;
|
|
|
|
const { settings } = useQuizData();
|
|
|
|
const updateAnswer = useQuizViewStore(state => state.updateAnswer);
|
2024-03-04 16:40:53 +00:00
|
|
|
|
2024-04-03 12:42:12 +00:00
|
|
|
return (
|
|
|
|
<Box
|
2024-03-04 16:40:53 +00:00
|
|
|
sx={{
|
2024-04-03 12:42:12 +00:00
|
|
|
display: "flex",
|
|
|
|
flexDirection: isMobile ? "column" : undefined,
|
|
|
|
alignItems: isMobile ? "center" : undefined,
|
2024-03-04 16:40:53 +00:00
|
|
|
}}
|
2024-04-03 12:42:12 +00:00
|
|
|
>
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
display: "flex",
|
|
|
|
width: "100%",
|
|
|
|
marginTop: "20px",
|
|
|
|
flexDirection: "column",
|
|
|
|
alignItems: "center",
|
|
|
|
gap: "20px",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Typography
|
|
|
|
variant="h5"
|
|
|
|
color={theme.palette.text.primary}
|
|
|
|
sx={{ wordBreak: "break-word" }}
|
|
|
|
>
|
|
|
|
{currentQuestion.title}
|
|
|
|
</Typography>
|
|
|
|
{isHorizontal &&
|
|
|
|
currentQuestion.content.back &&
|
|
|
|
currentQuestion.content.back !== " " && (
|
|
|
|
<Box sx={{ margin: "30px", width: "50vw", maxHeight: "550px" }}>
|
|
|
|
<img
|
|
|
|
key={currentQuestion.id}
|
|
|
|
src={currentQuestion.content.back}
|
|
|
|
style={{ width: "100%", height: "100%", objectFit: "cover" }}
|
|
|
|
alt=""
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
{
|
|
|
|
<TextField
|
|
|
|
autoFocus={true}
|
|
|
|
multiline
|
|
|
|
maxRows={4}
|
|
|
|
placeholder={currentQuestion.content.placeholder}
|
|
|
|
value={answer || ""}
|
|
|
|
onChange={async ({ target }: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
updateAnswer(currentQuestion.id, target.value, 0);
|
|
|
|
inputHC(target.value);
|
|
|
|
}}
|
|
|
|
inputProps={{
|
|
|
|
maxLength: 400,
|
|
|
|
background: settings.cfg.design
|
|
|
|
? quizThemes[settings.cfg.theme].isLight
|
|
|
|
? "#F2F3F7"
|
|
|
|
: "rgba(154,154,175, 0.2)"
|
|
|
|
: "transparent",
|
|
|
|
}}
|
|
|
|
sx={{
|
|
|
|
width: "100%",
|
|
|
|
"& .MuiOutlinedInput-root": {
|
|
|
|
backgroundColor: settings.cfg.design
|
|
|
|
? "rgba(154,154,175, 0.2)"
|
|
|
|
: "#FFFFFF",
|
|
|
|
},
|
|
|
|
"&:focus-visible": {
|
|
|
|
borderColor: theme.palette.primary.main,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
</Box>
|
|
|
|
{!isHorizontal &&
|
|
|
|
currentQuestion.content.back &&
|
|
|
|
currentQuestion.content.back !== " " && (
|
|
|
|
<Box sx={{ margin: "15px", width: "40vw" }}>
|
|
|
|
<img
|
|
|
|
key={currentQuestion.id}
|
|
|
|
src={currentQuestion.content.back}
|
|
|
|
style={{ width: "100%", height: "100%", objectFit: "cover" }}
|
|
|
|
alt=""
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
);
|
2024-03-04 16:40:53 +00:00
|
|
|
};
|