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

180 lines
5.2 KiB
TypeScript
Raw Normal View History

2023-12-11 08:51:53 +00:00
import { useState, useEffect } from "react";
2023-11-30 17:39:57 +00:00
import { Box, Typography, Slider, useTheme } from "@mui/material";
2023-12-11 08:51:53 +00:00
import { useDebouncedCallback } from "use-debounce";
2023-11-30 17:39:57 +00:00
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-12-11 08:51:53 +00:00
const [minRange, setMinRange] = useState<string>("0");
const [maxRange, setMaxRange] = useState<string>("100");
2023-11-30 17:39:57 +00:00
const theme = useTheme();
2023-12-01 13:48:25 +00:00
const { answers } = useQuizViewStore();
2023-12-11 08:51:53 +00:00
const updateMinRangeDebounced = useDebouncedCallback(
(value, crowded = false) => {
if (crowded) {
setMinRange(maxRange);
}
updateAnswer(currentQuestion.content.id, value);
},
1000
);
const updateMaxRangeDebounced = useDebouncedCallback(
(value, crowded = false) => {
if (crowded) {
setMaxRange(minRange);
}
updateAnswer(currentQuestion.content.id, value);
},
1000
);
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(() => {
2023-12-11 08:51:53 +00:00
console.log("ans", currentQuestion.content.start);
if (answer) {
setMinRange(answer.split("—")[0]);
setMaxRange(answer.split("—")[1]);
}
2023-12-01 13:48:25 +00:00
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-11 08:51:53 +00:00
setMinRange(String(currentQuestion.content.start));
setMaxRange(String(max));
2023-12-01 13:48:25 +00:00
}
2023-12-11 08:51:53 +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",
}}
>
2023-12-06 14:23:48 +00:00
{!currentQuestion.content.chooseRange && (
<CustomTextField
placeholder="0"
2023-12-11 08:51:53 +00:00
value={answer}
2023-12-06 14:23:48 +00:00
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"
2023-12-11 08:51:53 +00:00
value={minRange}
2023-12-06 14:23:48 +00:00
onChange={({ target }) => {
2023-12-11 08:51:53 +00:00
setMinRange(target.value);
if (window.Number(target.value) >= window.Number(maxRange)) {
updateMinRangeDebounced(`${maxRange}${maxRange}`, true);
return;
}
updateMinRangeDebounced(`${target.value}${maxRange}`);
2023-12-06 14:23:48 +00:00
}}
sx={{
maxWidth: "80px",
"& .MuiInputBase-input": { textAlign: "center" },
}}
/>
<CustomTextField
placeholder="0"
2023-12-11 08:51:53 +00:00
value={maxRange}
2023-12-06 14:23:48 +00:00
onChange={({ target }) => {
2023-12-11 08:51:53 +00:00
setMaxRange(target.value);
if (window.Number(target.value) <= window.Number(minRange)) {
updateMaxRangeDebounced(`${minRange}${minRange}`, true);
return;
}
updateMaxRangeDebounced(`${minRange}${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-11 08:51:53 +00:00
const range = String(value).replace(",", "—");
updateAnswer(currentQuestion.content.id, range);
}}
onChangeCommitted={(_, value) => {
if (currentQuestion.content.chooseRange) {
const range = value as number[];
setMinRange(String(range[0]));
setMaxRange(String(range[1]));
}
2023-11-30 17:39:57 +00:00
}}
/>
</Box>
</Box>
);
};