Validação de CPF
This commit is contained in:
parent
8e1fcd9609
commit
705ef778e9
@ -1,8 +1,9 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import InputMask from "react-input-mask";
|
|
||||||
|
|
||||||
function PatientForm({ onSave, onCancel, PatientDict }) {
|
function PatientForm({ onSave, onCancel, PatientDict }) {
|
||||||
|
|
||||||
|
|
||||||
const FormatTelefones = (valor) => {
|
const FormatTelefones = (valor) => {
|
||||||
const digits = String(valor).replace(/\D/g, '').slice(0, 11);
|
const digits = String(valor).replace(/\D/g, '').slice(0, 11);
|
||||||
return digits
|
return digits
|
||||||
@ -12,6 +13,56 @@ function PatientForm({ onSave, onCancel, PatientDict }) {
|
|||||||
.replace(/(\d{4})(\d{4})/, '$1-$2')
|
.replace(/(\d{4})(\d{4})/, '$1-$2')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ReceberRespostaAPIdoCPF = async (cpf) =>{
|
||||||
|
var myHeaders = new Headers();
|
||||||
|
myHeaders.append("Authorization", "Bearer <token>");
|
||||||
|
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 FormatCPF = (valor) => {
|
||||||
const digits = String(valor).replace(/\D/g, '').slice(0, 11);
|
const digits = String(valor).replace(/\D/g, '').slice(0, 11);
|
||||||
return digits
|
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){
|
if (!formData.nome || !formData.cpf || !formData.genero || !formData.dataNascimento){
|
||||||
alert('Por favor, preencha Nome ,CPF, Gênero e data de nascimento.');
|
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({
|
onSave({
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user