188 lines
6.5 KiB
JavaScript

import React, { useState, useEffect } from 'react';
import PatientForm from '../components/patients/PatientForm';
import PatientList from '../components/patients/PatientList';
const requestOptionsDelete = { method: "DELETE", redirect: "follow" };
const deletePatient = async (id) => {
// Função para excluir paciente
const [search, setSearch] = useState("");
const [pacientes, setPacientes] = useState([]);
function TablePaciente({ setCurrentPage, setPatientID }) {
if (!window.confirm("Tem certeza que deseja excluir este paciente?")) return;
await fetch(
`https://mock.apidog.com/m1/1053378-0-default/pacientes/${id}`,
requestOptionsDelete
)
.then((response) => response.text())
.then((mensage) => console.log(mensage))
.catch((error) => console.log("Deu problema", error));
};
const onChange = (e, id) => {
let value = e.target.value;
<<<<<<< HEAD
if (value === "verdetalhes") {
setCurrentPage("details-page-paciente");
}
if (value === "editar") {
setCurrentPage("edit-page-paciente");
setPatientID(id);
=======
if(value === 'verdetalhes'){
setPatientID(id);
setCurrentPage('details-page-paciente')
}
if(value === 'editar'){
setPatientID(id);
setCurrentPage('edit-page-paciente')
}
if(value === 'excluir'){
console.log(`Excluir ${id}`)
deletePatient(id)
>>>>>>> origin/detalhes-do-pacientes
}
if (value === "excluir") {
console.log(`Excluir ${id}`);
deletePatient(id);
}
};
var requestOptions = {
method: "GET",
redirect: "follow",
};
useEffect(() => {
fetch(
"https://mock.apidog.com/m1/1053378-0-default/pacientes",
requestOptions
)
.then((response) => response.json())
.then((result) => setPacientes(result["data"]))
.catch((error) =>
console.log("Erro para encontrar pacientes no banco de dados", error)
);
}, []);
// Filtrar pacientes pelo campo de pesquisa (nome, cpf, email, telefone)
const pacientesFiltrados = pacientes.filter((paciente) =>
`${paciente.nome} ${paciente.cpf} ${paciente.email} ${paciente.telefone}`
.toLowerCase()
.includes(search.toLowerCase())
);
return (
<>
<div className="page-heading">
<h3>Lista de Pacientes</h3>
</div>
<div className="page-content">
<section className="row">
<div className="col-12">
<div className="card">
{/* Header com título e botão alinhados */}
<div className="card-header d-flex justify-content-between align-items-center">
<h4 className="card-title mb-0">Pacientes Cadastrados</h4>
<button
className="btn btn-primary"
onClick={() => setCurrentPage("form-layout")}
>
<i className="bi bi-plus-circle"></i> Adicionar Paciente
</button>
</div>
<div className="card-body">
{/* Barra de pesquisa abaixo do título */}
<div className="mb-3">
<input
type="text"
placeholder="Pesquisar paciente..."
value={search}
onChange={(e) => setSearch(e.target.value)}
className="form-control"
/>
</div>
<div className="table-responsive">
<table className="table table-striped table-hover">
<thead>
<tr>
<th>Nome</th>
<th>CPF</th>
<th>Email</th>
<th>Telefone</th>
<th>Opções</th>
</tr>
</thead>
<tbody>
{pacientesFiltrados.length > 0 ? (
pacientesFiltrados.map((paciente) => (
<tr key={paciente.id}>
<td>{paciente.nome}</td>
<td>{paciente.cpf}</td>
<td>{paciente.email}</td>
<td>{paciente.telefone}</td>
<td>
<span
className={`badge ${
paciente.ativo === "ativo"
? "bg-success"
: "bg-danger"
}`}
>
{paciente.ativo}
</span>
</td>
<<<<<<<
<td>
<select onChange={(e) => onChange(e, paciente.id)}>
<option value=""> </option>
<option value="verdetalhes">Ver detalhes</option>
<option value="editar">Editar</option>
<option value="excluir">Excluir</option>
</select>
</td>
=======
<td>
<Button variant="Ghost" >Ghost</Button>
<svg xmlns="http://www.w3.org/2000/svg" width="23" height="36" viewBox="0 0 24 24" fill="none" stroke="#c01c28" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-trash2-icon lucide-trash-2"><path d="M10 11v6"/><path d="M14 11v6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/>
<path d="M3 6h18"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/>
</svg>
</td>
>>>>>>>
</tr>
))
) : (
<tr>
<td colSpan="6" className="text-center">
Nenhum paciente encontrado.
</td>
</tr>
)}
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
</div>
</>
);
}
export default TablePaciente;