frontPanel/src/pages/ViewPublicationPage/questions/Number.tsx

75 lines
2.1 KiB
TypeScript
Raw Normal View History

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 = {
currentQuestion: QuizQuestionNumber;
2023-11-30 17:39:57 +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();
const { answer } = answers.find(({ questionId }) => questionId === currentQuestion.content.id) ?? {};
2023-12-01 13:48:25 +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) {
updateAnswer(currentQuestion.content.id, "1");
2023-12-01 13:48:25 +00:00
}
}, []);
2023-11-30 17:39:57 +00:00
return (
<Box>
<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(
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}
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) => {
updateAnswer(currentQuestion.content.id, String(value));
2023-11-30 17:39:57 +00:00
}}
/>
</Box>
</Box>
);
};