feat: View Number component logic

This commit is contained in:
IlyaDoronin 2023-12-08 14:53:41 +03:00
parent 5d3f19d952
commit caafb87ae0
4 changed files with 48 additions and 55 deletions

@ -6,7 +6,6 @@ import { useState, useRef, useEffect } from "react";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { useQuestionsStore } from "@root/questions/store"; import { useQuestionsStore } from "@root/questions/store";
import { updateQuestion, getQuestionById } from "@root/questions/actions"; import { updateQuestion, getQuestionById } from "@root/questions/actions";
import { AnyTypedQuizQuestion } from "../../../model/questionTypes/shared"
import { SelectChangeEvent } from '@mui/material/Select'; import { SelectChangeEvent } from '@mui/material/Select';
import CalendarIcon from "@icons/CalendarIcon"; import CalendarIcon from "@icons/CalendarIcon";
import { DatePicker } from "@mui/x-date-pickers"; import { DatePicker } from "@mui/x-date-pickers";
@ -15,6 +14,9 @@ import { TimePicker } from '@mui/x-date-pickers/TimePicker';
import InfoIcon from "@icons/Info"; import InfoIcon from "@icons/Info";
import { DeleteIcon } from "@icons/questionsPage/deleteIcon"; import { DeleteIcon } from "@icons/questionsPage/deleteIcon";
import type { AnyTypedQuizQuestion } from "../../../model/questionTypes/shared"
import type { QuizQuestionNumber } from "../../../model/questionTypes/number"
const CONDITIONS = [ const CONDITIONS = [
"Все условия обязательны", "Все условия обязательны",
"Обязательно хотя бы одно условие", "Обязательно хотя бы одно условие",
@ -514,47 +516,34 @@ const NumberInputsType = ({ parentQuestion, targetQuestion, ruleIndex, setParent
(Укажите один или несколько вариантов) (Укажите один или несколько вариантов)
</Typography> </Typography>
</Box> </Box>
<TextField <Box>
sx={{
marginTop: "20px",
width: "100%"
}}
placeholder="от"
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[0] = Number((event.target as HTMLInputElement).value.replace(/[^0-9,\s]/g, ""))
if (newParentQuestion.content.rule.main[ruleIndex].rules[0].answers[1] === undefined) newParentQuestion.content.rule.main[ruleIndex].rules[0].answers[1] = 0
setParentQuestion(newParentQuestion)
}}
/>
{parentQuestion.content.chooseRange &&
<TextField <TextField
placeholder="до" sx={{ marginTop: "20px", width: "100%" }}
sx={{ placeholder="от"
marginTop: "20px", value={parentQuestion.content.rule.main[ruleIndex].rules[0].answers[0]?.split("—")[0]}
width: "100%" onChange={({ target }: React.ChangeEvent<HTMLInputElement>) => {
}} const newParentQuestion = JSON.parse(JSON.stringify(parentQuestion))
const previousValue = newParentQuestion.content.rule.main[ruleIndex].rules[0].answers[0];
value={parentQuestion.content.rule.main[ruleIndex].rules[0].answers[1]} 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;
onChange={(event: React.FormEvent<HTMLInputElement>) => {
let newParentQuestion = JSON.parse(JSON.stringify(parentQuestion))
newParentQuestion.content.rule.main[ruleIndex].rules[0].answers[1] = Number((event.target as HTMLInputElement).value.replace(/[^0-9,\s]/g, ""))
if (newParentQuestion.content.rule.main[ruleIndex].rules[0].answers[0] === undefined) newParentQuestion.content.rule.main[ruleIndex].rules[0].answers[0] = 0
setParentQuestion(newParentQuestion) 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]}
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 ? `${previousValue.split("—")[0] || 0}${target.value}` : `0—${target.value}`
setParentQuestion(newParentQuestion)
} }}
/>
}
</Box>
</Box > </Box >
) )
} }

