import { type Component, createSignal, For, Show } from "solid-js"; import { formatDate } from "@/utils"; import type { Therapy } from "@/api/therapy"; interface TherapyListProps { therapies: Therapy[]; onPendingTherapiesChange: (therapies: Therapy[]) => void; readOnly?: boolean; } const TherapyList: Component = (props) => { const [isAdding, setIsAdding] = createSignal(false); const [editingId, setEditingId] = createSignal(null); const [formData, setFormData] = createSignal({ name: "", dose: "", expDate: "", }); const startAdding = () => { setFormData({ name: "", dose: "", expDate: "" }); setIsAdding(true); }; const startEditing = (therapy: Therapy) => { setFormData({ name: therapy.name, dose: therapy.dose, expDate: therapy.expDate, }); setEditingId(therapy.idTherapy); }; const cancelEdit = () => { setIsAdding(false); setEditingId(null); setFormData({ name: "", dose: "", expDate: "" }); }; const handleSave = () => { const data = formData(); if (!data.name.trim() || !data.dose.trim() || !data.expDate) { alert("Please fill in all therapy fields"); return; } const newTherapies = [...props.therapies]; if (editingId()) { const index = newTherapies.findIndex((t) => t.idTherapy === editingId()); if (index !== -1) { newTherapies[index] = { ...newTherapies[index], name: data.name, dose: data.dose, expDate: data.expDate, }; props.onPendingTherapiesChange(newTherapies); } } else { const newTherapy: Therapy = { idTherapy: Date.now(), name: data.name, dose: data.dose, expDate: data.expDate, consultationId: 0, }; newTherapies.push(newTherapy); props.onPendingTherapiesChange(newTherapies); } cancelEdit(); }; const handleDelete = (therapyId: number) => { if (!confirm("Are you sure you want to delete this therapy?")) return; const newTherapies = props.therapies.filter( (t) => t.idTherapy !== therapyId, ); props.onPendingTherapiesChange(newTherapies); }; return (

Therapies

setFormData({ ...formData(), name: e.currentTarget.value }) } placeholder="e.g., Aspirin" required />
setFormData({ ...formData(), dose: e.currentTarget.value }) } placeholder="e.g., 100mg" required />
setFormData({ ...formData(), expDate: e.currentTarget.value }) } required />
0} fallback={

No therapies assigned yet.

} >
{(therapy) => (

{therapy.name}

Dose: {therapy.dose}
Expires:{" "} {formatDate(therapy.expDate)}
)}
); }; export default TherapyList;