diff --git a/src/components/AgendarConsulta/FormNovaConsulta.jsx b/src/components/AgendarConsulta/FormNovaConsulta.jsx index 6723e6c..0ab474d 100644 --- a/src/components/AgendarConsulta/FormNovaConsulta.jsx +++ b/src/components/AgendarConsulta/FormNovaConsulta.jsx @@ -1,8 +1,53 @@ -import React from "react"; +import React, { useState, useEffect } from "react"; import InputMask from "react-input-mask"; import "./style/styleagendamentos.css"; -const FormNovaConsulta = ({ onCancel }) => { +const FormNovaConsulta = ({ onCancel, patientID }) => { + const [selectedFile, setSelectedFile] = useState(null); + const [anexos, setAnexos] = useState([]); + const [loadingAnexos, setLoadingAnexos] = useState(false); + useEffect(() => { + if (!patientID) return; + + const fetchAnexos = async () => { + setLoadingAnexos(true); + try { + const res = await fetch(`https://mock.apidog.com/m1/1053378-0-default/pacientes/${patientID}/anexos`); + const data = await res.json(); + setAnexos(data.data || []); + } catch (err) { + console.error("Erro ao buscar anexos:", err); + } finally { + setLoadingAnexos(false); + } + }; + + fetchAnexos(); + }, [patientID]); + + const handleUpload = async () => { + if (!selectedFile) return; + + const formData = new FormData(); + formData.append("file", selectedFile); + + try { + const res = await fetch(`https://mock.apidog.com/m1/1053378-0-default/pacientes/${patientID}/anexos`, { + method: "POST", + body: formData + }); + if (res.ok) { + const novoAnexo = await res.json(); + setAnexos(prev => [...prev, novoAnexo]); + setSelectedFile(null); + } else { + console.error("Erro ao enviar anexo"); + } + } catch (err) { + console.error("Erro ao enviar anexo:", err); + } + }; + const handleSubmit = (e) => { e.preventDefault(); alert("Agendamento salvo!"); @@ -55,7 +100,29 @@ const FormNovaConsulta = ({ onCancel }) => {

Informações adicionais

- + + setSelectedFile(e.target.files[0])} + /> + {selectedFile && ( + + )} +
+ {loadingAnexos ? ( +

Carregando anexos...

+ ) : ( + anexos.map((anexo, index) => ( +
+ {anexo.nome || anexo.fileName} +
+ )) + )} +

Informações do atendimento

diff --git a/src/components/patients/PatientForm.jsx b/src/components/patients/PatientForm.jsx index 673a405..43d8f3e 100644 --- a/src/components/patients/PatientForm.jsx +++ b/src/components/patients/PatientForm.jsx @@ -167,7 +167,8 @@ function PatientForm({ onSave, onCancel,formData, setFormData }) { } }; - const handleSubmit = async () => { + const handleSubmit = async (e) => { + e.preventDefault(); if (!formData.nome || !formData.cpf || !formData.sexo || !formData.data_nascimento){ alert('Por favor, preencha Nome ,CPF, Gênero e data de nascimento.'); return; @@ -181,7 +182,7 @@ function PatientForm({ onSave, onCancel,formData, setFormData }) { } - onSave({ + const pacienteSalvo = await onSave({ ...formData, endereco: { cep: enderecoData.cep, @@ -214,8 +215,52 @@ function PatientForm({ onSave, onCancel,formData, setFormData }) { pacienteVip: formData.pacienteVip, }, }); - }; + const pacienteId = pacienteSalvo.id; + + try{ + if (formData.foto) await uploadFotoPaciente(pacienteId, formData.foto); + if (formData.anexos) await uploadAnexoPaciente(pacienteId, formData.anexos); + alert("Paciente salvo com sucesso!"); + } catch (error) { + console.error(error); + alert("Erro ao salvar paciente ou enviar arquivos."); + } + }; + const uploadFotoPaciente = async (pacienteId, foto) => { + const formDataUpload = new FormData(); + formDataUpload.append('foto', foto); + + try { + const res = await fetch(`https://suaapi.com/pacientes/${pacienteId}/foto`, { + method: 'POST', + headers: { 'Authorization': 'Bearer ' }, + body: formDataUpload + }); + if (!res.ok) throw new Error('Erro ao enviar foto'); + alert('Foto enviada com sucesso!'); + } catch (err) { + console.error(err); + alert('Falha ao enviar foto'); + } + }; + const uploadAnexoPaciente = async (pacienteId, anexo) => { + const formDataUpload = new FormData(); + formDataUpload.append('anexo', anexo); + try { + const res = await fetch(`https://suaapi.com/pacientes/${pacienteId}/anexos`, { + method: 'POST', + headers: { 'Authorization': 'Bearer ' }, + body: formDataUpload + }); + if (!res.ok) throw new Error('Erro ao enviar anexo'); + alert('Anexo enviado com sucesso!'); + } catch (err) { + console.error(err); + alert('Falha ao enviar anexo'); + } + }; + return (

MediConnect

diff --git a/src/pages/Details.jsx b/src/pages/Details.jsx index 59cf032..1f6b317 100644 --- a/src/pages/Details.jsx +++ b/src/pages/Details.jsx @@ -3,17 +3,69 @@ import avatarPlaceholder from '../assets/images/avatar_placeholder.png'; const Details = ({ patientID, setCurrentPage }) => { const [paciente, setPaciente] = useState({}); + const [anexos, setAnexos] = useState([]); + const [selectedFile, setSelectedFile] = useState(null); useEffect(() => { if (!patientID) return; fetch(`https://mock.apidog.com/m1/1053378-0-default/pacientes/${patientID}`) .then(res => res.json()) - + .then(result => {setPaciente(result.data)}) .catch(err => console.error("Erro ao buscar paciente:", err)); + + fetch(`https://mock.apidog.com/m1/1053378-0-default/pacientes/${patientID}/anexos`) + .then(res => res.json()) + .then(data => setAnexos(data.data || [])) + .catch(err => console.error("Erro ao buscar anexos:", err)); + }, [patientID]); + const handleUpload = async () => { + if (!selectedFile) return; + + const formData = new FormData(); + formData.append('file', selectedFile); + + try { + const response = await fetch(`https://mock.apidog.com/m1/1053378-0-default/pacientes/${patientID}/anexos`, { + method: 'POST', + body: formData, + }); + + if (response.ok) { + const newAnexo = await response.json(); + setAnexos(prev => [...prev, newAnexo]); + setSelectedFile(null); + } else { + console.error('Erro ao enviar anexo'); + } + } catch (err) { + console.error('Erro ao enviar anexo:', err); + } + }; + + const handleDelete = async (anexoId) => { + try { + const response = await fetch( + `https://mock.apidog.com/m1/1053378-0-default/pacientes/${patientID}/anexos/${anexoId}`, + { + method: "DELETE", + } + ); + + if (response.ok) { + setAnexos((prev) => prev.filter((a) => a.id !== anexoId)); + } else { + console.error("Erro ao deletar anexo"); + } + } catch (err) { + console.error("Erro ao deletar anexo:", err); + } + }; + + return ( @@ -137,7 +189,29 @@ const Details = ({ patientID, setCurrentPage }) => {
-

{ "-"}

+ {anexos.length > 0 ?( +
    + {anexos.map((anexo) => ( +
  • + + {anexo.nome} + + +
  • + ))} +
+ ) : ( +

-

+ )} +
+
+ + setSelectedFile(e.target.files[0])} accept="image/*"/> + {selectedFile && {selectedFile.name}} +