frontAnswerer/lib/components/ViewPublicationPage/questions/Text.tsx

79 lines
2.8 KiB
TypeScript
Raw Normal View History

import { Box, Typography, useMediaQuery, useTheme } from "@mui/material";
2023-12-16 14:55:56 +00:00
import CustomTextField from "@ui_kit/CustomTextField";
import { updateAnswer, useQuizViewStore } from "@stores/quizView";
2023-12-16 14:55:56 +00:00
import { sendAnswer } from "@api/quizRelase";
import { enqueueSnackbar } from "notistack";
import { useDebouncedCallback } from "use-debounce";
import type { QuizQuestionText } from "../../../model/questionTypes/text";
import { useQuizData } from "@contexts/QuizDataContext";
2023-12-16 14:55:56 +00:00
type TextProps = {
2024-02-02 14:35:02 +00:00
currentQuestion: QuizQuestionText;
2023-12-16 14:55:56 +00:00
};
export const Text = ({ currentQuestion }: TextProps) => {
2024-02-02 14:35:02 +00:00
const theme = useTheme();
const { quizId } = useQuizData();
2024-02-02 14:35:02 +00:00
const { answers } = useQuizViewStore();
const isMobile = useMediaQuery(theme.breakpoints.down(650));
2024-02-02 14:35:02 +00:00
const { answer } = answers.find(({ questionId }) => questionId === currentQuestion.id) ?? {};
2023-12-16 14:55:56 +00:00
2024-02-02 14:35:02 +00:00
const inputHC = useDebouncedCallback(async (text) => {
try {
2024-02-02 14:35:02 +00:00
await sendAnswer({
questionId: currentQuestion.id,
body: text,
qid: quizId,
2024-02-02 14:35:02 +00:00
});
2024-02-02 14:35:02 +00:00
} catch (e) {
enqueueSnackbar("ответ не был засчитан");
}
}, 400);
return (
<Box>
<Typography variant="h5" color={theme.palette.text.primary}>{currentQuestion.title}</Typography>
<Box
sx={{
display: "flex",
width: "100%",
marginTop: "20px",
flexDirection: isMobile ? "column-reverse" : undefined,
alignItems: "center"
2024-02-02 14:35:02 +00:00
}}
>
<CustomTextField
placeholder={currentQuestion.content.placeholder}
//@ts-ignore
value={answer || ""}
onChange={async ({ target }) => {
updateAnswer(currentQuestion.id, target.value, 0);
2024-02-02 14:35:02 +00:00
inputHC(target.value);
}
}
sx={{
"&: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>
</Box>
);
2023-12-16 14:55:56 +00:00
};