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

67 lines
2.1 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";
import { updateAnswer, useQuizViewStore } from "@stores/quizView/store";
2023-12-16 14:55:56 +00:00
import { sendAnswer } from "@api/quizRelase";
import { enqueueSnackbar } from "notistack";
import { useDebouncedCallback } from "use-debounce";
import { useQuizId } from "../../../contexts/QuizIdContext";
import type { QuizQuestionText } from "../../../model/questionTypes/text";
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 qid = useQuizId();
2024-02-02 14:35:02 +00:00
const { answers } = useQuizViewStore();
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,
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",
flexDirection: "column",
width: "100%",
marginTop: "20px",
}}
>
<CustomTextField
placeholder={currentQuestion.content.placeholder}
//@ts-ignore
value={answer || ""}
onChange={async ({ target }) => {
updateAnswer(currentQuestion.id, target.value);
inputHC(target.value);
}
}
sx={{
"&:focus-visible": {
borderColor: theme.palette.primary.main
}
}}
/>
</Box>
</Box>
);
2023-12-16 14:55:56 +00:00
};