2023-12-01 13:48:25 +00:00
|
|
|
import { useEffect } from "react";
|
2023-11-30 17:39:57 +00:00
|
|
|
import { Box, Typography, Slider, useTheme } from "@mui/material";
|
|
|
|
|
|
|
|
|
|
import CustomTextField from "@ui_kit/CustomTextField";
|
|
|
|
|
|
2023-12-01 13:48:25 +00:00
|
|
|
import { useQuizViewStore, updateAnswer } from "@root/quizView";
|
2023-11-30 17:39:57 +00:00
|
|
|
|
|
|
|
|
import type { QuizQuestionNumber } from "../../../model/questionTypes/number";
|
|
|
|
|
|
|
|
|
|
type NumberProps = {
|
2023-12-03 10:48:00 +00:00
|
|
|
currentQuestion: QuizQuestionNumber;
|
2023-11-30 17:39:57 +00:00
|
|
|
};
|
|
|
|
|
|
2023-12-03 10:48:00 +00:00
|
|
|
export const Number = ({ currentQuestion }: NumberProps) => {
|
2023-11-30 17:39:57 +00:00
|
|
|
const theme = useTheme();
|
2023-12-01 13:48:25 +00:00
|
|
|
const { answers } = useQuizViewStore();
|
2023-12-03 10:48:00 +00:00
|
|
|
const { answer } = answers.find(({ questionId }) => questionId === currentQuestion.content.id) ?? {};
|
2023-12-01 13:48:25 +00:00
|
|
|
|
2023-12-03 10:48:00 +00:00
|
|
|
const min = window.Number(currentQuestion.content.range.split("—")[0]);
|
|
|
|
|
const max = window.Number(currentQuestion.content.range.split("—")[1]);
|
2023-12-01 13:48:25 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!answer) {
|
2023-12-03 10:48:00 +00:00
|
|
|
updateAnswer(currentQuestion.content.id, "1");
|
2023-12-01 13:48:25 +00:00
|
|
|
}
|
|
|
|
|
}, []);
|
2023-11-30 17:39:57 +00:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Box>
|
2023-12-03 10:48:00 +00:00
|
|
|
<Typography variant="h5">{currentQuestion.title}</Typography>
|
2023-11-30 17:39:57 +00:00
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
width: "100%",
|
|
|
|
|
marginTop: "20px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<CustomTextField
|
|
|
|
|
placeholder="0"
|
2023-12-01 13:48:25 +00:00
|
|
|
value={answer || ""}
|
|
|
|
|
onChange={({ target }) => {
|
|
|
|
|
updateAnswer(
|
2023-12-03 10:48:00 +00:00
|
|
|
currentQuestion.content.id,
|
2023-12-01 13:48:25 +00:00
|
|
|
window.Number(target.value) > max
|
|
|
|
|
? String(max)
|
|
|
|
|
: window.Number(target.value) < min
|
|
|
|
|
? String(min)
|
|
|
|
|
: target.value
|
|
|
|
|
);
|
|
|
|
|
}}
|
2023-11-30 17:39:57 +00:00
|
|
|
sx={{
|
|
|
|
|
maxWidth: "80px",
|
2023-12-01 13:48:25 +00:00
|
|
|
"& .MuiInputBase-input": { textAlign: "center" },
|
2023-11-30 17:39:57 +00:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<Slider
|
2023-12-01 13:48:25 +00:00
|
|
|
value={window.Number(answer || 1)}
|
|
|
|
|
min={min}
|
|
|
|
|
max={max}
|
2023-12-03 10:48:00 +00:00
|
|
|
step={currentQuestion.content.step || 1}
|
2023-11-30 17:39:57 +00:00
|
|
|
sx={{
|
|
|
|
|
color: theme.palette.brightPurple.main,
|
|
|
|
|
padding: "0",
|
|
|
|
|
marginTop: "25px",
|
|
|
|
|
}}
|
|
|
|
|
onChange={(_, value) => {
|
2023-12-03 10:48:00 +00:00
|
|
|
updateAnswer(currentQuestion.content.id, String(value));
|
2023-11-30 17:39:57 +00:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
};
|