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

147 lines
4.5 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();
2023-12-06 14:23:48 +00:00
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) {
2023-12-06 14:23:48 +00:00
updateAnswer(
currentQuestion.content.id,
currentQuestion.content.chooseRange
? `${currentQuestion.content.start},${max}`
: String(currentQuestion.content.start)
);
2023-12-01 13:48:25 +00:00
}
2023-12-06 14:23:48 +00:00
}, [answer]);
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",
}}
>
2023-12-06 14:23:48 +00:00
{!currentQuestion.content.chooseRange && (
<CustomTextField
placeholder="0"
value={
currentQuestion.content.chooseRange
? answer?.split(",")[0]
: answer
}
onChange={({ target }) => {
updateAnswer(
currentQuestion.content.id,
window.Number(target.value) > max
? String(max)
: window.Number(target.value) < min
? String(min)
: target.value
);
}}
sx={{
maxWidth: "80px",
"& .MuiInputBase-input": { textAlign: "center" },
}}
/>
)}
{currentQuestion.content.chooseRange && (
<Box
sx={{
display: "flex",
gap: "15px",
"& .MuiFormControl-root": { width: "auto" },
}}
>
<CustomTextField
placeholder="0"
value={
currentQuestion.content.chooseRange
? answer?.split(",")[0]
: answer
}
onChange={({ target }) => {
updateAnswer(
currentQuestion.content.id,
window.Number(target.value) >
window.Number(answer?.split(",")[1])
? `${answer?.split(",")[1]},${answer?.split(",")[1]}`
: window.Number(target.value) < min
? `${min},${answer?.split(",")[1]}`
: `${target.value},${answer?.split(",")[1]}`
);
}}
sx={{
maxWidth: "80px",
"& .MuiInputBase-input": { textAlign: "center" },
}}
/>
<CustomTextField
placeholder="0"
value={answer?.split(",")[1]}
onChange={({ target }) => {
updateAnswer(
currentQuestion.content.id,
window.Number(target.value) > max
? `${answer?.split(",")[0]},${max}`
: window.Number(target.value) <
window.Number(answer?.split(",")[0])
? `${answer?.split(",")[0]},${answer?.split(",")[0]}`
: `${answer?.split(",")[0]},${target.value}`
);
}}
sx={{
maxWidth: "80px",
"& .MuiInputBase-input": { textAlign: "center" },
}}
/>
</Box>
)}
2023-11-30 17:39:57 +00:00
<Slider
2023-12-06 14:23:48 +00:00
value={
currentQuestion.content.chooseRange
? answer?.split(",").length || 0 > 1
? answer?.split(",").map((item) => window.Number(item))
: [min, min + 1]
: window.Number(answer || 1)
}
2023-12-01 13:48:25 +00:00
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>
);
};