"use client"; import { useEffect, useState } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { Button } from "@/components/ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Input } from "@/components/ui/input"; import { useToast } from "@/hooks/use-toast"; import { assignRoleToUser, listAssignmentsForPatient } from "@/lib/assignment"; import { listarProfissionais } from "@/lib/api"; type Props = { patientId: string; open: boolean; onClose: () => void; onSaved?: () => void; }; export default function AssignmentForm({ patientId, open, onClose, onSaved }: Props) { const { toast } = useToast(); const [professionals, setProfessionals] = useState([]); const [selectedProfessional, setSelectedProfessional] = useState(null); const [role, setRole] = useState("doctor"); const [loading, setLoading] = useState(false); const [existing, setExisting] = useState([]); useEffect(() => { async function load() { try { const pros = await listarProfissionais(); setProfessionals(pros || []); } catch (e) { console.warn('Erro ao carregar profissionais', e); setProfessionals([]); } try { const a = await listAssignmentsForPatient(patientId); setExisting(a || []); } catch (e) { setExisting([]); } } if (open) load(); }, [open, patientId]); async function handleSave() { if (!selectedProfessional) return toast({ title: 'Selecione um profissional', variant: 'warning' }); setLoading(true); try { await assignRoleToUser({ patient_id: patientId, user_id: selectedProfessional, role }); toast({ title: 'Atribuição criada', variant: 'success' }); onSaved && onSaved(); onClose(); } catch (err: any) { console.error(err); toast({ title: 'Erro ao criar atribuição', description: err?.message }); } finally { setLoading(false); } } return ( { if (!v) onClose(); }}> Atribuir profissional ao paciente
setRole(e.target.value)} />
Ex: doctor, nurse
{existing && existing.length > 0 && (
    {existing.map((it) => (
  • {it.user_id} — {it.role}
  • ))}
)}
); }