Browse Source

save responses and validate nulls

amenpunk 2 years ago
parent
commit
92aa3c8c0b
2 changed files with 28 additions and 7 deletions
  1. 11 7
      src/Components/Test/Cleaver/Question.jsx
  2. 17 0
      src/Pages/Pruebas/Cleaver.jsx

+ 11 - 7
src/Components/Test/Cleaver/Question.jsx

@@ -30,8 +30,8 @@ function CheckboxesGroup(props) {
30 30
 
31 31
   let { quiz, save, responses : resp, id:index} = props;
32 32
 
33
-  const [checkA, setCheckA] = React.useState( 0);
34
-  const [checkB, setCheckB] = React.useState(0);
33
+  const [checkA, setCheckA] = React.useState(resp[index]? resp[index].A : 0);
34
+  const [checkB, setCheckB] = React.useState(resp[index]? resp[index].B : 0);
35 35
 
36 36
   const changeA = (event) => {
37 37
     let { id, checked } = event.target
@@ -40,10 +40,12 @@ function CheckboxesGroup(props) {
40 40
       let temp = {
41 41
         [index]: {
42 42
           A:id,
43
-          B: resp[id]?.B ? resp[id].B : 0
43
+          B: resp[index] ? resp[index].B : 0
44 44
         }
45 45
       }
46
-      save(Object.assign(resp,temp))
46
+      let final = Object.assign(resp,temp);
47
+      console.log('Change A:',final)
48
+      save(final)
47 49
     }
48 50
   };
49 51
   
@@ -54,10 +56,12 @@ function CheckboxesGroup(props) {
54 56
       let temp = {
55 57
         [index]: {
56 58
           B:id,
57
-          A: resp[id]?.A ? resp[id].A : 0
59
+          A: resp[index] ? resp[index].A : 0
58 60
         }
59 61
       }
60
-      save(Object.assign(resp,temp))
62
+      let final = Object.assign(resp,temp)
63
+      console.log('Change B: ', final);
64
+      save(final)
61 65
     }
62 66
   };
63 67
 
@@ -124,7 +128,7 @@ export function Question({quiz, index, current, save, responses}) {
124 128
         <CardContent>
125 129
           <div variant="body2">
126 130
             <List>
127
-              <CheckboxexHeader group={index + 1} title={instrucciondepregunta}/>
131
+              <CheckboxexHeader group={id} title={instrucciondepregunta}/>
128 132
             </List>
129 133
             <CheckboxesGroup 
130 134
               id={id}

+ 17 - 0
src/Pages/Pruebas/Cleaver.jsx

@@ -3,6 +3,7 @@ import { Service } from '../../Utils/HTTP'
3 3
 import useAuth from '../../Auth/useAuth.js';
4 4
 import { Question } from '../../Components/Test/Cleaver/Question.jsx'
5 5
 import { Box,Button } from '@mui/material'
6
+import toast, { Toaster } from 'react-hot-toast';
6 7
 
7 8
 export function Cleaver() {
8 9
 
@@ -14,6 +15,9 @@ export function Cleaver() {
14 15
   const [current, setCurrent] = React.useState(0);
15 16
   const [responses, setRespones] = React.useState({});
16 17
 
18
+  const BadQuestion = () => toast("Escoge una respuesta en cada columna",{
19
+      icon : '⚠️'
20
+  });
17 21
 
18 22
   React.useEffect(() => {
19 23
     // TODO:
@@ -29,7 +33,17 @@ export function Cleaver() {
29 33
   }, [token]);
30 34
 
31 35
   const handleAddQuestion = () => {
36
+
32 37
     let currentAnswer  = totalRespondidas[totalRespondidas.length - 1];
38
+    let current_resp = responses[currentAnswer.id];
39
+
40
+    if(!current_resp){
41
+      return BadQuestion();
42
+    }
43
+    console.log(current_resp)
44
+    if(parseInt( current_resp.A ) ===0 || parseInt( current_resp.B ) === 0){
45
+      return BadQuestion();
46
+    }
33 47
     const nextHiddenItem = totalPreguntas.filter(({id}) => id !== currentAnswer.id );
34 48
     if (nextHiddenItem) {
35 49
       setPreguntas(nextHiddenItem);
@@ -85,6 +99,9 @@ export function Cleaver() {
85 99
           Siguiente
86 100
         </Button>
87 101
       </div>
102
+      <Toaster
103
+        position="top-center"
104
+        />
88 105
     </div>
89 106
   )
90 107