"use client"; import { useState } from "react"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { Plus, Pencil, Trash2, Check, X } from "lucide-react"; interface Profile { id: string; name: string; molecule: string | null; defaultDose: string | null; unit: string; intervalHours: number; minIntervalHours: number | null; isPreset: boolean; } const inputCls = "w-full px-3 py-2.5 rounded-lg border border-slate-200 dark:border-slate-700 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-400 bg-white dark:bg-slate-800 dark:text-slate-100"; const UNITS = ["mL", "mg", "gouttes", "sachet", "comprimé", "mL/kg", "mg/kg"]; const emptyForm = () => ({ name: "", molecule: "", defaultDose: "", unit: "mL", intervalHours: "6", minIntervalHours: "" }); export default function MedicationsPage() { const qc = useQueryClient(); const [showForm, setShowForm] = useState(false); const [editingId, setEditingId] = useState(null); const [form, setForm] = useState(emptyForm()); const [saving, setSaving] = useState(false); const { data: profiles = [], isLoading } = useQuery({ queryKey: ["med-profiles"], queryFn: () => fetch("/api/medication-profiles").then((r) => r.json()), }); function startEdit(p: Profile) { setEditingId(p.id); setForm({ name: p.name, molecule: p.molecule ?? "", defaultDose: p.defaultDose ?? "", unit: p.unit, intervalHours: String(p.intervalHours), minIntervalHours: p.minIntervalHours ? String(p.minIntervalHours) : "", }); setShowForm(false); } function cancelEdit() { setEditingId(null); setForm(emptyForm()); } async function handleCreate(e: React.FormEvent) { e.preventDefault(); setSaving(true); await fetch("/api/medication-profiles", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: form.name, molecule: form.molecule || null, defaultDose: form.defaultDose || null, unit: form.unit, intervalHours: form.intervalHours, minIntervalHours: form.minIntervalHours || null, }), }); qc.invalidateQueries({ queryKey: ["med-profiles"] }); setForm(emptyForm()); setShowForm(false); setSaving(false); } async function handleUpdate(e: React.FormEvent) { e.preventDefault(); if (!editingId) return; setSaving(true); await fetch(`/api/medication-profiles/${editingId}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: form.name, molecule: form.molecule || null, defaultDose: form.defaultDose || null, unit: form.unit, intervalHours: form.intervalHours, minIntervalHours: form.minIntervalHours || null, }), }); qc.invalidateQueries({ queryKey: ["med-profiles"] }); cancelEdit(); setSaving(false); } async function handleDelete(id: string, name: string) { if (!confirm(`Supprimer ${name} ?`)) return; await fetch(`/api/medication-profiles/${id}`, { method: "DELETE" }); qc.invalidateQueries({ queryKey: ["med-profiles"] }); } const ProfileForm = ({ onSubmit, onCancel }: { onSubmit: (e: React.FormEvent) => void; onCancel: () => void }) => (
setForm((f) => ({ ...f, name: e.target.value }))} required className={inputCls} placeholder="Doliprane" />
setForm((f) => ({ ...f, molecule: e.target.value }))} className={inputCls} placeholder="Paracétamol" />
setForm((f) => ({ ...f, defaultDose: e.target.value }))} className={inputCls} placeholder="2.4" />
setForm((f) => ({ ...f, intervalHours: e.target.value }))} required className={inputCls} placeholder="6" min="0.5" step="0.5" />
setForm((f) => ({ ...f, minIntervalHours: e.target.value }))} className={inputCls} placeholder="4 (optionnel)" min="0.5" step="0.5" />
); return (

Médicaments

Profils et intervalles de prise

{!showForm && !editingId && ( )}
{showForm &&
{ setShowForm(false); setForm(emptyForm()); }} />
} {isLoading && (
)}
{profiles.map((p) => (
{editingId === p.id ? ( ) : (

{p.name}

{p.isPreset && ( preset )}
{p.molecule &&

{p.molecule}

}
{p.defaultDose && ( {p.defaultDose} {p.unit} )} {p.intervalHours}h {p.minIntervalHours && ( urgence {p.minIntervalHours}h )}
)}
))}
{!isLoading && profiles.length === 0 && !showForm && (

Aucun profil médicament

)}
); }