import { type Component, createSignal, For, Show } from "solid-js"; import { formatDate } from "@/utils"; import { type Therapy, therapyApi, type UpdateTherapyRequest, } from "@/api/therapy"; interface ExistingTherapyListProps { consultationId: number; therapies: Therapy[]; onTherapiesChange: (therapies: Therapy[]) => void; readOnly?: boolean; } const ExistingTherapyList: Component = (props) => { const [editingId, setEditingId] = createSignal(null); const [formData, setFormData] = createSignal({ name: "", dose: "", expDate: "", }); const startEditing = (therapy: Therapy) => { setFormData({ name: therapy.name, dose: therapy.dose, expDate: therapy.expDate, }); setEditingId(therapy.idTherapy); }; const cancelEdit = () => { setEditingId(null); setFormData({ name: "", dose: "", expDate: "" }); }; const handleUpdate = async () => { const data = formData(); if (!data.name.trim() || !data.dose.trim() || !data.expDate) { alert("Please fill in all therapy fields"); return; } try { const updateData: UpdateTherapyRequest = { name: data.name, dose: data.dose, expDate: data.expDate, }; await therapyApi.updateTherapy(editingId()!, updateData); const updated = props.therapies.map((t) => t.idTherapy === editingId() ? { ...t, name: data.name, dose: data.dose, expDate: data.expDate } : t, ); props.onTherapiesChange(updated); cancelEdit(); } catch (error: any) { alert(error.message || "Failed to update therapy"); } }; const handleDelete = async (therapyId: number) => { if (!confirm("Are you sure you want to delete this therapy?")) return; try { await therapyApi.deleteTherapy(therapyId); const filtered = props.therapies.filter((t) => t.idTherapy !== therapyId); props.onTherapiesChange(filtered); } catch (error: any) { alert(error.message || "Failed to delete therapy"); } }; return ( 0} fallback={

No existing therapies.

} >
{(therapy) => (

{therapy.name}

Dose: {therapy.dose}
Expires:{" "} {formatDate(therapy.expDate)}
} >
setFormData({ ...formData(), name: e.currentTarget.value, }) } required />
setFormData({ ...formData(), dose: e.currentTarget.value, }) } required />
setFormData({ ...formData(), expDate: e.currentTarget.value, }) } required />
)} ); }; export default ExistingTherapyList;