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

209 lines
8.2 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-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-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-08-24 11:09:47 +00:00
maxWidth: "640px",
display: "flex",
padding: "20px",
flexDirection: "column",
gap: "20px",
}}
>
<Box sx={{ gap: "10px", display: "flex", flexDirection: "column" }}>
2023-09-21 07:00:08 +00:00
<Typography sx={{ fontWeight: "500", fontSize: "18px", color: "#4D4D4D", mb: "10px" }}>
Выбор значения из диапазона
</Typography>
2023-08-24 11:09:47 +00:00
<Box sx={{ display: "flex", alignItems: "center", gap: "20px" }}>
2023-09-12 14:36:22 +00:00
<CustomNumberField
2023-09-06 08:01:44 +00:00
placeholder={"0"}
2023-09-12 15:23:56 +00:00
min={0}
max={99}
2023-09-15 12:37:12 +00:00
value={listQuestions[quizId][totalIndex].content.range.split("—")[0]}
2023-09-06 08:01:44 +00:00
onChange={({ target }) => {
2023-09-06 13:20:12 +00:00
const clonContent = listQuestions[quizId][totalIndex].content;
2023-09-15 12:37:12 +00:00
clonContent.range = `${target.value}${listQuestions[quizId][totalIndex].content.range.split("—")[1]}`;
2023-09-06 13:20:12 +00:00
updateQuestionsList(quizId, totalIndex, {
content: clonContent,
});
2023-09-06 08:01:44 +00:00
}}
2023-09-12 15:23:56 +00:00
onBlur={({ target }) => {
2023-09-13 08:15:02 +00:00
const start = listQuestions[quizId][totalIndex].content.start;
2023-09-12 15:23:56 +00:00
const min = Number(target.value);
2023-09-15 12:37:12 +00:00
const max = Number(listQuestions[quizId][totalIndex].content.range.split("—")[1]);
2023-09-12 15:23:56 +00:00
if (min >= max) {
const clonContent = listQuestions[quizId][totalIndex].content;
clonContent.range = `${max - 1 >= 0 ? max - 1 : 0}${
2023-09-15 12:37:12 +00:00
listQuestions[quizId][totalIndex].content.range.split("—")[1]
2023-09-12 15:23:56 +00:00
}`;
updateQuestionsList(quizId, totalIndex, {
content: clonContent,
});
}
2023-09-13 08:15:02 +00:00
if (start < min) {
updateQuestionsList(quizId, totalIndex, {
content: {
...listQuestions[quizId][totalIndex].content,
start: min,
},
});
}
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-06 08:01:44 +00:00
placeholder={"100"}
2023-09-12 15:23:56 +00:00
min={0}
max={100}
2023-09-15 12:37:12 +00:00
value={listQuestions[quizId][totalIndex].content.range.split("—")[1]}
2023-09-06 08:01:44 +00:00
onChange={({ target }) => {
2023-09-06 13:20:12 +00:00
const clonContent = listQuestions[quizId][totalIndex].content;
2023-09-15 12:37:12 +00:00
clonContent.range = `${listQuestions[quizId][totalIndex].content.range.split("—")[0]}${target.value}`;
2023-09-06 13:20:12 +00:00
updateQuestionsList(quizId, totalIndex, {
content: clonContent,
});
2023-09-06 08:01:44 +00:00
}}
2023-09-12 15:23:56 +00:00
onBlur={({ target }) => {
2023-09-13 08:15:02 +00:00
const start = listQuestions[quizId][totalIndex].content.start;
const step = listQuestions[quizId][totalIndex].content.step;
2023-09-15 12:37:12 +00:00
const min = Number(listQuestions[quizId][totalIndex].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) {
const clonContent = listQuestions[quizId][totalIndex].content;
2023-09-15 12:37:12 +00:00
clonContent.range = `${listQuestions[quizId][totalIndex].content.range.split("—")[0]}${
min + 1 >= 100 ? 100 : min + 1
}`;
2023-09-12 15:23:56 +00:00
updateQuestionsList(quizId, totalIndex, {
content: clonContent,
});
}
2023-09-13 08:15:02 +00:00
if (start > max) {
updateQuestionsList(quizId, totalIndex, {
content: {
...listQuestions[quizId][totalIndex].content,
start: max,
},
});
}
if (step > max) {
updateQuestionsList(quizId, totalIndex, {
content: {
...listQuestions[quizId][totalIndex].content,
step: max,
},
});
if (range % step) {
2023-09-15 12:37: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-09-21 07:00:08 +00:00
<Typography sx={{ fontWeight: "500", fontSize: "18px", color: "#4D4D4D", mb: "10px" }}>
Начальное значение
</Typography>
2023-09-12 14:36:22 +00:00
<CustomNumberField
2023-09-06 08:01:44 +00:00
placeholder={"50"}
2023-09-15 12:37:12 +00:00
min={Number(listQuestions[quizId][totalIndex].content.range.split("—")[0])}
max={Number(listQuestions[quizId][totalIndex].content.range.split("—")[1])}
2023-09-12 15:23:56 +00:00
value={String(listQuestions[quizId][totalIndex].content.start)}
2023-09-06 08:01:44 +00:00
onChange={({ target }) => {
2023-09-06 13:20:12 +00:00
const clonContent = listQuestions[quizId][totalIndex].content;
2023-09-06 08:01:44 +00:00
clonContent.start = Number(target.value);
2023-09-06 13:20:12 +00:00
updateQuestionsList(quizId, totalIndex, {
content: clonContent,
});
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-21 07:00:08 +00:00
<Typography sx={{ fontWeight: "500", fontSize: "18px", color: "#4D4D4D", mb: "10px" }}>Шаг</Typography>
2023-09-12 14:36:22 +00:00
<CustomNumberField
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-09-12 15:23:56 +00:00
value={String(listQuestions[quizId][totalIndex].content.step)}
2023-09-06 08:01:44 +00:00
onChange={({ target }) => {
2023-09-06 13:20:12 +00:00
const clonContent = listQuestions[quizId][totalIndex].content;
2023-09-06 08:01:44 +00:00
clonContent.step = Number(target.value);
2023-09-06 13:20:12 +00:00
updateQuestionsList(quizId, totalIndex, {
content: clonContent,
});
2023-09-06 08:01:44 +00:00
}}
2023-09-12 15:23:56 +00:00
onBlur={({ target }) => {
2023-09-15 12:37:12 +00:00
const min = Number(listQuestions[quizId][totalIndex].content.range.split("—")[0]);
const max = Number(listQuestions[quizId][totalIndex].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-09-12 15:23:56 +00:00
updateQuestionsList(quizId, totalIndex, {
content: {
...listQuestions[quizId][totalIndex].content,
2023-09-13 07:28:24 +00:00
step: max,
2023-09-12 15:23:56 +00:00
},
});
}
2023-09-12 15:57:48 +00:00
if (range % step) {
2023-09-15 12:37: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-09-15 12:37:12 +00:00
<ButtonsOptions switchState={switchState} SSHC={SSHC} totalIndex={totalIndex} />
2023-08-24 11:09:47 +00:00
<SwitchSlider switchState={switchState} totalIndex={totalIndex} />
</>
);
}