Reac front end for psicometric app

Question.jsx 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import * as React from 'react';
  2. import { Remove as RemoveIcon, Add as AddIcon } from '@mui/icons-material';
  3. import {
  4. Card,CardContent,Avatar,Checkbox,List,Tooltip,Fade,
  5. ListItem,ListItemButton,ListItemIcon,ListItemText,
  6. Box,LinearProgress,
  7. } from '@mui/material'
  8. import { deepPurple } from '@mui/material/colors';
  9. import { useDispatch, useSelector } from 'react-redux';
  10. import { setResponse } from '../../../Slices/cleaverSlice';
  11. function LinearProgressWithLabel(props) {
  12. return (
  13. <Box sx={{ display: 'flex', alignItems: 'center' }}>
  14. <Box sx={{ width: '100%', mr: 0 }}>
  15. <LinearProgress variant="determinate" {...props} />
  16. </Box>
  17. </Box>
  18. );
  19. }
  20. function CheckboxexHeader(prop) {
  21. return (
  22. <List sx={{ width: '100%', bgcolor: 'background.paper' }}>
  23. <ListItem>
  24. <Avatar sx={{ bgcolor: deepPurple[500] }}>{prop.group}</Avatar>
  25. <ListItemButton role={undefined} dense>
  26. <ListItemText style={{marginRight :25, color:'silver'}} primary={prop.title} />
  27. <ListItemIcon>
  28. <AddIcon />
  29. </ListItemIcon>
  30. <ListItemIcon>
  31. <RemoveIcon />
  32. </ListItemIcon>
  33. </ListItemButton>
  34. </ListItem>
  35. </List>
  36. );
  37. }
  38. function CheckboxesGroup(props) {
  39. let { quiz, id : index } = props;
  40. let resp = useSelector((state) => state.cleaver.responses)
  41. const [checkA, setCheckA] = React.useState(resp[index]? resp[index].A : 0);
  42. const [checkB, setCheckB] = React.useState(resp[index]? resp[index].B : 0);
  43. const dispatch = useDispatch()
  44. const changeA = (event) => {
  45. let { id, checked } = event.target
  46. if(parseInt( checkB ) !== parseInt(id) && checked ){
  47. setCheckA(parseInt( id ))
  48. let temp = {
  49. [index]: {
  50. A:id,
  51. B: resp[index] ? resp[index].B : 0
  52. }
  53. }
  54. dispatch(setResponse(temp))
  55. }
  56. };
  57. const changeB = (event) => {
  58. let { id, checked } = event.target
  59. if(parseInt( checkA ) !== parseInt( id ) && checked){
  60. setCheckB(parseInt( id ))
  61. let temp = {
  62. [index]: {
  63. B:id,
  64. A: resp[index] ? resp[index].A : 0
  65. }
  66. }
  67. dispatch(setResponse(temp))
  68. }
  69. };
  70. return (
  71. <List sx={{ width: '100%', bgcolor: 'background.paper' }}>
  72. {quiz.map((value) => {
  73. const labelId = `checkbox-list-label-${value.id}`;
  74. return (
  75. <ListItem key={value.id}>
  76. <ListItemButton dense>
  77. <Tooltip title={value.decription} placement="top-start" arrow>
  78. <ListItemText
  79. sx={{
  80. '& .MuiTypography-root' : {
  81. fontWeight:'bold'
  82. }}}
  83. className="checkbox_label_question"
  84. id={labelId}
  85. primary={value.nombre}
  86. />
  87. </Tooltip>
  88. <ListItemIcon>
  89. <Checkbox
  90. id={value.id.toString()}
  91. sx={{ '& .MuiSvgIcon-root': { fontSize: 28 } }}
  92. color="error"
  93. edge="start"
  94. disableRipple
  95. inputProps={{ 'aria-labelledby': value.id }}
  96. checked={parseInt( checkA ) === parseInt( value.id )}
  97. onChange={changeA}
  98. />
  99. </ListItemIcon>
  100. <ListItemIcon>
  101. <Checkbox
  102. id={value.id.toString()}
  103. sx={{ '& .MuiSvgIcon-root': { fontSize: 28 } }}
  104. color="error"
  105. edge="start"
  106. disableRipple
  107. inputProps={{ 'aria-labelledby': value.id }}
  108. checked={parseInt( checkB ) === parseInt( value.id )}
  109. onChange={changeB}
  110. />
  111. </ListItemIcon>
  112. </ListItemButton>
  113. </ListItem>
  114. );
  115. })}
  116. </List>
  117. );
  118. }
  119. export function Question({quiz, index, current, progress}) {
  120. let { instrucciondepregunta, respuestas,id } = quiz
  121. let checked = index === current;
  122. return (
  123. <Fade in={checked} unmountOnExit>
  124. <Card className="question_form">
  125. <CardContent>
  126. <div variant="body2">
  127. <List>
  128. <CheckboxexHeader group={index + 1} title={instrucciondepregunta}/>
  129. </List>
  130. <CheckboxesGroup
  131. id={id}
  132. quiz={respuestas}
  133. />
  134. </div>
  135. </CardContent>
  136. <LinearProgressWithLabel value={progress ? parseInt(progress) : 0 } />
  137. </Card>
  138. </Fade>
  139. );
  140. }