frontPanel/src/pages/Questions/Branching/BranchingModal/Settings.tsx

717 lines
22 KiB
TypeScript
Raw Normal View History

import CalendarIcon from "@icons/CalendarIcon";
import { DeleteIcon } from "@icons/questionsPage/deleteIcon";
2023-12-31 02:53:25 +00:00
import {
Box,
Checkbox,
FormControl,
2023-12-31 02:53:25 +00:00
FormControlLabel,
IconButton,
MenuItem,
2023-12-31 02:53:25 +00:00
Radio,
RadioGroup,
Select,
TextField,
Typography,
useMediaQuery,
useTheme,
2023-12-31 02:53:25 +00:00
} from "@mui/material";
import { SelectChangeEvent } from "@mui/material/Select";
import { DatePicker } from "@mui/x-date-pickers";
2023-12-31 02:53:25 +00:00
import { TimePicker } from "@mui/x-date-pickers/TimePicker";
import RadioCheck from "@ui_kit/RadioCheck";
import RadioIcon from "@ui_kit/RadioIcon";
import moment from "moment";
import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import type { AnyTypedQuizQuestion, QuizQuestionNumber } from "@frontend/squzanswerer";
2023-12-08 11:53:41 +00:00
const CONDITIONS = ["Все условия обязательны", "Обязательно хотя бы одно условие"];
2023-11-29 15:45:15 +00:00
interface Props {
2023-12-31 02:53:25 +00:00
parentQuestion: AnyTypedQuizQuestion;
targetQuestion: AnyTypedQuizQuestion;
ruleIndex: number;
setParentQuestion: (q: AnyTypedQuizQuestion) => void;
2023-11-29 15:45:15 +00:00
}
2024-04-26 14:41:36 +00:00
// Этот компонент вызывается 1 раз на каждое условие родителя для перехода к этому вопросу. Поэтому для изменения стора мы знаем индекс
export const TypeSwitch = ({ parentQuestion, targetQuestion, ruleIndex, setParentQuestion }: Props) => {
2023-12-31 02:53:25 +00:00
switch (parentQuestion.type) {
case "variant":
case "images":
case "varimg":
case "emoji":
case "select":
return parentQuestion.content.variants === undefined ? (
<BlockRule text={"У родителя нет вариантов"} />
) : (
<SelectorType
targetQuestion={targetQuestion}
parentQuestion={parentQuestion}
ruleIndex={ruleIndex}
setParentQuestion={setParentQuestion}
/>
);
break;
case "date":
return (
<DateInputsType
targetQuestion={targetQuestion}
parentQuestion={parentQuestion}
ruleIndex={ruleIndex}
setParentQuestion={setParentQuestion}
/>
);
break;
case "number":
return (
<NumberInputsType
targetQuestion={targetQuestion}
parentQuestion={parentQuestion}
ruleIndex={ruleIndex}
setParentQuestion={setParentQuestion}
/>
);
break;
case "page":
return <BlockRule text={"У такого родителя может быть только один потомок"} />;
2023-12-31 02:53:25 +00:00
break;
case "text":
return (
<TextInputsType
targetQuestion={targetQuestion}
parentQuestion={parentQuestion}
ruleIndex={ruleIndex}
setParentQuestion={setParentQuestion}
/>
);
break;
case "file":
return (
<FileInputsType
targetQuestion={targetQuestion}
parentQuestion={parentQuestion}
ruleIndex={ruleIndex}
setParentQuestion={setParentQuestion}
/>
);
break;
case "rating":
return (
<RatingInputsType
targetQuestion={targetQuestion}
parentQuestion={parentQuestion}
ruleIndex={ruleIndex}
setParentQuestion={setParentQuestion}
/>
);
break;
default:
return <BlockRule text={"Не распознан тип родительского вопроса"} />;
break;
}
};
2023-11-29 15:45:15 +00:00
export const BlockRule = ({ text }: { text: string }) => {
2023-12-31 02:53:25 +00:00
return (
<Typography
sx={{
margin: "100px 0",
textAlign: "center",
}}
>
{text}
</Typography>
);
};
const SelectorType = ({ parentQuestion, targetQuestion, ruleIndex, setParentQuestion }: Props) => {
2023-12-31 02:53:25 +00:00
const theme = useTheme();
const quizId = Number(useParams().quizId);
return (
<Box
sx={{
padding: "20px",
margin: "20px",
borderRadius: "8px",
bgcolor: "#F2F3F7",
height: "280px",
overflow: "auto",
}}
>
<Box
sx={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
pb: "5px",
}}
>
<Typography sx={{ color: "#4D4D4D", fontWeight: "500" }}>Новое условие</Typography>
2023-12-31 02:53:25 +00:00
<IconButton
sx={{ borderRadius: "6px", padding: "2px" }}
onClick={() => {
let newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
newParentQuestion.content.rule.main.splice(ruleIndex, 1);
setParentQuestion(newParentQuestion);
}}
2023-11-29 15:45:15 +00:00
>
2023-12-31 02:53:25 +00:00
<DeleteIcon color={"#4D4D4D"} />
</IconButton>
</Box>
2023-12-31 02:53:25 +00:00
<Box
sx={{
2023-12-31 02:53:25 +00:00
display: "flex",
alignItems: "center",
pb: "10px",
}}
>
<Typography sx={{ color: "#4D4D4D", fontWeight: "500" }}>Дан ответ</Typography>
<Typography sx={{ color: "#7E2AEA", pl: "10px" }}>(Укажите один или несколько вариантов)</Typography>
2023-12-31 02:53:25 +00:00
</Box>
<Select
multiple
value={parentQuestion.content?.rule?.main[ruleIndex]?.rules[0]?.answers || []}
2023-12-31 02:53:25 +00:00
onChange={(event: SelectChangeEvent) => {
let newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
newParentQuestion.content.rule.main[ruleIndex].rules[0].answers = (event.target as HTMLSelectElement).value;
2023-12-31 02:53:25 +00:00
setParentQuestion(newParentQuestion);
}}
2023-12-31 02:53:25 +00:00
sx={{
width: "100%",
height: "48px",
borderRadius: "8px",
"& .MuiOutlinedInput-notchedOutline": {
border: `1px solid ${theme.palette.brightPurple.main} !important`,
height: "48px",
borderRadius: "10px",
},
}}
>
{parentQuestion.content.variants.map((e: any) => {
return <MenuItem value={e.id}>{e.answer}</MenuItem>;
})}
</Select>
<FormControl>
<RadioGroup
aria-labelledby="demo-controlled-radio-buttons-group"
value={parentQuestion.content.rule.main[ruleIndex].or}
onChange={(_, value) => {
let newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
newParentQuestion.content.rule.main[ruleIndex].or = value;
setParentQuestion(newParentQuestion);
}}
>
{CONDITIONS.map((condition, totalIndex) => (
<FormControlLabel
key={totalIndex}
sx={{ color: theme.palette.grey2.main }}
value={Boolean(Number(totalIndex))}
control={
<Radio
checkedIcon={<RadioCheck />}
icon={<RadioIcon />}
/>
2023-12-31 02:53:25 +00:00
}
label={condition}
/>
))}
</RadioGroup>
</FormControl>
</Box>
);
};
const DateInputsType = ({ parentQuestion, targetQuestion, ruleIndex, setParentQuestion }: Props) => {
2023-12-31 02:53:25 +00:00
const theme = useTheme();
const upLg = useMediaQuery(theme.breakpoints.up("md"));
2024-05-17 02:52:53 +00:00
const time = moment(new Date());
2023-12-31 02:53:25 +00:00
const [firstDate, setFirstDate] = useState(time);
const [secondDate, setSecondDate] = useState(time);
const [firstTime, setFirstTime] = useState(time);
const [secondTime, setSecondTime] = useState(time);
useEffect(() => {
let newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
newParentQuestion.content.rule.main[ruleIndex].rules[0].answers[0] = time;
if (newParentQuestion.content.dateRange) parentQuestion.content.rule.main[ruleIndex].rules[0].answers[1] = time;
2023-12-31 02:53:25 +00:00
setParentQuestion(newParentQuestion);
}, [firstDate, secondDate, firstTime, secondTime]);
return (
<Box
sx={{
padding: "20px",
margin: "20px",
borderRadius: "8px",
bgcolor: "#F2F3F7",
height: "280px",
overflow: "auto",
}}
>
2023-12-31 02:53:25 +00:00
<Box
sx={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
pb: "5px",
}}
>
<Typography sx={{ color: "#4D4D4D", fontWeight: "500" }}>Новое условие</Typography>
2023-12-31 02:53:25 +00:00
<IconButton
sx={{ borderRadius: "6px", padding: "2px" }}
onClick={() => {
let newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
newParentQuestion.content.rule.main.splice(ruleIndex, 1);
setParentQuestion(newParentQuestion);
}}
>
2023-12-31 02:53:25 +00:00
<DeleteIcon color={"#4D4D4D"} />
</IconButton>
</Box>
2023-11-29 15:45:15 +00:00
2023-12-31 02:53:25 +00:00
<Box
sx={{
display: "flex",
alignItems: "center",
pb: "10px",
}}
>
<Typography sx={{ color: "#4D4D4D", fontWeight: "500" }}>Дан ответ</Typography>
<Typography sx={{ color: "#7E2AEA", pl: "10px" }}>(Укажите один или несколько вариантов)</Typography>
2023-12-31 02:53:25 +00:00
</Box>
<Box
sx={{
backgroundColor: "#E8EAEE",
margin: "10px",
}}
>
{parentQuestion.content.dateRange && (
<Typography sx={{ color: "#4D4D4D", p: "10px" }}>(Начало периода)</Typography>
2023-12-31 02:53:25 +00:00
)}
<DatePicker
2024-05-17 02:52:53 +00:00
defaultValue={moment(
new Date(parentQuestion.content.rule.main[ruleIndex].rules[0].answers[0]).toLocaleDateString()
2023-12-31 02:53:25 +00:00
)}
onChange={(dateString) => {
const date = dateString?.toDate().toLocaleDateString("ru-RU", {
year: "numeric",
month: "2-digit",
day: "2-digit",
});
2023-12-31 02:53:25 +00:00
let newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
newParentQuestion.content.rule.main[ruleIndex].rules[0].answers = [date];
2023-12-31 02:53:25 +00:00
setParentQuestion(newParentQuestion);
}}
slots={{
openPickerIcon: () => <CalendarIcon />,
}}
slotProps={{
openPickerButton: {
sx: {
p: 0,
},
"data-cy": "open-datepicker",
},
}}
sx={{
p: "10px",
"& .MuiInputBase-root": {
minWidth: "325px",
backgroundColor: "#F2F3F7",
borderRadius: "10px",
pr: "31px",
"& input": {
py: "11px",
pl: upLg ? "20px" : "13px",
lineHeight: "19px",
},
"& fieldset": {
borderColor: "#9A9AAF",
},
},
}}
/>
{parentQuestion.content.time && (
<TimePicker
value={parentQuestion.content.rule.main[ruleIndex].rules[0].answers[0]}
sx={{
2023-12-31 02:53:25 +00:00
p: "10px",
"& .MuiInputBase-root": {
minWidth: "325px",
backgroundColor: "#F2F3F7",
borderRadius: "10px",
pr: "22px",
"& input": {
py: "11px",
pl: upLg ? "20px" : "13px",
lineHeight: "19px",
},
"& fieldset": {
borderColor: "#9A9AAF",
},
},
}}
2023-12-31 02:53:25 +00:00
/>
)}
</Box>
2023-12-31 02:53:25 +00:00
{parentQuestion.content.dateRange && (
<Box
2023-12-31 02:53:25 +00:00
sx={{
backgroundColor: "#E8EAEE",
margin: "10px",
}}
>
2023-12-31 02:53:25 +00:00
{parentQuestion.content.dateRange && (
<Typography sx={{ color: "#4D4D4D", p: "10px" }}>(Конец периода)</Typography>
2023-12-31 02:53:25 +00:00
)}
<DatePicker
value={parentQuestion.content.rule.main[ruleIndex].rules[0].answers[1]}
2023-12-31 02:53:25 +00:00
onChange={() => {}}
slots={{
openPickerIcon: () => <CalendarIcon />,
}}
slotProps={{
openPickerButton: {
sx: {
p: 0,
},
"data-cy": "open-datepicker",
},
}}
sx={{
p: "10px",
"& .MuiInputBase-root": {
minWidth: "325px",
backgroundColor: "#F2F3F7",
borderRadius: "10px",
pr: "31px",
"& input": {
py: "11px",
pl: upLg ? "20px" : "13px",
lineHeight: "19px",
},
"& fieldset": {
borderColor: "#9A9AAF",
},
},
}}
/>
{parentQuestion.content.time && (
<TimePicker
value={parentQuestion.content.rule.main[ruleIndex].rules[0].answers[1]}
2023-12-31 02:53:25 +00:00
sx={{
p: "10px",
"& .MuiInputBase-root": {
minWidth: "325px",
backgroundColor: "#F2F3F7",
borderRadius: "10px",
pr: "22px",
"& input": {
py: "11px",
pl: upLg ? "20px" : "13px",
lineHeight: "19px",
},
"& fieldset": {
borderColor: "#9A9AAF",
},
},
}}
/>
2023-12-31 02:53:25 +00:00
)}
</Box>
2023-12-31 02:53:25 +00:00
)}
</Box>
);
};
const NumberInputsType = ({ parentQuestion, targetQuestion, ruleIndex, setParentQuestion }: Props) => {
2023-12-31 02:53:25 +00:00
const theme = useTheme();
const quizId = Number(useParams().quizId);
return (
<Box
sx={{
padding: "20px",
margin: "20px",
borderRadius: "8px",
bgcolor: "#F2F3F7",
height: "280px",
overflow: "auto",
}}
>
<Box
sx={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
pb: "5px",
}}
>
<Typography sx={{ color: "#4D4D4D", fontWeight: "500" }}>Новое условие</Typography>
2023-12-31 02:53:25 +00:00
<IconButton
sx={{ borderRadius: "6px", padding: "2px" }}
onClick={() => {
let newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
newParentQuestion.content.rule.main.splice(ruleIndex, 1);
setParentQuestion(newParentQuestion);
}}
2023-11-29 15:45:15 +00:00
>
2023-12-31 02:53:25 +00:00
<DeleteIcon color={"#4D4D4D"} />
</IconButton>
</Box>
2023-11-29 15:45:15 +00:00
2023-12-31 02:53:25 +00:00
<Box
sx={{
display: "flex",
alignItems: "center",
pb: "10px",
}}
>
<Typography sx={{ color: "#4D4D4D", fontWeight: "500" }}>Дан ответ</Typography>
<Typography sx={{ color: "#7E2AEA", pl: "10px" }}>(Укажите один или несколько вариантов)</Typography>
2023-12-31 02:53:25 +00:00
</Box>
<Box>
<TextField
sx={{ marginTop: "20px", width: "100%" }}
placeholder="от"
value={parentQuestion.content.rule.main[ruleIndex].rules[0].answers[0]?.split("—")[0]}
2023-12-31 02:53:25 +00:00
onChange={({ target }: React.ChangeEvent<HTMLInputElement>) => {
const newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
const previousValue = newParentQuestion.content.rule.main[ruleIndex].rules[0].answers[0];
newParentQuestion.content.rule.main[ruleIndex].rules[0].answers[0] = (parentQuestion as QuizQuestionNumber)
.content.chooseRange
? previousValue
? `${target.value}${previousValue.split("—")[1] || 0}`
: `${target.value}—0`
: target.value;
2023-12-31 02:53:25 +00:00
setParentQuestion(newParentQuestion);
}}
/>
{(parentQuestion as QuizQuestionNumber).content.chooseRange && (
<TextField
placeholder="до"
sx={{ marginTop: "20px", width: "100%" }}
value={parentQuestion.content.rule.main[ruleIndex].rules[0].answers[0]?.split("—")[1]}
2023-12-31 02:53:25 +00:00
onChange={({ target }: React.ChangeEvent<HTMLInputElement>) => {
const newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
const previousValue = newParentQuestion.content.rule.main[ruleIndex].rules[0].answers[0];
newParentQuestion.content.rule.main[ruleIndex].rules[0].answers[0] = previousValue
2023-12-31 02:53:25 +00:00
? `${previousValue.split("—")[0] || 0}${target.value}`
: `0—${target.value}`;
setParentQuestion(newParentQuestion);
2023-11-29 15:45:15 +00:00
}}
2023-12-31 02:53:25 +00:00
/>
)}
</Box>
</Box>
);
};
const TextInputsType = ({ parentQuestion, targetQuestion, ruleIndex, setParentQuestion }: Props) => {
2023-12-31 02:53:25 +00:00
const theme = useTheme();
const quizId = Number(useParams().quizId);
return (
<Box
sx={{
padding: "20px",
margin: "20px",
borderRadius: "8px",
bgcolor: "#F2F3F7",
height: "280px",
overflow: "auto",
}}
>
<Box
sx={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
pb: "5px",
}}
>
<Typography sx={{ color: "#4D4D4D", fontWeight: "500" }}>Новое условие</Typography>
2023-12-31 02:53:25 +00:00
<IconButton
sx={{ borderRadius: "6px", padding: "2px" }}
onClick={() => {
let newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
newParentQuestion.content.rule.main.splice(ruleIndex, 1);
setParentQuestion(newParentQuestion);
}}
2023-11-29 15:45:15 +00:00
>
2023-12-31 02:53:25 +00:00
<DeleteIcon color={"#4D4D4D"} />
</IconButton>
</Box>
2023-11-29 15:45:15 +00:00
2023-12-31 02:53:25 +00:00
<Box
sx={{
display: "inline",
alignItems: "center",
pb: "10px",
}}
>
<Typography sx={{ color: "#4D4D4D", fontWeight: "500" }}>Дан ответ</Typography>
2023-12-31 02:53:25 +00:00
<Typography sx={{ color: "#7E2AEA", pl: "10px", fontSize: "12px" }}>
(Укажите текст, при совпадении с которым пользователь попадёт на этот вопрос)
2023-12-31 02:53:25 +00:00
</Typography>
</Box>
<TextField
sx={{
marginTop: "20px",
width: "100%",
}}
value={parentQuestion.content.rule.main[ruleIndex].rules[0].answers[0]}
onChange={(event: React.FormEvent<HTMLInputElement>) => {
let newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
newParentQuestion.content.rule.main[ruleIndex].rules[0].answers = [(event.target as HTMLInputElement).value];
2023-12-31 02:53:25 +00:00
setParentQuestion(newParentQuestion);
}}
/>
</Box>
);
};
const FileInputsType = ({ parentQuestion, targetQuestion, ruleIndex, setParentQuestion }: Props) => {
2023-12-31 02:53:25 +00:00
const theme = useTheme();
const quizId = Number(useParams().quizId);
return (
<Box
sx={{
padding: "20px",
margin: "20px",
borderRadius: "8px",
bgcolor: "#F2F3F7",
height: "280px",
overflow: "auto",
}}
>
<Box
sx={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
pb: "5px",
}}
>
<Typography sx={{ color: "#4D4D4D", fontWeight: "500" }}>Новое условие</Typography>
2023-12-31 02:53:25 +00:00
<IconButton
sx={{ borderRadius: "6px", padding: "2px" }}
onClick={() => {
let newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
newParentQuestion.content.rule.main.splice(ruleIndex, 1);
setParentQuestion(newParentQuestion);
}}
2023-11-29 15:45:15 +00:00
>
2023-12-31 02:53:25 +00:00
<DeleteIcon color={"#4D4D4D"} />
</IconButton>
</Box>
2023-11-29 15:45:15 +00:00
2023-12-31 02:53:25 +00:00
<Box
sx={{
display: "inline",
alignItems: "center",
pb: "10px",
}}
>
<Typography sx={{ color: "#4D4D4D", fontWeight: "500" }}>
Перевести на этот вопрос если пользователь загрузил файл
</Typography>
</Box>
<FormControlLabel
control={
<Checkbox
2023-11-29 15:45:15 +00:00
sx={{
2023-12-31 02:53:25 +00:00
margin: 0,
2023-11-29 15:45:15 +00:00
}}
checked={parentQuestion.content.rule.main[ruleIndex].rules[0].answers[0]}
2023-12-31 02:53:25 +00:00
onChange={(event: React.FormEvent<HTMLInputElement>) => {
let newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
newParentQuestion.content.rule.main[ruleIndex].rules[0].answers = [
(event.target as HTMLInputElement).checked,
];
2023-12-31 02:53:25 +00:00
setParentQuestion(newParentQuestion);
}}
/>
}
label="да"
/>
</Box>
);
};
const RatingInputsType = ({ parentQuestion, targetQuestion, ruleIndex, setParentQuestion }: Props) => {
2023-12-31 02:53:25 +00:00
const theme = useTheme();
const quizId = Number(useParams().quizId);
return (
<Box
sx={{
padding: "20px",
margin: "20px",
borderRadius: "8px",
bgcolor: "#F2F3F7",
height: "280px",
overflow: "auto",
}}
>
<Box
sx={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
pb: "5px",
}}
>
<Typography sx={{ color: "#4D4D4D", fontWeight: "500" }}>Новое условие</Typography>
2023-12-31 02:53:25 +00:00
<IconButton
sx={{ borderRadius: "6px", padding: "2px" }}
onClick={() => {
let newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
newParentQuestion.content.rule.main.splice(ruleIndex, 1);
setParentQuestion(newParentQuestion);
}}
2023-11-29 15:45:15 +00:00
>
2023-12-31 02:53:25 +00:00
<DeleteIcon color={"#4D4D4D"} />
</IconButton>
</Box>
2023-11-29 15:45:15 +00:00
2023-12-31 02:53:25 +00:00
<Box
sx={{
display: "inline",
alignItems: "center",
pb: "10px",
}}
>
<Typography sx={{ color: "#7E2AEA", pl: "10px", fontSize: "12px" }}>
Ожидаемое количество ячеек(не более доступного количества)
</Typography>
</Box>
<TextField
sx={{
marginTop: "20px",
width: "100%",
}}
value={parentQuestion.content.rule.main[ruleIndex].rules[0].answers[0]}
onChange={(event: React.FormEvent<HTMLInputElement>) => {
let newParentQuestion = JSON.parse(JSON.stringify(parentQuestion));
let valueNumber = Number((event.target as HTMLInputElement).value.replace(/[^0-9,\s]/g, ""));
valueNumber = valueNumber > parentQuestion.content.steps ? parentQuestion.content.steps : valueNumber;
newParentQuestion.content.rule.main[ruleIndex].rules[0].answers = [valueNumber];
2023-12-31 02:53:25 +00:00
setParentQuestion(newParentQuestion);
}}
/>
</Box>
);
};