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

228 lines
6.8 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-19 11:46:17 +00:00
import { useQuestionsStore } from "@stores/quizData/store"
2023-12-29 00:58:19 +00:00
import { modes } from "../../../utils/themes/Publication/themePublication";
2023-12-16 14:55:56 +00:00
type NumberProps = {
currentQuestion: QuizQuestionNumber;
};
export const Number = ({ currentQuestion }: NumberProps) => {
const { settings } = useQuestionsStore()
2023-12-16 14:55:56 +00:00
const [minRange, setMinRange] = useState<string>("0");
const [maxRange, setMaxRange] = useState<string>("100000000000");
const theme = useTheme();
const { answers } = useQuizViewStore();
2023-12-29 00:58:19 +00:00
const isMobile = useMediaQuery(theme.breakpoints.down(650));
const updateMinRangeDebounced = useDebouncedCallback(async (value, crowded = false) => {
2023-12-16 14:55:56 +00:00
if (crowded) {
setMinRange(maxRange);
}
try {
await sendAnswer({
questionId: currentQuestion.id,
body: value,
//@ts-ignore
qid: settings.qid
})
updateAnswer(currentQuestion.id, value);
} catch (e) {
enqueueSnackbar("ответ не был засчитан")
}
2023-12-16 14:55:56 +00:00
}, 1000);
2023-12-29 00:58:19 +00:00
const updateMaxRangeDebounced = useDebouncedCallback(async (value, crowded = false) => {
2023-12-16 14:55:56 +00:00
if (crowded) {
setMaxRange(minRange);
}
try {
await sendAnswer({
questionId: currentQuestion.id,
body: value,
//@ts-ignore
qid: settings.qid
})
updateAnswer(currentQuestion.id, value);
} catch (e) {
enqueueSnackbar("ответ не был засчитан")
}
2023-12-16 14:55:56 +00:00
}, 1000);
const answer = answers.find(({ questionId }) => questionId === currentQuestion.id)?.answer as string;
2023-12-16 14:55:56 +00:00
2023-12-29 00:58:19 +00:00
const mode = modes;
2023-12-16 14:55:56 +00:00
const min = window.Number(currentQuestion.content.range.split("—")[0]);
const max = window.Number(currentQuestion.content.range.split("—")[1]);
const sliderValue = answer || currentQuestion.content.start + "—" + max;
useEffect(() => {
if (answer) {
setMinRange(answer.split("—")[0]);
setMaxRange(answer.split("—")[1]);
}
if (!answer) {
setMinRange(String(currentQuestion.content.start));
setMaxRange(String(max));
}
}, []);
return (
<Box>
2023-12-29 00:58:19 +00:00
<Typography variant="h5" color={theme.palette.text.primary}>{currentQuestion.title}</Typography>
2023-12-16 14:55:56 +00:00
<Box
sx={{
display: "flex",
flexDirection: "column",
width: "100%",
marginTop: "20px",
gap: "30px",
2023-12-29 00:58:19 +00:00
paddingRight: isMobile ? "10px" : undefined
2023-12-16 14:55:56 +00:00
}}
>
<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}
onChange={async (_, value) => {
const range = String(value).replace(",", "—").replace (/\D/, '');
updateAnswer(currentQuestion.id, range);
updateMinRangeDebounced(range, true);
2023-12-16 14:55:56 +00:00
}}
onChangeCommitted={(_, value) => {
if (currentQuestion.content.chooseRange) {
const range = value as number[];
setMinRange(String(range[0]));
setMaxRange(String(range[1]));
}
}}
2023-12-29 00:58:19 +00:00
//@ts-ignore
sx={{
color: theme.palette.primary.main,
"& .MuiSlider-valueLabel": {
background: theme.palette.primary.main,
}
}}
2023-12-16 14:55:56 +00:00
/>
{!currentQuestion.content.chooseRange && (
<CustomTextField
placeholder="0"
value={answer}
2023-12-29 00:58:19 +00:00
onChange={async ({ target }) => {
updateMinRangeDebounced(window.Number(target.value.replace (/\D/, '')) > max
2023-12-29 00:58:19 +00:00
? String(max)
: window.Number(target.value) < min
? String(min)
: target.value, true);
2023-12-16 14:55:56 +00:00
}}
sx={{
maxWidth: "80px",
2023-12-29 00:58:19 +00:00
borderColor: theme.palette.text.primary,
"& .MuiInputBase-input": {
textAlign: "center",
backgroundColor: mode[settings.cfg.theme] ? "white" : theme.palette.background.default,
},
2023-12-16 14:55:56 +00:00
}}
/>
)}
{currentQuestion.content.chooseRange && (
<Box
sx={{
display: "flex",
gap: "15px",
alignItems: "center",
"& .MuiFormControl-root": { width: "auto" },
}}
>
<CustomTextField
placeholder="0"
value={minRange}
onChange={({ target }) => {
setMinRange(target.value.replace (/\D/, ''));
2023-12-16 14:55:56 +00:00
if (window.Number(target.value) >= window.Number(maxRange)) {
updateMinRangeDebounced(`${maxRange}${maxRange}`, true);
return;
}
updateMinRangeDebounced(`${target.value}${maxRange}`);
}}
sx={{
maxWidth: "80px",
2023-12-29 00:58:19 +00:00
borderColor: theme.palette.text.primary,
"& .MuiInputBase-input": {
textAlign: "center",
backgroundColor: mode[settings.cfg.theme] ? "white" : theme.palette.background.default,
},
2023-12-16 14:55:56 +00:00
}}
/>
2023-12-29 00:58:19 +00:00
<Typography color={theme.palette.text.primary}>до</Typography>
2023-12-16 14:55:56 +00:00
<CustomTextField
placeholder="0"
value={maxRange}
onChange={({ target }) => {
setMaxRange(target.value.replace (/\D/, ''));
2023-12-16 14:55:56 +00:00
if (window.Number(target.value) <= window.Number(minRange)) {
updateMaxRangeDebounced(`${minRange}${minRange}`, true);
return;
}
updateMaxRangeDebounced(`${minRange}${target.value}`);
}}
sx={{
maxWidth: "80px",
2023-12-29 00:58:19 +00:00
borderColor: theme.palette.text.primary,
"& .MuiInputBase-input": {
textAlign: "center",
backgroundColor: mode[settings.cfg.theme] ? "white" : theme.palette.background.default,
},
2023-12-16 14:55:56 +00:00
}}
/>
</Box>
)}
</Box>
</Box>
);
2023-12-29 00:58:19 +00:00
2024-01-19 11:46:17 +00:00
};