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

252 lines
9.9 KiB
TypeScript
Raw Normal View History

2023-12-16 14:55:56 +00:00
import { useState, useEffect } from "react";
2023-12-29 00:58:19 +00:00
import { Box, Typography, useMediaQuery, useTheme } from "@mui/material";
2023-12-16 14:55:56 +00:00
import { useDebouncedCallback } from "use-debounce";
import CustomTextField from "@ui_kit/CustomTextField";
import { CustomSlider } from "@ui_kit/CustomSlider";
2024-01-19 11:46:17 +00:00
import { useQuizViewStore, updateAnswer } from "@stores/quizView/store";
2023-12-16 14:55:56 +00:00
import type { QuizQuestionNumber } from "../../../model/questionTypes/number";
import { enqueueSnackbar } from "notistack";
import { sendAnswer } from "@api/quizRelase";
2024-01-31 12:57:07 +00:00
import { quizThemes } from "@utils/themes/Publication/themePublication";
2024-02-02 14:35:02 +00:00
import { useQuizData } from "@utils/hooks/useQuizData";
2023-12-16 14:55:56 +00:00
type NumberProps = {
currentQuestion: QuizQuestionNumber;
2023-12-16 14:55:56 +00:00
};
export const Number = ({ currentQuestion }: NumberProps) => {
2024-02-02 14:35:02 +00:00
const { settings } = useQuizData();
2024-01-31 12:57:07 +00:00
const [inputValue, setInputValue] = useState<string>("0");
const [minRange, setMinRange] = useState<string>("0");
const [maxRange, setMaxRange] = useState<string>("100000000000");
const theme = useTheme();
const { answers } = useQuizViewStore();
const isMobile = useMediaQuery(theme.breakpoints.down(650));
2024-01-31 12:57:07 +00:00
const min = window.Number(currentQuestion.content.range.split("—")[0]);
const max = window.Number(currentQuestion.content.range.split("—")[1]);
2024-01-31 12:57:07 +00:00
const sendAnswerToBackend = async (value: string) => {
try {
await sendAnswer({
questionId: currentQuestion.id,
body: value,
2024-01-31 12:57:07 +00:00
//@ts-ignore
qid: settings.qid,
});
updateAnswer(currentQuestion.id, value);
} catch (e) {
2024-01-31 12:57:07 +00:00
enqueueSnackbar("ответ не был засчитан");
}
2024-01-31 12:57:07 +00:00
};
const updateValueDebounced = useDebouncedCallback(async (value: string) => {
const newValue =
window.Number(value) < window.Number(minRange)
? minRange
: window.Number(value) > window.Number(maxRange)
? maxRange
: value;
setInputValue(newValue);
await sendAnswerToBackend(newValue);
}, 1000);
2024-01-31 12:57:07 +00:00
const updateMinRangeDebounced = useDebouncedCallback(
async (value: string, crowded = false) => {
const newMinValue = crowded
? maxRange
: window.Number(value.split("—")[0]) < min
? String(min)
: value.split("—")[0];
setMinRange(newMinValue);
await sendAnswerToBackend(`${newMinValue}${value.split("—")[1]}`);
},
1000
);
const updateMaxRangeDebounced = useDebouncedCallback(
async (value: string, crowded = false) => {
const newMaxValue = crowded
? minRange
: window.Number(value.split("—")[1]) > max
? String(max)
: value.split("—")[1];
setMaxRange(newMaxValue);
await sendAnswerToBackend(`${value.split("—")[0]}${newMaxValue}`);
},
1000
);
const answer = answers.find(
({ questionId }) => questionId === currentQuestion.id
)?.answer as string;
const sliderValue = answer || currentQuestion.content.start + "—" + max;
useEffect(() => {
if (answer) {
2024-01-31 12:57:07 +00:00
if (answer.includes("—")) {
setMinRange(answer.split("—")[0]);
setMaxRange(answer.split("—")[1]);
} else {
setInputValue(answer);
2023-12-16 14:55:56 +00:00
}
}
if (!answer) {
setMinRange(String(currentQuestion.content.start));
setMaxRange(String(max));
2024-01-31 12:57:07 +00:00
setInputValue(String(currentQuestion.content.start));
}
}, []);
return (
<Box>
2024-01-31 12:57:07 +00:00
<Typography variant="h5" color={theme.palette.text.primary}>
{currentQuestion.title}
</Typography>
<Box
sx={{
display: "flex",
flexDirection: "column",
width: "100%",
marginTop: "20px",
gap: "30px",
2024-01-31 12:57:07 +00:00
paddingRight: isMobile ? "10px" : undefined,
}}
>
<CustomSlider
value={
currentQuestion.content.chooseRange
? sliderValue.split("—").length || 0 > 1
? sliderValue.split("—").map((item) => window.Number(item))
: [min, min + 1]
: window.Number(sliderValue.split("—")[0])
}
min={min}
max={max}
step={currentQuestion.content.step || 1}
2024-01-31 12:57:07 +00:00
onChange={(_, value) => {
const range = Array.isArray(value)
? `${value[0]}${value[1]}`
: String(value);
updateAnswer(currentQuestion.id, range);
}}
2024-01-31 12:57:07 +00:00
onChangeCommitted={async (_, value) => {
if (currentQuestion.content.chooseRange && Array.isArray(value)) {
setMinRange(String(value[0]));
setMaxRange(String(value[1]));
await sendAnswerToBackend(`${value[0]}${value[1]}`);
2024-01-31 12:57:07 +00:00
return;
}
2024-01-31 12:57:07 +00:00
setInputValue(String(value));
await sendAnswerToBackend(String(value));
}}
2024-01-31 12:57:07 +00:00
//@ts-ignore
sx={{
color: theme.palette.primary.main,
"& .MuiSlider-valueLabel": {
background: theme.palette.primary.main,
2024-01-31 12:57:07 +00:00
},
}}
/>
{!currentQuestion.content.chooseRange && (
<CustomTextField
placeholder="0"
2024-01-31 12:57:07 +00:00
value={inputValue}
onChange={({ target }) => {
const value = target.value.replace(/\D/g, "");
setInputValue(value);
updateValueDebounced(value);
}}
sx={{
maxWidth: "80px",
borderColor: theme.palette.text.primary,
"& .MuiInputBase-input": {
textAlign: "center",
2024-01-31 12:57:07 +00:00
backgroundColor: quizThemes[settings.cfg.theme].isLight
? "white"
: theme.palette.background.default,
},
}}
/>
)}
{currentQuestion.content.chooseRange && (
<Box
sx={{
display: "flex",
gap: "15px",
alignItems: "center",
"& .MuiFormControl-root": { width: "auto" },
}}
>
<CustomTextField
placeholder="0"
value={minRange}
onChange={({ target }) => {
2024-01-31 12:57:07 +00:00
const newValue = target.value.replace(/\D/g, "");
setMinRange(newValue);
2024-01-31 12:57:07 +00:00
if (window.Number(newValue) >= window.Number(maxRange)) {
updateMinRangeDebounced(`${maxRange}${maxRange}`, true);
return;
}
2024-01-31 12:57:07 +00:00
updateMinRangeDebounced(`${newValue}${maxRange}`);
}}
sx={{
maxWidth: "80px",
borderColor: theme.palette.text.primary,
"& .MuiInputBase-input": {
textAlign: "center",
2024-01-31 12:57:07 +00:00
backgroundColor: quizThemes[settings.cfg.theme].isLight
? "white"
: theme.palette.background.default,
},
}}
/>
<Typography color={theme.palette.text.primary}>до</Typography>
<CustomTextField
placeholder="0"
value={maxRange}
onChange={({ target }) => {
2024-01-31 12:57:07 +00:00
const newValue = target.value.replace(/\D/g, "");
setMaxRange(newValue);
2024-01-31 12:57:07 +00:00
if (window.Number(newValue) <= window.Number(minRange)) {
updateMaxRangeDebounced(`${minRange}${minRange}`, true);
return;
}
2024-01-31 12:57:07 +00:00
updateMaxRangeDebounced(`${minRange}${newValue}`);
}}
sx={{
maxWidth: "80px",
borderColor: theme.palette.text.primary,
"& .MuiInputBase-input": {
textAlign: "center",
2024-01-31 12:57:07 +00:00
backgroundColor: quizThemes[settings.cfg.theme].isLight
? "white"
: theme.palette.background.default,
},
}}
/>
</Box>
)}
</Box>
</Box>
);
2024-01-19 11:46:17 +00:00
};