frontPanel/src/pages/Questions/SliderOptions/SliderOptions.tsx

227 lines
8.3 KiB
TypeScript
Raw Normal View History

2023-09-12 15:57:48 +00:00
import { useState } from "react";
2023-09-06 13:20:12 +00:00
import { useParams } from "react-router-dom";
2023-09-15 12:37:12 +00:00
import { Box, Typography, useMediaQuery, useTheme } from "@mui/material";
import ButtonsOptions from "../ButtonsOptions";
2023-09-12 14:36:22 +00:00
import CustomNumberField from "@ui_kit/CustomNumberField";
import SwitchSlider from "./switchSlider";
2023-09-06 08:01:44 +00:00
import { questionStore, updateQuestionsList } from "@root/questions";
2023-10-03 14:03:57 +00:00
import type { QuizQuestionNumber } from "../../../model/questionTypes/number";
2023-08-24 11:09:47 +00:00
interface Props {
totalIndex: number;
}
2023-08-24 11:09:47 +00:00
export default function SliderOptions({ totalIndex }: Props) {
2023-09-15 12:37:12 +00:00
const theme = useTheme();
const isTablet = useMediaQuery(theme.breakpoints.down(980));
const isMobile = useMediaQuery(theme.breakpoints.down(790));
2023-09-12 15:57:48 +00:00
const [switchState, setSwitchState] = useState("setting");
const [stepError, setStepError] = useState("");
2023-09-06 13:20:12 +00:00
const quizId = Number(useParams().quizId);
2023-09-06 08:01:44 +00:00
const { listQuestions } = questionStore();
2023-10-03 14:03:57 +00:00
const question = listQuestions[quizId][totalIndex] as QuizQuestionNumber;
2023-09-06 13:20:12 +00:00
2023-08-24 11:09:47 +00:00
const SSHC = (data: string) => {
setSwitchState(data);
};
2023-08-24 11:09:47 +00:00
return (
<>
<Box
sx={{
2023-09-15 12:37:12 +00:00
width: isTablet ? "auto" : "100%",
2023-09-26 21:11:27 +00:00
maxWidth: "673.8px",
2023-08-24 11:09:47 +00:00
display: "flex",
2023-09-26 21:11:27 +00:00
pl: "20px",
2023-10-03 14:41:12 +00:00
pr: isMobile ? "13px" : "20px",
2023-09-26 21:11:27 +00:00
pb: isMobile ? "30px" : "20px",
2023-08-24 11:09:47 +00:00
flexDirection: "column",
2023-10-03 14:41:12 +00:00
gap: isMobile ? "25px" : "20px",
2023-08-24 11:09:47 +00:00
}}
>
2023-10-03 14:41:12 +00:00
<Box
sx={{
gap: isMobile ? "10px" : "14px",
mt: isMobile ? "25px" : "0px",
display: "flex",
flexDirection: "column",
marginRight: isMobile ? "10px" : "0px",
}}
>
2023-09-26 21:11:27 +00:00
<Typography sx={{ fontWeight: "500", fontSize: "18px", color: "#4D4D4D" }}>
2023-09-21 07:00:08 +00:00
Выбор значения из диапазона
</Typography>
2023-10-03 14:41:12 +00:00
<Box sx={{ width: "100%", display: "flex", alignItems: "center", gap: isMobile ? "9px" : "20px" }}>
2023-09-12 14:36:22 +00:00
<CustomNumberField
2023-09-26 21:11:27 +00:00
sx={{ maxWidth: "310px", width: "100%" }}
2023-09-06 08:01:44 +00:00
placeholder={"0"}
2023-09-12 15:23:56 +00:00
min={0}
max={99}
2023-10-03 14:03:57 +00:00
value={question.content.range.split("—")[0]}
2023-09-06 08:01:44 +00:00
onChange={({ target }) => {
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionNumber>(quizId, totalIndex, {
2023-10-03 14:03:57 +00:00
content: {
...question.content,
range: `${target.value}${
question.content.range.split("—")[1]
}`,
},
2023-09-06 13:20:12 +00:00
});
2023-09-06 08:01:44 +00:00
}}
2023-09-12 15:23:56 +00:00
onBlur={({ target }) => {
2023-10-03 14:03:57 +00:00
const start = question.content.start;
2023-09-12 15:23:56 +00:00
const min = Number(target.value);
2023-10-03 14:03:57 +00:00
const max = Number(question.content.range.split("—")[1]);
2023-09-12 15:23:56 +00:00
if (min >= max) {
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionNumber>(quizId, totalIndex, {
2023-10-03 14:03:57 +00:00
content: {
...question.content,
range: `${max - 1 >= 0 ? max - 1 : 0}${
question.content.range.split("—")[1]
}`,
},
2023-09-12 15:23:56 +00:00
});
}
2023-09-13 08:15:02 +00:00
if (start < min) {
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionNumber>(quizId, totalIndex, {
2023-10-03 14:03:57 +00:00
content: { ...question.content, start: min },
2023-09-13 08:15:02 +00:00
});
}
2023-09-12 15:23:56 +00:00
}}
2023-09-06 08:01:44 +00:00
/>
2023-08-24 11:09:47 +00:00
<Typography></Typography>
2023-09-12 14:36:22 +00:00
<CustomNumberField
2023-09-26 21:11:27 +00:00
sx={{ maxWidth: "310px", width: "100%" }}
2023-09-06 08:01:44 +00:00
placeholder={"100"}
2023-09-12 15:23:56 +00:00
min={0}
max={100}
2023-10-03 14:03:57 +00:00
value={question.content.range.split("—")[1]}
2023-09-06 08:01:44 +00:00
onChange={({ target }) => {
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionNumber>(quizId, totalIndex, {
2023-10-03 14:03:57 +00:00
content: {
...question.content,
range: `${question.content.range.split("—")[0]}${
target.value
}`,
},
2023-09-06 13:20:12 +00:00
});
2023-09-06 08:01:44 +00:00
}}
2023-09-12 15:23:56 +00:00
onBlur={({ target }) => {
2023-10-03 14:03:57 +00:00
const start = question.content.start;
const step = question.content.step;
const min = Number(question.content.range.split("—")[0]);
2023-09-12 15:23:56 +00:00
const max = Number(target.value);
2023-09-13 08:15:02 +00:00
const range = max - min;
2023-09-12 15:23:56 +00:00
if (max <= min) {
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionNumber>(quizId, totalIndex, {
2023-10-03 14:03:57 +00:00
content: {
...question.content,
range: `${question.content.range.split("—")[0]}${
min + 1 >= 100 ? 100 : min + 1
}`,
},
2023-09-12 15:23:56 +00:00
});
}
2023-09-13 08:15:02 +00:00
if (start > max) {
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionNumber>(quizId, totalIndex, {
2023-10-03 14:03:57 +00:00
content: { ...question.content, start: max },
2023-09-13 08:15:02 +00:00
});
}
if (step > max) {
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionNumber>(quizId, totalIndex, {
2023-10-03 14:03:57 +00:00
content: { ...question.content, step: max },
2023-09-13 08:15:02 +00:00
});
if (range % step) {
2023-10-03 14:41:12 +00:00
setStepError(`Шаг должен делить без остатка диапазон ${max} - ${min} = ${max - min}`);
2023-09-13 08:15:02 +00:00
} else {
setStepError("");
}
}
2023-09-12 15:23:56 +00:00
}}
2023-09-06 08:01:44 +00:00
/>
2023-08-24 11:09:47 +00:00
</Box>
</Box>
<Box
sx={{
display: "flex",
alignItems: "center",
justifyContent: "space-between",
2023-09-21 07:00:08 +00:00
flexDirection: isMobile ? "column-reverse" : "",
gap: isMobile ? "15px" : "50px",
2023-08-24 11:09:47 +00:00
}}
>
2023-09-12 14:36:22 +00:00
<Box sx={{ width: "100%" }}>
2023-10-03 14:41:12 +00:00
<Typography sx={{ fontWeight: "500", fontSize: "18px", color: "#4D4D4D", mb: isMobile ? "10px" : "14px" }}>
2023-09-21 07:00:08 +00:00
Начальное значение
</Typography>
2023-09-12 14:36:22 +00:00
<CustomNumberField
2023-09-26 21:11:27 +00:00
sx={{ maxWidth: "310px", width: "100%" }}
2023-09-06 08:01:44 +00:00
placeholder={"50"}
2023-10-03 14:03:57 +00:00
min={Number(question.content.range.split("—")[0])}
max={Number(question.content.range.split("—")[1])}
value={String(question.content.start)}
2023-09-06 08:01:44 +00:00
onChange={({ target }) => {
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionNumber>(quizId, totalIndex, {
2023-10-03 14:03:57 +00:00
content: { ...question.content, start: Number(target.value) },
2023-09-06 13:20:12 +00:00
});
2023-09-06 08:01:44 +00:00
}}
/>
2023-08-24 11:09:47 +00:00
</Box>
2023-09-12 14:36:22 +00:00
<Box sx={{ width: "100%" }}>
2023-09-22 07:26:07 +00:00
<Typography
sx={{
fontWeight: "500",
fontSize: "18px",
color: "#4D4D4D",
mb: "10px",
}}
>
Шаг
</Typography>
2023-09-12 14:36:22 +00:00
<CustomNumberField
2023-09-26 21:11:27 +00:00
sx={{ maxWidth: "310px", width: "100%" }}
2023-09-12 15:23:56 +00:00
min={0}
max={100}
2023-09-06 08:01:44 +00:00
placeholder={"1"}
2023-09-12 15:57:48 +00:00
error={stepError}
2023-10-03 14:03:57 +00:00
value={String(question.content.step)}
2023-09-06 08:01:44 +00:00
onChange={({ target }) => {
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionNumber>(quizId, totalIndex, {
2023-10-03 14:03:57 +00:00
content: { ...question.content, step: Number(target.value) },
2023-09-06 13:20:12 +00:00
});
2023-09-06 08:01:44 +00:00
}}
2023-09-12 15:23:56 +00:00
onBlur={({ target }) => {
2023-10-03 14:03:57 +00:00
const min = Number(question.content.range.split("—")[0]);
const max = Number(question.content.range.split("—")[1]);
2023-09-12 15:23:56 +00:00
const range = max - min;
const step = Number(target.value);
2023-09-13 07:28:24 +00:00
if (step > max) {
2023-10-04 11:35:02 +00:00
updateQuestionsList<QuizQuestionNumber>(quizId, totalIndex, {
2023-10-03 14:03:57 +00:00
content: { ...question.content, step: max },
2023-09-12 15:23:56 +00:00
});
}
2023-09-12 15:57:48 +00:00
if (range % step) {
2023-10-03 14:41:12 +00:00
setStepError(`Шаг должен делить без остатка диапазон ${max} - ${min} = ${max - min}`);
2023-09-12 15:57:48 +00:00
} else {
setStepError("");
}
2023-09-12 15:23:56 +00:00
}}
2023-09-06 08:01:44 +00:00
/>
2023-08-24 11:09:47 +00:00
</Box>
</Box>
</Box>
2023-10-03 14:41:12 +00:00
<ButtonsOptions switchState={switchState} SSHC={SSHC} totalIndex={totalIndex} />
2023-08-24 11:09:47 +00:00
<SwitchSlider switchState={switchState} totalIndex={totalIndex} />
</>
);
}