Reac front end for psicometric app

Cleaver.jsx 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import React, { useMemo } from 'react'
  2. import { Service } from '../../Utils/HTTP'
  3. import useAuth from '../../Auth/useAuth.js';
  4. import { Question } from '../../Components/Test/Cleaver/Question.jsx'
  5. import { Box,Button } from '@mui/material'
  6. export function Cleaver() {
  7. let auth = useAuth();
  8. let token = useMemo(() => auth.getToken(), [auth])
  9. const [totalRespondidas, setRespondidas] = React.useState([]);
  10. const [totalPreguntas, setPreguntas] = React.useState([]);
  11. const [current, setCurrent] = React.useState(0);
  12. const [response, setRespones] = React.useState([]);
  13. React.useEffect(() => {
  14. let rest = new Service(`/prueba/findid/1`)
  15. rest.get(token.toString())
  16. .then(({ data }) => {
  17. console.log(data.questions)
  18. setPreguntas(data.questions)
  19. setRespondidas(data.questions.slice(0,1))
  20. setCurrent(0)
  21. }).catch(console.log)
  22. }, [token]);
  23. const handleAddQuestion = () => {
  24. let currentAnswer = totalRespondidas[totalRespondidas.length - 1];
  25. const nextHiddenItem = totalPreguntas.filter(({id}) => id !== currentAnswer.id );
  26. if (nextHiddenItem) {
  27. setPreguntas(nextHiddenItem);
  28. setCurrent(current+1);
  29. let temp = nextHiddenItem.shift()
  30. setRespondidas([...totalRespondidas,temp]);
  31. }
  32. };
  33. const handleRemoveQuestion = () => {
  34. let ultimaRespondida = totalRespondidas[totalRespondidas.length - 1];
  35. console.log(ultimaRespondida)
  36. setPreguntas([ultimaRespondida,...totalPreguntas]);
  37. let current_without_last = totalRespondidas.filter(({id}) => id !== ultimaRespondida.id);
  38. setRespondidas([...current_without_last]);
  39. setCurrent(current - 1 )
  40. }
  41. console.log('total respondidas: ', totalRespondidas)
  42. console.log('total respondidas total: ', totalRespondidas.length)
  43. return (
  44. <div className="content-section question">
  45. <Box className="question_body">
  46. {totalRespondidas.map((item,i) => (
  47. <Question key={item.id} id={item.id} quiz={item} index={i} current={current} /> )
  48. )}
  49. </Box>
  50. <div className="question_btn">
  51. <Button
  52. className="nextquestion_btn"
  53. sx={{ backgroundColor: 'var(--main)' }}
  54. variant="contained"
  55. disabled={totalRespondidas.length <= 1}
  56. onClick={handleRemoveQuestion}
  57. >
  58. Anterior
  59. </Button>
  60. <Button
  61. className="nextquestion_btn"
  62. sx={{ backgroundColor: 'var(--main)' }}
  63. variant="contained"
  64. disabled={totalPreguntas.length <= 0}
  65. onClick={handleAddQuestion}
  66. >
  67. Siguiente
  68. </Button>
  69. </div>
  70. </div>
  71. )
  72. }