"use client"; import { useParams, useRouter } from "next/navigation"; import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Checkbox } from "@/components/ui/checkbox"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Dialog, DialogContent, DialogTrigger } from "@/components/ui/dialog"; import { Calendar } from "@/components/ui/calendar"; import { CalendarIcon } from "lucide-react"; import { format } from "date-fns"; import TiptapEditor from "@/components/ui/tiptap-editor"; import { Skeleton } from "@/components/ui/skeleton"; import { reportsApi } from "@/services/reportsApi.mjs"; import Sidebar from "@/components/Sidebar"; export default function EditarLaudoPage() { const router = useRouter(); const params = useParams(); const patientId = params.id as string; const laudoId = params.laudoId as string; const [formData, setFormData] = useState({}); const [loading, setLoading] = useState(true); const [isSubmitting, setIsSubmitting] = useState(false); const [isDatePickerOpen, setIsDatePickerOpen] = useState(false); useEffect(() => { if (laudoId) { setLoading(true); reportsApi.getReportById(laudoId) .then((data: any) => { console.log("Fetched report data:", data); // The API now returns an array, get the first element const reportData = Array.isArray(data) && data.length > 0 ? data[0] : null; if (reportData) { setFormData({ ...reportData, due_at: reportData.due_at ? new Date(reportData.due_at) : null, }); } }) .catch(error => { console.error("Failed to fetch report details:", error); // Here you could add a toast notification to inform the user }) .finally(() => { setLoading(false); }); } else { // If there's no laudoId, we shouldn't be in a loading state. setLoading(false); } }, [laudoId]); const handleInputChange = (e: React.ChangeEvent) => { const { id, value } = e.target; setFormData((prev: any) => ({ ...prev, [id]: value })); }; const handleSelectChange = (id: string, value: string) => { setFormData((prev: any) => ({ ...prev, [id]: value })); }; const handleCheckboxChange = (id: string, checked: boolean) => { setFormData((prev: any) => ({ ...prev, [id]: checked })); }; const handleDateChange = (date: Date | undefined) => { console.log("Date selected:", date); if (date) { setFormData((prev: any) => ({ ...prev, due_at: date })); } }; const handleDateSelect = (date: Date | undefined) => { handleDateChange(date); setIsDatePickerOpen(false); // Close the dialog after selection }; const handleEditorChange = (html: string, json: object) => { setFormData((prev: any) => ({ ...prev, content_html: html, content_json: json })); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); try { const { id, patient_id, created_at, updated_at, created_by, updated_by, ...updateData } = formData; await reportsApi.updateReport(laudoId, updateData); // toast({ title: "Laudo atualizado com sucesso!" }); router.push(`/doctor/medicos/${patientId}/laudos`); } catch (error) { console.error("Failed to update laudo", error); // toast({ title: "Erro ao atualizar laudo", variant: "destructive" }); } finally { setIsSubmitting(false); } }; if (loading) { return (
) } return (
Editar Laudo - {formData.order_number}