adicionado leitura ao CRUD com a nova API

This commit is contained in:
DaniloSts 2025-09-28 19:24:59 -03:00
parent 0d2116815d
commit 8130ce47ec
4 changed files with 53 additions and 59 deletions

View File

@ -8,6 +8,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@
import { Plus, Edit, Trash2, Eye, Calendar, Filter } from "lucide-react";
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog";
import SecretaryLayout from "@/components/secretary-layout";
import { patientsService } from "@/services/patientsApi.mjs"
export default function PacientesPage() {
const [searchTerm, setSearchTerm] = useState("");
@ -28,40 +29,43 @@ export default function PacientesPage() {
setDetailsDialogOpen(true);
setPatientDetails(null);
try {
const res = await fetch(`https://mock.apidog.com/m1/1053378-0-default/pacientes/${patientId}`);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const json = await res.json();
setPatientDetails(json?.data ?? null);
const res = await patientsService.getById(patientId);
console.log(res)
setPatientDetails(res[0]);
} catch (e: any) {
setPatientDetails({ error: e?.message || "Erro ao buscar detalhes" });
}
};
const fetchPacientes = useCallback(
async (pageToFetch: number) => {
if (isFetching || !hasNext) return;
setIsFetching(true);
setError(null);
try {
const res = await fetch(`https://mock.apidog.com/m1/1053378-0-default/pacientes?page=${pageToFetch}&limit=20`);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const json = await res.json();
const items = Array.isArray(json?.data) ? json.data : [];
const mapped = items.map((p: any) => ({
id: String(p.id ?? ""),
nome: p.nome ?? "",
telefone: p?.contato?.celular ?? p?.contato?.telefone1 ?? p?.telefone ?? "",
cidade: p?.endereco?.cidade ?? p?.cidade ?? "",
estado: p?.endereco?.estado ?? p?.estado ?? "",
ultimoAtendimento: p.ultimo_atendimento ?? p.ultimoAtendimento ?? undefined,
proximoAtendimento: p.proximo_atendimento ?? p.proximoAtendimento ?? undefined,
convenio: p.convenio ?? "",
vip: Boolean(p.vip ?? false),
status: p.status ?? undefined,
}));
setPatients((prev) => [...prev, ...mapped]);
setHasNext(Boolean(json?.pagination?.has_next));
setPage(pageToFetch + 1);
const res = await patientsService.list();
console.log(res)
const mapped = res.map((p: any) => ({
id: String(p.id ?? ""),
nome: p.full_name ?? "",
telefone: p.phone_mobile ?? p.phone1 ?? "",
cidade: p.city ?? "",
estado: p.state ?? "",
ultimoAtendimento: p.last_visit_at ?? "",
proximoAtendimento: p.next_appointment_at ?? "",
vip: Boolean(p.vip ?? false),
convenio: p.convenio ?? "", // se não existir, fica vazio
status: p.status ?? undefined,
}));
setPatients((prev) => {
const all = [...prev, ...mapped];
const unique = Array.from(new Map(all.map(p => [p.id, p])).values());
return unique;
});
if (mapped.length === 0) setHasNext(false); // parar carregamento
else setPage(prev => prev + 1);
} catch (e: any) {
setError(e?.message || "Erro ao buscar pacientes");
} finally {
@ -311,33 +315,23 @@ export default function PacientesPage() {
<div className="text-red-600">{patientDetails.error}</div>
) : (
<div className="space-y-2 text-left">
<div>
<strong>Nome:</strong> {patientDetails.nome}
</div>
<div>
<strong>Telefone:</strong> {patientDetails?.contato?.celular ?? patientDetails?.contato?.telefone1 ?? patientDetails?.telefone ?? ""}
</div>
<div>
<strong>Cidade:</strong> {patientDetails?.endereco?.cidade ?? patientDetails?.cidade ?? ""}
</div>
<div>
<strong>Estado:</strong> {patientDetails?.endereco?.estado ?? patientDetails?.estado ?? ""}
</div>
<div>
<strong>Convênio:</strong> {patientDetails.convenio ?? ""}
</div>
<div>
<strong>VIP:</strong> {patientDetails.vip ? "Sim" : "Não"}
</div>
<div>
<strong>Status:</strong> {patientDetails.status ?? ""}
</div>
<div>
<strong>Último atendimento:</strong> {patientDetails.ultimo_atendimento ?? patientDetails.ultimoAtendimento ?? ""}
</div>
<div>
<strong>Próximo atendimento:</strong> {patientDetails.proximo_atendimento ?? patientDetails.proximoAtendimento ?? ""}
</div>
<p><strong>Nome:</strong> {patientDetails.full_name}</p>
<p><strong>CPF:</strong> {patientDetails.cpf}</p>
<p><strong>Email:</strong> {patientDetails.email}</p>
<p><strong>Telefone:</strong> {patientDetails.phone_mobile ?? patientDetails.phone1 ?? patientDetails.phone2 ?? "-"}</p>
<p><strong>Nome social:</strong> {patientDetails.social_name ?? "-"}</p>
<p><strong>Sexo:</strong> {patientDetails.sex ?? "-"}</p>
<p><strong>Tipo sanguíneo:</strong> {patientDetails.blood_type ?? "-"}</p>
<p><strong>Peso:</strong> {patientDetails.weight_kg ?? "-"}{patientDetails.weight_kg ? "kg": ""}</p>
<p><strong>Altura:</strong> {patientDetails.height_m ?? "-"}{patientDetails.height_m ? "m": ""}</p>
<p><strong>IMC:</strong> {patientDetails.bmi ?? "-"}</p>
<p><strong>Endereço:</strong> {patientDetails.street ?? "-"}</p>
<p><strong>Bairro:</strong> {patientDetails.neighborhood ?? "-"}</p>
<p><strong>Cidade:</strong> {patientDetails.city ?? "-"}</p>
<p><strong>Estado:</strong> {patientDetails.state ?? "-"}</p>
<p><strong>CEP:</strong> {patientDetails.cep ?? "-"}</p>
<p><strong>Criado em:</strong> {patientDetails.created_at ?? "-"}</p>
<p><strong>Atualizado em:</strong> {patientDetails.updated_at ?? "-"}</p>
</div>
)}
</AlertDialogDescription>

View File

@ -66,13 +66,13 @@ export default function SecretaryLayout({ children }: PatientLayoutProps) {
const menuItems = [
{
href: "#",
href: "##",
icon: Home,
label: "Dashboard",
// Botão para o dashboard da secretária
},
{
href: "#",
href: "###",
icon: Calendar,
label: "Consultas",
// Botão para página de consultas marcadas

View File

@ -11,13 +11,11 @@ async function login() {
"Content-Type": "application/json",
"apikey": API_KEY, // valor fixo
},
body: JSON.stringify({ email: "hugo@popcode.com.br", password: "hdoria" }),
body: JSON.stringify({ email: "riseup@popcode.com.br", password: "riseup" }),
});
const data = await response.json();
console.log("Resposta da API:", data);
console.log("Token:", data.access_token);
console.log(data.access_token)
// salvar o token do usuário
//localStorage.setItem("token", data.access_token);
tempToken = data.access_token
@ -30,10 +28,12 @@ async function request(endpoint, options = {}) {
//const token = localStorage.getItem("token"); // token do usuário, salvo no login
const token = tempToken;
console.log(`endpoint: ${endpoint}`)
const headers = {
"Content-Type": "application/json",
"apikey": API_KEY, // obrigatório sempre
...(token ? { Authorization: `Bearer ${token}` } : {}), // obrigatório em todas EXCETO login
...(token ? { "Authorization": `Bearer ${token}` } : {}), // obrigatório em todas EXCETO login
...options.headers,
};

View File

@ -2,7 +2,7 @@ import { api } from "./api.mjs";
export const patientsService = {
list: () => api.get("/rest/v1/patients"),
getById: (id) => api.get(`/rest/v1/patients/${id}`),
getById: (id) => api.get(`/rest/v1/patients?id=eq.${id}`),
create: (data) => api.post("/rest/v1/patients", data),
update: (id, data) => api.patch(`/rest/v1/patients/${id}`, data),
delete: (id) => api.delete(`/rest/v1/patients/${id}`),