frontAnswerer/src/pages/ViewPublicationPage/questions/Text.tsx

69 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-12-29 00:58:19 +00:00
import { Box, Typography, useTheme } from "@mui/material";
2023-12-16 14:55:56 +00:00
import CustomTextField from "@ui_kit/CustomTextField";
2024-01-19 11:46:17 +00:00
import { useQuizViewStore, updateAnswer } from "@stores/quizView/store";
2023-12-16 14:55:56 +00:00
import type { QuizQuestionText } from "../../../model/questionTypes/text";
import { enqueueSnackbar } from "notistack";
2024-01-19 11:46:17 +00:00
import { useQuestionsStore } from "@stores/quizData/store"
import { sendAnswer } from "@api/quizRelase";
import { useDebouncedCallback } from "use-debounce";
2023-12-16 14:55:56 +00:00
type TextProps = {
currentQuestion: QuizQuestionText;
};
export const Text = ({ currentQuestion }: TextProps) => {
2023-12-29 00:58:19 +00:00
const theme = useTheme();
const { settings } = useQuestionsStore()
2023-12-16 14:55:56 +00:00
const { answers } = useQuizViewStore();
const { answer } = answers.find(({ questionId }) => questionId === currentQuestion.id) ?? {};
2023-12-16 14:55:56 +00:00
const inputHC = useDebouncedCallback(async (text) => {
if (!settings) return;
try {
await sendAnswer({
questionId: currentQuestion.id,
body: text,
qid: settings.qid
})
} catch (e) {
enqueueSnackbar("ответ не был засчитан")
}
}, 400);
2023-12-16 14:55:56 +00:00
return (
<Box>
2023-12-29 00:58:19 +00:00
<Typography variant="h5" color={theme.palette.text.primary}>{currentQuestion.title}</Typography>
2023-12-16 14:55:56 +00:00
<Box
sx={{
display: "flex",
flexDirection: "column",
width: "100%",
marginTop: "20px",
}}
>
<CustomTextField
placeholder={currentQuestion.content.placeholder}
//@ts-ignore
2023-12-16 14:55:56 +00:00
value={answer || ""}
onChange={async ({ target }) => {
updateAnswer(currentQuestion.id, target.value)
inputHC(target.value)
}
}
2023-12-29 00:58:19 +00:00
sx={{
"&:focus-visible": {
borderColor: theme.palette.primary.main
}
}}
2023-12-16 14:55:56 +00:00
/>
</Box>
</Box>
);
};