浏览代码

consum register service

amenpunk 3 年之前
父节点
当前提交
e7fbcffe49
共有 3 个文件被更改,包括 53 次插入16 次删除
  1. 45 11
      src/Components/Register/PersonalInfo.js
  2. 5 5
      src/Pages/Home.jsx
  3. 3 0
      src/Utils/HTTP.js

+ 45 - 11
src/Components/Register/PersonalInfo.js

1
 import  { useState } from 'react'
1
 import  { useState } from 'react'
2
-import { useFormik, Form, FormikProvider } from 'formik'; // import { useNavigate } from 'react-router-dom';
3
-import { Stack, TextField,Box, Button,
2
+import { useFormik, Form, FormikProvider } from 'formik'; 
3
+import { useNavigate } from 'react-router-dom';
4
+import { 
5
+    Stack, TextField,Box, Button,
4
     Backdrop, CircularProgress,
6
     Backdrop, CircularProgress,
5
 } from '@mui/material';
7
 } from '@mui/material';
6
 import toast, { Toaster } from 'react-hot-toast';
8
 import toast, { Toaster } from 'react-hot-toast';
7
 import * as Yup from 'yup';
9
 import * as Yup from 'yup';
8
 
10
 
11
+import { Service } from '../../Utils/HTTP';
12
+// import useAuth from '../../Auth/useAuth';;
13
+
9
 export function PersonalInfo(props) {
14
 export function PersonalInfo(props) {
10
 
15
 
11
     let { handleBack, setClient, client } = props
16
     let { handleBack, setClient, client } = props
12
 
17
 
18
+    let navigate = useNavigate()
19
+
13
     const RegisterSchema = Yup.object().shape({
20
     const RegisterSchema = Yup.object().shape({
14
         nit: Yup.string().min(2, 'Demasiado corto!').max(50, 'Demasiado largo!').required('El nit es requerido'),
21
         nit: Yup.string().min(2, 'Demasiado corto!').max(50, 'Demasiado largo!').required('El nit es requerido'),
15
         cui: Yup.string().min(2, 'Demasiado corto!').max(50, 'Demasiado Largo!').required('Tu CUI/DPI es requerido'),
22
         cui: Yup.string().min(2, 'Demasiado corto!').max(50, 'Demasiado Largo!').required('Tu CUI/DPI es requerido'),
31
         validationSchema: RegisterSchema,
38
         validationSchema: RegisterSchema,
32
         onSubmit: (values) => {
39
         onSubmit: (values) => {
33
             setOpen(true);
40
             setOpen(true);
34
-            // let url = 'http://204.48.25.93:8081/registro'
35
-            // let url = 'http://psicoadmin.ditca.org:8081/registro'
36
-            console.log(client)
37
-            setClient({
38
-                ...client, ...values
39
-            })
40
 
41
 
41
-            setTimeout(() => {
42
+            let new_client = {
43
+                ...client, ...values
44
+            }
45
+            setClient(new_client)
46
+
47
+            let body = {
48
+                "nombre" : new_client.firstName,
49
+                "apelidos" : new_client.lastName,
50
+                "telefono" : new_client.telefono,
51
+                "direccio": new_client.direccion,
52
+                "fechacumple": new_client.nacimiento,
53
+                "nit": new_client.nit,
54
+                "cui": new_client.cui,
55
+                "email": new_client.email,
56
+                "username": new_client.email,
57
+                "pwd": new_client.password
58
+            }
59
+
60
+
61
+            let req =  new Service('/registro');
62
+
63
+            req.post(body)
64
+            .then( data => {
65
+
66
+                setTimeout(() => {
67
+                    setOpen(false);
68
+                    return navigate('/')
69
+                }, 2000)
70
+
71
+                console.log("DATA ::", data)
42
                 let nombre = client.firstName +  " " + client.lastName
72
                 let nombre = client.firstName +  " " + client.lastName
43
                 toast.success(`Bienvenido ${nombre} !!`)
73
                 toast.success(`Bienvenido ${nombre} !!`)
44
-                setOpen(false);
45
-            }, 5000)
46
 
74
 
47
 
75
 
76
+            })
77
+            .catch(error => {
78
+                console.log("ERROR ::", error)
79
+                toast.error(`Ups ocurrió un error puede que tu usuario yo exista o sea un error interno, intenta nuevamente más tarde o ponte en contacto con nosotros`)
80
+                return setOpen(false);
81
+            })
48
 
82
 
49
         }
83
         }
50
     });
84
     });

+ 5 - 5
src/Pages/Home.jsx

1
 import { Col, Row } from 'react-bootstrap'
1
 import { Col, Row } from 'react-bootstrap'
2
-import React, { useEffect } from 'react'
2
+import React, { } from 'react'
3
 
3
 
4
 import { PersonOutline, VerifiedUser, ListAlt  } from '@mui/icons-material/'
4
 import { PersonOutline, VerifiedUser, ListAlt  } from '@mui/icons-material/'
5
 
5
 
7
 import Candidatos from '../Components/Home/Candidatos'
7
 import Candidatos from '../Components/Home/Candidatos'
8
 import { Card } from '../Components/Card';
8
 import { Card } from '../Components/Card';
9
 
9
 
10
-import useAuth from '../Auth/useAuth';
11
-import { Service } from '../Utils/HTTP.js';
10
+// import useAuth from '../Auth/useAuth';
11
+// import { Service } from '../Utils/HTTP.js';
12
 
12
 
13
 export function Home() {
13
 export function Home() {
14
 
14
 
15
+    /*
15
     let auth = useAuth();
16
     let auth = useAuth();
16
-    
17
-
18
     useEffect(() => {
17
     useEffect(() => {
19
         let token = auth.getToken();
18
         let token = auth.getToken();
20
         let myService = new Service("/persona/view");
19
         let myService = new Service("/persona/view");
25
             console.log('Error Response :: ', e)
24
             console.log('Error Response :: ', e)
26
         })
25
         })
27
     } ,[])
26
     } ,[])
27
+*/
28
 
28
 
29
     return (
29
     return (
30
         <section >
30
         <section >

+ 3 - 0
src/Utils/HTTP.js

46
         if(!token){
46
         if(!token){
47
             let response = await fetch(this.url, {
47
             let response = await fetch(this.url, {
48
                 method: "POST",
48
                 method: "POST",
49
+                headers:{
50
+                    'Content-Type': 'application/json',
51
+                },
49
                 body : JSON.stringify(body)
52
                 body : JSON.stringify(body)
50
             })
53
             })
51
             return await response.json(); 
54
             return await response.json();