diff --git a/src/components/patients/PatientForm.jsx b/src/components/patients/PatientForm.jsx index 9844b80..0c53e46 100644 --- a/src/components/patients/PatientForm.jsx +++ b/src/components/patients/PatientForm.jsx @@ -1,8 +1,9 @@ import React, { useState, useEffect } from 'react'; -import InputMask from "react-input-mask"; + function PatientForm({ onSave, onCancel, PatientDict }) { + const FormatTelefones = (valor) => { const digits = String(valor).replace(/\D/g, '').slice(0, 11); return digits @@ -12,6 +13,56 @@ function PatientForm({ onSave, onCancel, PatientDict }) { .replace(/(\d{4})(\d{4})/, '$1-$2') } + const ReceberRespostaAPIdoCPF = async (cpf) =>{ + var myHeaders = new Headers(); + myHeaders.append("Authorization", "Bearer "); + myHeaders.append("Content-Type", "application/json"); + + var raw = JSON.stringify({ + "cpf": cpf + }); + + var requestOptions = { + method: 'POST', + headers: myHeaders, + body: raw, + redirect: 'follow' + }; + + const response = await fetch("https://mock.apidog.com/m1/1053378-0-default/pacientes/validar-cpf", requestOptions) + const result = await response.json() + + return result.data + + + } + + const ValidarCPF = async (cpf) => { + let aviso + let Erro = false + + const resutadoAPI = await ReceberRespostaAPIdoCPF(cpf) + + const valido = resutadoAPI.valido + const ExisteNoBancoDeDados = resutadoAPI.existe + + if(valido === false){ + aviso = 'CPF inválido' + Erro = true + } + else if(ExisteNoBancoDeDados === true){ + aviso = 'O CPF informado já está presente no sistema' + Erro = true + + + } + + return [Erro,aviso] + + + + } + const FormatCPF = (valor) => { const digits = String(valor).replace(/\D/g, '').slice(0, 11); return digits @@ -163,10 +214,18 @@ function PatientForm({ onSave, onCancel, PatientDict }) { } }; - const handleSubmit = () => { + const handleSubmit = async () => { if (!formData.nome || !formData.cpf || !formData.genero || !formData.dataNascimento){ alert('Por favor, preencha Nome ,CPF, Gênero e data de nascimento.'); - return; + + } + + const CPFinvalido = await ValidarCPF(formData.cpf) + console.log(CPFinvalido) + if(CPFinvalido[0] === true){ + alert(CPFinvalido[1]) + return + } onSave({