From 6e93cb5f143f208ecc740f870690281f8d813146 Mon Sep 17 00:00:00 2001 From: jp-lima Date: Wed, 17 Sep 2025 17:09:32 -0300 Subject: [PATCH] atualizar paciente --- src/components/patients/PatientForm.jsx | 172 +++++++++--------------- src/pages/EditPage.jsx | 28 +++- src/pages/FormLayout.jsx | 6 +- 3 files changed, 90 insertions(+), 116 deletions(-) diff --git a/src/components/patients/PatientForm.jsx b/src/components/patients/PatientForm.jsx index 34dcb9a..7c7fa70 100644 --- a/src/components/patients/PatientForm.jsx +++ b/src/components/patients/PatientForm.jsx @@ -1,9 +1,7 @@ import React, { useState, useEffect } from 'react'; -function PatientForm({ onSave, onCancel, PatientDict }) { - - PatientDict = PatientDict || {} +function PatientForm({ onSave, onCancel,formData, setFormData }) { const FormatTelefones = (valor) => { const digits = String(valor).replace(/\D/g, '').slice(0, 11); @@ -66,61 +64,6 @@ function PatientForm({ onSave, onCancel, PatientDict }) { .replace(/(\d{3})(\d{1,2})$/, '$1-$2'); } - - const [formData, setFormData] = useState({ - foto: null, - nome:PatientDict.nome, - nomeSocial: PatientDict.nome_social, - dataNascimento: PatientDict.data_nascimento, - genero: PatientDict.sexo, - cpf: PatientDict.cpf, - profissao: PatientDict.profissao , - nomeMae: PatientDict.nome_mae, - profissaoMae: PatientDict.profissao_mae, - nomePai: PatientDict.nome_pai, - nomeResponsavel: '', - cpfResponsavel: '', - nomeConjuge: '', - outroId: '', - cep:PatientDict.endereco?.cep, - cidade: PatientDict.endereco?.cidade, - estado: PatientDict.endereco?.estado, - bairro: PatientDict.endereco?.bairro, - rua: PatientDict.endereco?.logradouro, - numero: PatientDict.endereco?.numero, - complemento: PatientDict.endereco?.complemento, - email: PatientDict.contato?.email, - telefone1: PatientDict.contato?.telefone1, - telefone2: PatientDict.contato?.telefone2, - telefone3: PatientDict.contato?.telefone3, - observacoes: PatientDict.observacoes, - rg: PatientDict.rg, - documentoTipo: PatientDict.outros_documentos?.tipo, - numeroDocumento: '', - etniaRaca: '', - naturalidade: '', - nacionalidade: '', - estadoCivil: '', - rnConvenio: false, - - // INFORMAÇÕES MÉDICAS - tipoSanguineo: '', - peso: '', - altura: '', - imc: '', - alergias: '', - - // INFORMAÇÕES DE CONVÊNIO - convenio: '', - plano: '', - numeroMatricula: '', - validadeCarteira: '', - validadeIndeterminada: false, - pacienteVip: false, - - // ANEXO - anexos: null, - }); // Estado para armazenar a URL da foto do avatar const [avatarUrl, setAvatarUrl] = useState(null); @@ -155,9 +98,19 @@ function PatientForm({ onSave, onCancel, PatientDict }) { }, [formData.peso, formData.altura]); + const [enderecoData, setEnderecoData] = useState({}) + useEffect(() => {setEnderecoData(formData.endereco || {}); console.log(enderecoData)}, [formData.endereco]) + + const [contato, setContato] = useState({}) + + useEffect(() => {setContato(formData.contato || {})}, [formData.contato]) + + const handleChange = (e) => { const { name, value, type, checked, files } = e.target; - + + console.log(enderecoData) + if (type === 'checkbox') { setFormData({ ...formData, [name]: checked }); } else if (type === 'file') { @@ -172,19 +125,24 @@ function PatientForm({ onSave, onCancel, PatientDict }) { reader.readAsDataURL(files[0]); } else if (name === 'foto' && !files[0]) { setAvatarUrl(null); // Limpa o avatar se nenhum arquivo for selecionado - } - - } else { - setFormData({ ...formData, [name]: value }); - } + }} + if (name.includes('cpf')) { let cpfFormatado = FormatCPF(value); setFormData(prev => ({ ...prev, [name]: cpfFormatado })); + } else if (name.includes('telefone')) { let telefoneFormatado = FormatTelefones(value); - setFormData(prev => ({ ...prev, [name]: telefoneFormatado })); + setContato(prev => ({ ...prev, [name]: telefoneFormatado })); } + + if (name === 'email') { + setContato(prev => ({ ...prev, email: value })); + }else if(name.includes('endereco')) { + setEnderecoData(prev => ({ ...prev, [name.split('.')[1]]: value })); + }else{ + setFormData({ ...formData, [name]: value });} }; const handleCepBlur = async () => { @@ -211,35 +169,31 @@ function PatientForm({ onSave, onCancel, PatientDict }) { }; const handleSubmit = async () => { - if (!formData.nome || !formData.cpf || !formData.genero || !formData.dataNascimento){ - alert('Por favor, preencha Nome ,CPF, Gênero e data de nascimento.'); - + if (!formData.nome || !formData.cpf || !formData.sexo || !formData.data_nascimento){ + alert('Por favor, preencha Nome ,CPF, Gênero e data de nascimento.'); } - const CPFinvalido = await ValidarCPF(formData.cpf) console.log(CPFinvalido) if(CPFinvalido[0] === true){ alert(CPFinvalido[1]) - return - - } + return} onSave({ ...formData, endereco: { - cep: formData.cep, - cidade: formData.cidade, - estado: formData.estado, - bairro: formData.bairro, - logradouro: formData.rua, - numero: formData.numero, - complemento: formData.complemento, + cep: enderecoData.cep, + cidade: enderecoData.cidade, + estado: enderecoData.estado, + bairro: enderecoData.bairro, + logradouro: enderecoData.logradouro, + numero: enderecoData.numero, + complemento: enderecoData.complemento, }, contato: { - email: formData.email, - telefone1: formData.telefone1, - telefone2: formData.telefone2, - telefone3: formData.telefone3, + email: contato.email, + telefone1: contato.telefone1, + telefone2: contato.telefone2, + telefone3: contato.telefone3, }, infoMedicas: { tipoSanguineo: formData.tipoSanguineo, @@ -316,19 +270,19 @@ function PatientForm({ onSave, onCancel, PatientDict }) { {/* CADASTRO */}
- +
- +
- +
- @@ -354,7 +308,7 @@ function PatientForm({ onSave, onCancel, PatientDict }) {
- +
@@ -381,7 +335,7 @@ function PatientForm({ onSave, onCancel, PatientDict }) {
- @@ -391,31 +345,31 @@ function PatientForm({ onSave, onCancel, PatientDict }) {
- +
- +
- +
- +
- +
- +
- +
@@ -557,31 +511,31 @@ function PatientForm({ onSave, onCancel, PatientDict }) {
- +
- +
- +
- +
- +
- +
- +
@@ -598,20 +552,20 @@ function PatientForm({ onSave, onCancel, PatientDict }) {
- - + +
- - + +
- +
- +
diff --git a/src/pages/EditPage.jsx b/src/pages/EditPage.jsx index 456aa3f..eb29a8a 100644 --- a/src/pages/EditPage.jsx +++ b/src/pages/EditPage.jsx @@ -23,9 +23,28 @@ fetch(`https://mock.apidog.com/m1/1053378-0-default/pacientes/${id}`, requestOpt .catch(error => console.log('error', error)); }, []) - const HandlePutPatient = () => { - console.log('paciente atualizado') +const HandlePutPatient = () => { + alert(`Atualizando paciente "${PatientToPUT.nome}" com sucesso`) + var myHeaders = new Headers(); +myHeaders.append("Authorization", "Bearer "); +myHeaders.append("Content-Type", "application/json"); + +var raw = JSON.stringify(PatientToPUT) + +console.log(PatientToPUT) + +var requestOptions = { + method: 'PUT', + headers: myHeaders, + body: raw, + redirect: 'follow' +}; + +fetch("https://mock.apidog.com/m1/1053378-0-default/pacientes/", requestOptions) + .then(response => response.text()) + .then(result => console.log('ATUALIZADO COM SUCESSO',result)) + .catch(error => console.log('error', error)); } @@ -41,8 +60,9 @@ fetch(`https://mock.apidog.com/m1/1053378-0-default/pacientes/${id}`, requestOpt diff --git a/src/pages/FormLayout.jsx b/src/pages/FormLayout.jsx index 14e05a7..1e63ef4 100644 --- a/src/pages/FormLayout.jsx +++ b/src/pages/FormLayout.jsx @@ -8,7 +8,7 @@ function FormLayout( ) { // Este estado vai controlar qual "tela" mostrar: 'list' (lista) ou 'form' (formulário) const [view, setView] = useState('form'); - + const [formData, setFormData] = useState({}) var myHeaders = new Headers(); myHeaders.append("Content-Type", "application/json"); @@ -55,8 +55,8 @@ function FormLayout( ) { setView('list')} - PatientDict={{}} - + formData={formData} + setFormData={setFormData} /> )}