Reac front end for psicometric app

Question.jsx 4.0KB

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