From f736667bfb96e1f8d59e40e044694e4568a21479 Mon Sep 17 00:00:00 2001 From: DaniloSts Date: Wed, 1 Oct 2025 09:49:11 -0300 Subject: [PATCH] CRUD Pacientes com nova API --- app/secretary/pacientes/[id]/editar/page.tsx | 27 ++++++----- app/secretary/pacientes/novo/page.tsx | 50 +++++++++++++++----- services/api.mjs | 2 +- 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/app/secretary/pacientes/[id]/editar/page.tsx b/app/secretary/pacientes/[id]/editar/page.tsx index b9881e2..00f11fe 100644 --- a/app/secretary/pacientes/[id]/editar/page.tsx +++ b/app/secretary/pacientes/[id]/editar/page.tsx @@ -228,16 +228,21 @@ export default function EditarPacientePage() { cep: formData.cep || null, }; - console.log(payload) - console.log(JSON.stringify(payload)) - try { - const res = await patientsService.update(patientId, JSON.stringify(payload)); - console.log(res[0]) + await patientsService.update(patientId, payload); + toast({ + title: "Sucesso", + description: "Paciente atualizado com sucesso", + variant: "default" + }); router.push("/secretary/pacientes"); } catch (err: any) { - toast({ title: "Erro", description: err?.message || "Não foi possível atualizar o paciente" }); - console.log("deu ruim") + console.error("Erro ao atualizar paciente:", err); + toast({ + title: "Erro", + description: err?.message || "Não foi possível atualizar o paciente", + variant: "destructive" + }); } }; @@ -472,13 +477,13 @@ export default function EditarPacientePage() {
- - handleInputChange("email", e.target.value)} /> + + handleInputChange("email", e.target.value)} required/>
- - handleInputChange("phoneMobile", e.target.value)} placeholder="(00) 00000-0000" /> + + handleInputChange("phoneMobile", e.target.value)} placeholder="(00) 00000-0000" required/>
diff --git a/app/secretary/pacientes/novo/page.tsx b/app/secretary/pacientes/novo/page.tsx index 892cdbc..0da8445 100644 --- a/app/secretary/pacientes/novo/page.tsx +++ b/app/secretary/pacientes/novo/page.tsx @@ -32,6 +32,27 @@ export default function NovoPacientePage() { setAnexos(anexos.filter((_, i) => i !== index)); }; + + const cleanNumber = (value: string): string => value.replace(/\D/g, ''); + + const formatCPF = (value: string): string => { + const cleaned = cleanNumber(value).substring(0, 11); + return cleaned.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/, '$1.$2.$3-$4'); + }; + + const formatCEP = (value: string): string => { + const cleaned = cleanNumber(value).substring(0, 8); + return cleaned.replace(/(\d{5})(\d{3})/, '$1-$2'); + }; + + const formatPhoneMobile = (value: string): string => { + const cleaned = cleanNumber(value).substring(0, 11); + if (cleaned.length > 10) { + return cleaned.replace(/(\d{2})(\d{5})(\d{4})/, '+55 ($1) $2-$3'); + } + return cleaned.replace(/(\d{2})(\d{4})(\d{4})/, '+55 ($1) $2-$3'); + }; + const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (isLoading) return; @@ -42,15 +63,15 @@ export default function NovoPacientePage() { const apiPayload = { full_name: (formData.get("nome") as string) || "", // obrigatório social_name: (formData.get("nomeSocial") as string) || undefined, - cpf: (formData.get("cpf") as string) || "", // obrigatório + cpf: (formatCPF(formData.get("cpf") as string)) || "", // obrigatório email: (formData.get("email") as string) || "", // obrigatório - phone_mobile: (formData.get("celular") as string) || "", // obrigatório + phone_mobile: (formatPhoneMobile(formData.get("celular") as string)) || "", // obrigatório birth_date: formData.get("dataNascimento") ? new Date(formData.get("dataNascimento") as string) : undefined, sex: (formData.get("sexo") as string) || undefined, blood_type: (formData.get("tipoSanguineo") as string) || undefined, weight_kg: formData.get("peso") ? parseFloat(formData.get("peso") as string) : undefined, height_m: formData.get("altura") ? parseFloat(formData.get("altura") as string) : undefined, - cep: (formData.get("cep") as string) || undefined, + cep: (formatCEP(formData.get("cep") as string)) || undefined, street: (formData.get("endereco") as string) || undefined, number: (formData.get("numero") as string) || undefined, complement: (formData.get("complemento") as string) || undefined, @@ -59,6 +80,10 @@ export default function NovoPacientePage() { state: (formData.get("estado") as string) || undefined, }; + console.log(apiPayload.email) + console.log(apiPayload.cep) + console.log(apiPayload.phone_mobile) + const errors: string[] = []; const fullName = apiPayload.full_name?.trim() || ""; if (!fullName || fullName.length < 2 || fullName.length > 255) { @@ -96,6 +121,7 @@ export default function NovoPacientePage() { } if (errors.length) { toast({ title: "Corrija os campos", description: errors[0] }); + console.log("campos errados") setIsLoading(false); return; } @@ -200,23 +226,23 @@ export default function NovoPacientePage() {
- +
- +