2023-11-29 15:45:15 +00:00
import { Box , MenuItem , FormControl , Checkbox , FormControlLabel , Radio , RadioGroup , Typography , useTheme , Select , Chip , IconButton , TextField } from "@mui/material"
import RadioCheck from "@ui_kit/RadioCheck"
import RadioIcon from "@ui_kit/RadioIcon"
import { QuizQuestionBase } from "model/questionTypes/shared"
import { useState , useRef , useEffect } from "react" ;
import { useParams } from "react-router-dom" ;
import { useQuestionsStore } from "@root/questions/store" ;
import { updateQuestion , getQuestionById } from "@root/questions/actions" ;
import { AnyQuizQuestion } from "../../../model/questionTypes/shared"
import { SelectChangeEvent } from '@mui/material/Select' ;
import InfoIcon from "@icons/Info" ;
import { DeleteIcon } from "@icons/questionsPage/deleteIcon" ;
const CONDITIONS = [
"В с е условия обязательны" ,
"Обязательно хотя бы одно условие" ,
] ;
interface Props {
parentQuestion : AnyQuizQuestion ;
targetQuestion : AnyQuizQuestion ;
ruleIndex : number ;
2023-12-03 02:16:53 +00:00
setParentQuestion : ( q :AnyQuizQuestion ) = > void ;
2023-11-29 15:45:15 +00:00
}
//Этот компонент вызывается 1 раз на каждое условие родителя для перехода к этому вопросу. Поэтому для изменения стора мы знаем индекс
2023-12-03 02:16:53 +00:00
export const TypeSwitch = ( { parentQuestion , targetQuestion , ruleIndex , setParentQuestion } : Props ) = > {
console . log ( "index " + ruleIndex )
2023-11-29 15:45:15 +00:00
switch ( parentQuestion . type ) {
// case 'nonselected':
// return <BlockRule text={"Н е выбран тип родительского вопроса"} />
// break;
case "variant" :
case "images" :
case "varimg" :
case "emoji" :
case "select" :
return ( parentQuestion . content . variants === undefined ? < BlockRule text = { "У родителя нет вариантов" } / > :
2023-12-03 02:16:53 +00:00
< SelectorType targetQuestion = { targetQuestion } parentQuestion = { parentQuestion } ruleIndex = { ruleIndex } setParentQuestion = { setParentQuestion } / >
2023-11-29 15:45:15 +00:00
//Реализован
)
break ;
case "date" :
2023-12-03 02:16:53 +00:00
// return <DateInputsType targetQuestion={targetQuestion} parentQuestion={parentQuestion} ruleIndex={ruleIndex} setParentQuestion={setParentQuestion} />
2023-11-29 15:45:15 +00:00
return < > < / >
break ;
case "number" :
2023-12-03 02:16:53 +00:00
return < NumberInputsType targetQuestion = { targetQuestion } parentQuestion = { parentQuestion } ruleIndex = { ruleIndex } setParentQuestion = { setParentQuestion } / >
2023-11-29 15:45:15 +00:00
//Реализован
break ;
case "page" :
case "date" :
return < BlockRule text = { "У такого родителя может быть только один потомок" } / >
break ;
case "text" :
2023-12-03 02:16:53 +00:00
return < TextInputsType targetQuestion = { targetQuestion } parentQuestion = { parentQuestion } ruleIndex = { ruleIndex } setParentQuestion = { setParentQuestion } / >
2023-11-29 15:45:15 +00:00
//Реализован
break ;
case "file" :
2023-12-03 02:16:53 +00:00
return < FileInputsType targetQuestion = { targetQuestion } parentQuestion = { parentQuestion } ruleIndex = { ruleIndex } setParentQuestion = { setParentQuestion } / >
2023-11-29 15:45:15 +00:00
//Реализован
break ;
case "rating" :
2023-12-03 02:16:53 +00:00
return < RatingInputsType targetQuestion = { targetQuestion } parentQuestion = { parentQuestion } ruleIndex = { ruleIndex } setParentQuestion = { setParentQuestion } / >
2023-11-29 15:45:15 +00:00
//Реализован
break ;
default :
return < BlockRule text = { "Н е распознан тип родительского вопроса" } / >
break ;
}
}
export const BlockRule = ( { text } : { text : string } ) = > {
return (
< Typography
sx = { {
margin : "100px 0" ,
textAlign : "center"
} }
> { text } < / Typography >
)
}
2023-12-03 02:16:53 +00:00
const SelectorType = ( { parentQuestion , targetQuestion , ruleIndex , setParentQuestion } : Props ) = > {
2023-11-29 15:45:15 +00:00
const theme = useTheme ( ) ;
const quizId = Number ( useParams ( ) . quizId ) ;
2023-12-03 02:16:53 +00:00
console . log ( parentQuestion )
2023-11-29 15:45:15 +00:00
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 >
< IconButton
sx = { { borderRadius : "6px" , padding : "2px" } }
onClick = { ( ) = > {
2023-12-03 02:16:53 +00:00
let newParentQuestion = JSON . parse ( JSON . stringify ( parentQuestion ) )
2023-11-29 15:45:15 +00:00
newParentQuestion . content . rule . main . splice ( ruleIndex , 1 )
2023-12-03 02:16:53 +00:00
setParentQuestion ( newParentQuestion )
2023-11-29 15:45:15 +00:00
} }
>
< DeleteIcon color = { "#4D4D4D" } / >
< / IconButton >
< / Box >
< Box
sx = { {
display : "flex" ,
alignItems : "center" ,
pb : "10px" ,
} }
>
< Typography sx = { { color : "#4D4D4D" , fontWeight : "500" } } >
Д а н о т в е т
< / Typography >
< Typography sx = { { color : "#7E2AEA" , pl : "10px" } } >
( У к а ж и т е о д и н и л и н е с к о л ь к о в а р и а н т о в )
< / Typography >
< / Box >
< Select
multiple
2023-12-03 02:16:53 +00:00
value = { parentQuestion . content . rule . main [ ruleIndex ] . rules [ 0 ] . answers || [ ] }
2023-11-29 15:45:15 +00:00
onChange = { ( event : SelectChangeEvent ) = > {
2023-12-03 02:16:53 +00:00
let newParentQuestion = JSON . parse ( JSON . stringify ( parentQuestion ) )
newParentQuestion . content . rule . main [ ruleIndex ] . rules [ 0 ] . answers = ( event . target as HTMLSelectElement ) . value
setParentQuestion ( newParentQuestion )
console . log ( parentQuestion . content . rule . main [ ruleIndex ] )
console . log ( parentQuestion . content . rule . main [ ruleIndex ] . rules [ 0 ] )
console . log ( parentQuestion . content . rule . main [ ruleIndex ] . rules [ 0 ] . answers )
2023-11-29 15:45:15 +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 ) = > {
2023-12-03 02:16:53 +00:00
let newParentQuestion = JSON . parse ( JSON . stringify ( parentQuestion ) )
newParentQuestion . content . rule . main [ ruleIndex ] . or = value
setParentQuestion ( newParentQuestion )
2023-11-29 15:45:15 +00:00
} }
>
{ CONDITIONS . map ( ( condition , totalIndex ) = > (
< FormControlLabel
key = { totalIndex }
sx = { { color : theme.palette.grey2.main } }
value = { Boolean ( Number ( totalIndex ) ) }
control = {
< Radio
checkedIcon = { < RadioCheck / > }
icon = { < RadioIcon / > }
/ >
}
label = { condition }
/ >
) ) }
< / RadioGroup >
< / FormControl >
< / B o x >
)
}
2023-12-03 02:16:53 +00:00
const DateInputsType = ( { parentQuestion , targetQuestion , ruleIndex , setParentQuestion } : Props ) = > {
2023-11-29 15:45:15 +00:00
return < > < / >
}
2023-12-03 02:16:53 +00:00
const NumberInputsType = ( { parentQuestion , targetQuestion , ruleIndex , setParentQuestion } : Props ) = > {
2023-11-29 15:45:15 +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 >
< IconButton
sx = { { borderRadius : "6px" , padding : "2px" } }
onClick = { ( ) = > {
2023-12-03 02:16:53 +00:00
let newParentQuestion = JSON . parse ( JSON . stringify ( parentQuestion ) )
2023-11-29 15:45:15 +00:00
newParentQuestion . content . rule . main . splice ( ruleIndex , 1 )
2023-12-03 02:16:53 +00:00
setParentQuestion ( newParentQuestion )
2023-11-29 15:45:15 +00:00
} }
>
< DeleteIcon color = { "#4D4D4D" } / >
< / IconButton >
< / Box >
< Box
sx = { {
display : "flex" ,
alignItems : "center" ,
pb : "10px" ,
} }
>
< Typography sx = { { color : "#4D4D4D" , fontWeight : "500" } } >
Д а н о т в е т
< / Typography >
< Typography sx = { { color : "#7E2AEA" , pl : "10px" } } >
( У к а ж и т е о д и н и л и н е с к о л ь к о в а р и а н т о в )
< / Typography >
< / Box >
{ / * < T e x t F i e l d
sx = { {
marginTop : "20px" ,
width : "100%"
} }
value = { parentQuestion . content . rule . main [ ruleIndex ] . rules [ 0 ] . answers [ 0 ] }
onChange = { ( event : React.FormEvent < HTMLInputElement > ) = > {
2023-12-03 02:16:53 +00:00
let newParentQuestion = JSON . parse ( JSON . stringify ( parentQuestion ) )
newParentQuestion . content . rule . main [ ruleIndex ] . rules [ 0 ] . answers = [ ( event . target as HTMLInputElement ) . value . replace ( /[^0-9,\s]/g , "" ) ]
setParentQuestion ( newParentQuestion )
2023-11-29 15:45:15 +00:00
} }
/> */ }
< / B o x >
)
}
2023-12-03 02:16:53 +00:00
const TextInputsType = ( { parentQuestion , targetQuestion , ruleIndex , setParentQuestion } : Props ) = > {
2023-11-29 15:45:15 +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 >
< IconButton
sx = { { borderRadius : "6px" , padding : "2px" } }
onClick = { ( ) = > {
2023-12-03 02:16:53 +00:00
let newParentQuestion = JSON . parse ( JSON . stringify ( parentQuestion ) )
2023-11-29 15:45:15 +00:00
newParentQuestion . content . rule . main . splice ( ruleIndex , 1 )
2023-12-03 02:16:53 +00:00
setParentQuestion ( newParentQuestion )
2023-11-29 15:45:15 +00:00
} }
>
< DeleteIcon color = { "#4D4D4D" } / >
< / IconButton >
< / Box >
< Box
sx = { {
display : "inline" ,
alignItems : "center" ,
pb : "10px" ,
} }
>
< Typography sx = { { color : "#4D4D4D" , fontWeight : "500" } } >
Д а н о т в е т
< / Typography >
< Typography sx = { { color : "#7E2AEA" , pl : "10px" , fontSize : "12px" } } >
( У к а ж и т е т е к с т , п р и с о в п а д е н и и с к о т о р ы м п о л ь з о в а т е л ь п о п а д ё т н а э т о т в о п р о с )
< / Typography >
< / Box >
{ / * < T e x t F i e l d
sx = { {
marginTop : "20px" ,
width : "100%"
} }
value = { parentQuestion . content . rule . main [ ruleIndex ] . rules [ 0 ] . answers [ 0 ] }
onChange = { ( event : React.FormEvent < HTMLInputElement > ) = > {
2023-12-03 02:16:53 +00:00
let newParentQuestion = JSON . parse ( JSON . stringify ( parentQuestion ) )
2023-11-29 15:45:15 +00:00
newParentQuestion . content . rule . main [ ruleIndex ] . rules [ 0 ] . answers = [ ( event . target as HTMLInputElement ) . value ]
2023-12-03 02:16:53 +00:00
setParentQuestion ( newParentQuestion )
2023-11-29 15:45:15 +00:00
} }
/> */ }
< / B o x >
)
}
2023-12-03 02:16:53 +00:00
const FileInputsType = ( { parentQuestion , targetQuestion , ruleIndex , setParentQuestion } : Props ) = > {
2023-11-29 15:45:15 +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 >
< IconButton
sx = { { borderRadius : "6px" , padding : "2px" } }
onClick = { ( ) = > {
2023-12-03 02:16:53 +00:00
let newParentQuestion = JSON . parse ( JSON . stringify ( parentQuestion ) )
2023-11-29 15:45:15 +00:00
newParentQuestion . content . rule . main . splice ( ruleIndex , 1 )
2023-12-03 02:16:53 +00:00
setParentQuestion ( newParentQuestion )
2023-11-29 15:45:15 +00:00
} }
>
< DeleteIcon color = { "#4D4D4D" } / >
< / IconButton >
< / Box >
< Box
sx = { {
display : "inline" ,
alignItems : "center" ,
pb : "10px" ,
} }
>
< Typography sx = { { color : "#4D4D4D" , fontWeight : "500" } } >
П е р е в е с т и н а э т о т в о п р о с е с л и п о л ь з о в а т е л ь з а г р у з и л ф а й л
< / Typography >
< / Box >
< FormControlLabel control = { < Checkbox
sx = { {
margin : 0
} }
checked = { parentQuestion . content . rule . main [ ruleIndex ] . rules [ 0 ] . answers [ 0 ] }
onChange = { ( event : React.FormEvent < HTMLInputElement > ) = > {
2023-12-03 02:16:53 +00:00
let newParentQuestion = JSON . parse ( JSON . stringify ( parentQuestion ) )
newParentQuestion . content . rule . main [ ruleIndex ] . rules [ 0 ] . answers = [ ( event . target as HTMLInputElement ) . checked ]
setParentQuestion ( newParentQuestion )
2023-11-29 15:45:15 +00:00
} }
/>} label="да" / >
< / B o x >
)
}
2023-12-03 02:16:53 +00:00
const RatingInputsType = ( { parentQuestion , targetQuestion , ruleIndex , setParentQuestion } : Props ) = > {
2023-11-29 15:45:15 +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 >
< IconButton
sx = { { borderRadius : "6px" , padding : "2px" } }
onClick = { ( ) = > {
2023-12-03 02:16:53 +00:00
let newParentQuestion = JSON . parse ( JSON . stringify ( parentQuestion ) )
2023-11-29 15:45:15 +00:00
newParentQuestion . content . rule . main . splice ( ruleIndex , 1 )
2023-12-03 02:16:53 +00:00
setParentQuestion ( newParentQuestion )
2023-11-29 15:45:15 +00:00
} }
>
< DeleteIcon color = { "#4D4D4D" } / >
< / IconButton >
< / Box >
< Box
sx = { {
display : "inline" ,
alignItems : "center" ,
pb : "10px" ,
} }
>
< Typography sx = { { color : "#7E2AEA" , pl : "10px" , fontSize : "12px" } } >
( О ж и д а е м о е к о л и ч е с т в о я ч е е к )
< / Typography >
< / Box >
{ / * < T e x t F i e l d
sx = { {
marginTop : "20px" ,
width : "100%"
} }
value = { parentQuestion . content . rule . main [ ruleIndex ] . rules [ 0 ] . answers [ 0 ] }
onChange = { ( event : React.FormEvent < HTMLInputElement > ) = > {
2023-12-03 02:16:53 +00:00
let newParentQuestion = JSON . parse ( JSON . stringify ( parentQuestion ) )
newParentQuestion . content . rule . main [ ruleIndex ] . rules [ 0 ] . answers = [ ( event . target as HTMLInputElement ) . value . replace ( /[^0-9,\s]/g , "" ) ]
setParentQuestion ( newParentQuestion )
2023-11-29 15:45:15 +00:00
} }
/> */ }
< / B o x >
)
}
// {
// content: {
// rule: {
// main: [
// {
// next: "id of next question", // 2 string
// or: true // 3 boolean
// rules: [
// {
// question: "question id", string
// answers: [] // ответы на вопросы. для вариантов выбора - конкретные айдишники, для полей ввода текста - текст по полному совпадению, для ввода файла ии ещё какой подобной дичи - просто факт того что файл ввели, т.е . boolean
// }
// ]
// }
// ],
// default: ID string
// }
// }
// }