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
 
30
 
31
   let { quiz, save, responses : resp, id:index} = props;
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
   const changeA = (event) => {
36
   const changeA = (event) => {
37
     let { id, checked } = event.target
37
     let { id, checked } = event.target
40
       let temp = {
40
       let temp = {
41
         [index]: {
41
         [index]: {
42
           A:id,
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
       let temp = {
56
       let temp = {
55
         [index]: {
57
         [index]: {
56
           B:id,
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
         <CardContent>
128
         <CardContent>
125
           <div variant="body2">
129
           <div variant="body2">
126
             <List>
130
             <List>
127
-              <CheckboxexHeader group={index + 1} title={instrucciondepregunta}/>
131
+              <CheckboxexHeader group={id} title={instrucciondepregunta}/>
128
             </List>
132
             </List>
129
             <CheckboxesGroup 
133
             <CheckboxesGroup 
130
               id={id}
134
               id={id}

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

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