@ -22,7 +22,7 @@ import type { AnyTypedQuizQuestion } from "../../model/questionTypes/shared";
import { useCurrentQuiz } from "@root/quizes/hooks"; import { useCurrentQuiz } from "@root/quizes/hooks";
import { enqueueSnackbar } from "notistack"; import { enqueueSnackbar } from "notistack";
import { useQuestionsStore } from "@root/questions/store"; import { useQuestionsStore } from "@root/questions/store";
import { updateOpenedModalSettingsId } from "@root/questions/actions";
interface Props { interface Props {
switchState: string; switchState: string;
@ -175,8 +175,9 @@ export default function ButtonsOptions({
<MiniButtonSetting <MiniButtonSetting
key={title} key={title}
onClick={() => { onClick={() => {
SSHC(value); openedModal()
myFunc(question); // SSHC(value);
// myFunc(question);
}} }}
sx={{ sx={{
backgroundColor: backgroundColor:

@ -27,7 +27,7 @@ export const Number = ({ currentQuestion }: NumberProps) => {
updateAnswer( updateAnswer(
currentQuestion.content.id, currentQuestion.content.id,
currentQuestion.content.chooseRange currentQuestion.content.chooseRange
? `${currentQuestion.content.start},${max}` ? `${currentQuestion.content.start}${max}`
: String(currentQuestion.content.start), : String(currentQuestion.content.start),
false false
); );
@ -50,7 +50,7 @@ export const Number = ({ currentQuestion }: NumberProps) => {
placeholder="0" placeholder="0"
value={ value={
currentQuestion.content.chooseRange currentQuestion.content.chooseRange
? answer?.split(",")[0] ? answer?.split("")[0]
: answer : answer
} }
onChange={({ target }) => { onChange={({ target }) => {
@ -81,18 +81,18 @@ export const Number = ({ currentQuestion }: NumberProps) => {
placeholder="0" placeholder="0"
value={ value={
currentQuestion.content.chooseRange currentQuestion.content.chooseRange
? answer?.split(",")[0] ? answer?.split("")[0]
: answer : answer
} }
onChange={({ target }) => { onChange={({ target }) => {
updateAnswer( updateAnswer(
currentQuestion.content.id, currentQuestion.content.id,
window.Number(target.value) > window.Number(target.value) >
window.Number(answer?.split(",")[1]) window.Number(answer?.split("")[1])
? `${answer?.split(",")[1]},${answer?.split(",")[1]}` ? `${answer?.split("—")[1]}${answer?.split("—")[1]}`
: window.Number(target.value) < min : window.Number(target.value) < min
? `${min},${answer?.split(",")[1]}` ? `${min}${answer?.split("—")[1]}`
: `${target.value},${answer?.split(",")[1]}` : `${target.value}${answer?.split("—")[1]}`
); );
}} }}
sx={{ sx={{
@ -102,16 +102,16 @@ export const Number = ({ currentQuestion }: NumberProps) => {
/> />
<CustomTextField <CustomTextField
placeholder="0" placeholder="0"
value={answer?.split(",")[1]} value={answer?.split("")[1]}
onChange={({ target }) => { onChange={({ target }) => {
updateAnswer( updateAnswer(
currentQuestion.content.id, currentQuestion.content.id,
window.Number(target.value) > max window.Number(target.value) > max
? `${answer?.split(",")[0]},${max}` ? `${answer?.split("—")[0]}${max}`
: window.Number(target.value) < : window.Number(target.value) <
window.Number(answer?.split(",")[0]) window.Number(answer?.split("")[0])
? `${answer?.split(",")[0]},${answer?.split(",")[0]}` ? `${answer?.split("—")[0]}${answer?.split("—")[0]}`
: `${answer?.split(",")[0]},${target.value}` : `${answer?.split("—")[0]}${target.value}`
); );
}} }}
sx={{ sx={{
@ -124,8 +124,8 @@ export const Number = ({ currentQuestion }: NumberProps) => {
<Slider <Slider
value={ value={
currentQuestion.content.chooseRange currentQuestion.content.chooseRange
? answer?.split(",").length || 0 > 1 ? answer?.split("").length || 0 > 1
? answer?.split(",").map((item) => window.Number(item)) ? answer?.split("").map((item) => window.Number(item))
: [min, min + 1] : [min, min + 1]
: window.Number(answer || 1) : window.Number(answer || 1)
} }
@ -138,7 +138,10 @@ export const Number = ({ currentQuestion }: NumberProps) => {
marginTop: "25px", marginTop: "25px",
}} }}
onChange={(_, value) => { onChange={(_, value) => {
updateAnswer(currentQuestion.content.id, String(value)); updateAnswer(
currentQuestion.content.id,
String(value).replace(",", "—")
);
}} }}
/> />
</Box> </Box>

@ -288,7 +288,7 @@ export const createTypedQuestion = async (
title: question.title, title: question.title,
description: question.description, description: question.description,
page: 0, page: 0,
required: true, required: false,
content: JSON.stringify(defaultQuestionByType[type].content), content: JSON.stringify(defaultQuestionByType[type].content),
}); });