From 6cd536292e764d6d7eed4b5aa0a365b7d40bf028 Mon Sep 17 00:00:00 2001 From: DaniloSts Date: Sat, 4 Oct 2025 19:17:11 -0300 Subject: [PATCH] =?UTF-8?q?Implementado=20API=20para=20agendar=20com=20m?= =?UTF-8?q?=C3=A9dicos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/patient/schedule/page.tsx | 61 +++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/app/patient/schedule/page.tsx b/app/patient/schedule/page.tsx index cf26044..daa2dee 100644 --- a/app/patient/schedule/page.tsx +++ b/app/patient/schedule/page.tsx @@ -1,7 +1,7 @@ "use client"; import type React from "react"; -import { useState } from "react"; +import { useState, useEffect, useCallback } from "react"; import { useRouter } from "next/navigation"; import { toast } from "sonner"; @@ -17,15 +17,24 @@ import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Textarea } from "@/components/ui/textarea"; import { Calendar, Clock, User } from "lucide-react"; +import { doctorsService } from "services/doctorsApi.mjs"; // Interface para o estado local do formulário (sem alterações) interface AppointmentFormState { - doctorId: string; + id: string; date: string; time: string; observations: string; } +interface Doctor { + id: string; + full_name: string; + specialty: string; + phone_mobile: string; + +} + // --- DADOS MOCKADOS (ALTERAÇÃO 1: Adicionando location e phone) --- const doctors = [ { id: "1", name: "Dr. João Silva", specialty: "Cardiologia", location: "Consultório A - 2º andar", phone: "(11) 3333-4444" }, @@ -37,16 +46,40 @@ const availableTimes = ["09:00", "09:30", "10:00", "10:30", "14:00", "14:30", "1 export default function ScheduleAppointmentPage() { const router = useRouter(); + const [doctors, setDoctors] = useState([]); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + // [SINCRONIZAÇÃO 1 - continuação] - Obtendo a lista de agendamentos existentes const { addAppointment, appointments } = useAppointments(); const [formData, setFormData] = useState({ - doctorId: "", + id: "", date: "", time: "", observations: "", }); + const fetchDoctors = useCallback(async () => { + setLoading(true); + setError(null); + try { + + const data: Doctor[] = await doctorsService.list(); + setDoctors(data || []); + } catch (e: any) { + console.error("Erro ao carregar lista de médicos:", e); + setError("Não foi possível carregar a lista de médicos. Verifique a conexão com a API."); + setDoctors([]); + } finally { + setLoading(false); + } + }, []); + + useEffect(() => { + fetchDoctors(); + }, [fetchDoctors]); + const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData(prevState => ({ ...prevState, [name]: value })); @@ -58,18 +91,18 @@ export default function ScheduleAppointmentPage() { const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); - if (!formData.doctorId || !formData.date || !formData.time) { + if (!formData.id || !formData.date || !formData.time) { toast.error("Por favor, preencha os campos de médico, data e horário."); return; } - const selectedDoctor = doctors.find(doc => doc.id === formData.doctorId); + const selectedDoctor = doctors.find(doc => doc.id === formData.id); if (!selectedDoctor) return; // Validação de conflito (sem alterações, já estava correta) const isConflict = appointments.some( (apt) => - apt.doctorName === selectedDoctor.name && + apt.doctorName === selectedDoctor.full_name && apt.date === formData.date && apt.time === formData.time ); @@ -82,13 +115,13 @@ export default function ScheduleAppointmentPage() { // [ALTERAÇÃO 2] - Utilizando os dados do médico selecionado para location e phone // e removendo os placeholders. addAppointment({ - doctorName: selectedDoctor.name, + doctorName: selectedDoctor.full_name, specialty: selectedDoctor.specialty, date: formData.date, time: formData.time, observations: formData.observations, - location: selectedDoctor.location, // Usando a localização do médico - phone: selectedDoctor.phone, // Usando o telefone do médico + phone: selectedDoctor.phone_mobile, + location: "" }); toast.success("Consulta agendada com sucesso!"); @@ -118,8 +151,8 @@ export default function ScheduleAppointmentPage() {