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

151 lines
4.6 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
2023-12-08 11:53:41 +00:00
? `${currentQuestion.content.start}${max}`
2023-12-06 15:30:46 +00:00
: String(currentQuestion.content.start),
false
2023-12-06 14:23:48 +00:00
);
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
2023-12-08 11:53:41 +00:00
? answer?.split("—")[0]
2023-12-06 14:23:48 +00:00
: 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
2023-12-08 11:53:41 +00:00
? answer?.split("—")[0]
2023-12-06 14:23:48 +00:00
: answer
}
onChange={({ target }) => {
updateAnswer(
currentQuestion.content.id,
window.Number(target.value) >
2023-12-08 11:53:41 +00:00
window.Number(answer?.split("—")[1])
? `${answer?.split("—")[1]}${answer?.split("—")[1]}`
2023-12-06 14:23:48 +00:00
: window.Number(target.value) < min
2023-12-08 11:53:41 +00:00
? `${min}${answer?.split("—")[1]}`
: `${target.value}${answer?.split("—")[1]}`
2023-12-06 14:23:48 +00:00
);
}}
sx={{
maxWidth: "80px",
"& .MuiInputBase-input": { textAlign: "center" },
}}
/>
<CustomTextField
placeholder="0"
2023-12-08 11:53:41 +00:00
value={answer?.split("—")[1]}
2023-12-06 14:23:48 +00:00
onChange={({ target }) => {
updateAnswer(
currentQuestion.content.id,
window.Number(target.value) > max
2023-12-08 11:53:41 +00:00
? `${answer?.split("—")[0]}${max}`
2023-12-06 14:23:48 +00:00
: window.Number(target.value) <
2023-12-08 11:53:41 +00:00
window.Number(answer?.split("—")[0])
? `${answer?.split("—")[0]}${answer?.split("—")[0]}`
: `${answer?.split("—")[0]}${target.value}`
2023-12-06 14:23:48 +00:00
);
}}
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
2023-12-08 11:53:41 +00:00
? answer?.split("—").length || 0 > 1
? answer?.split("—").map((item) => window.Number(item))
2023-12-06 14:23:48 +00:00
: [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) => {
2023-12-08 11:53:41 +00:00
updateAnswer(
currentQuestion.content.id,
String(value).replace(",", "—")
);
2023-11-30 17:39:57 +00:00
}}
/>
</Box>
</Box>
);
};