import { useEffect, useState, type FC } from 'react'; import { Box, Button, Card, CardContent, CardActions, Chip, CircularProgress, Dialog, DialogActions, DialogContent, DialogTitle, Alert, IconButton, List, ListItem, ListItemText, MenuItem, Stack, TextField, Typography, } from '@mui/material'; import EditIcon from '@mui/icons-material/Edit'; import DeleteIcon from '@mui/icons-material/Delete'; import AddIcon from '@mui/icons-material/Add'; import { useMedicationStore, useAuthStore } from '../../store/useStore'; import type { Medication, CreateMedicationRequest, UpdateMedicationRequest, } from '../../types/api'; import { DoseLogger } from './DoseLogger'; const ROUTES = ['oral', 'topical', 'injection', 'inhalation', 'other'] as const; const emptyCreate: CreateMedicationRequest = { name: '', dosage: '', frequency: '', route: 'oral', profile_id: 'default', }; export const MedicationManager: FC = () => { const { medications, isLoading, error, loadMedications, createMedication, updateMedication, deleteMedication, clearError, } = useMedicationStore(); const user = useAuthStore((s) => s.user); const [createOpen, setCreateOpen] = useState(false); const [createForm, setCreateForm] = useState(emptyCreate); const [editTarget, setEditTarget] = useState(null); const [editForm, setEditForm] = useState({}); const [deleteTarget, setDeleteTarget] = useState(null); const [saving, setSaving] = useState(false); useEffect(() => { loadMedications(); }, [loadMedications]); const openCreate = () => { // profile_id is deterministic: profile_. The backend auto-creates // this profile on register, so the id always resolves to a real profile. const profileId = user?.profile_id ?? `profile_${user?.user_id ?? 'default'}`; setCreateForm({ ...emptyCreate, profile_id: profileId, }); setCreateOpen(true); }; const submitCreate = async () => { setSaving(true); try { await createMedication(createForm); setCreateOpen(false); } catch { /* error surfaced via store */ } finally { setSaving(false); } }; const openEdit = (med: Medication) => { setEditTarget(med); setEditForm({ name: med.name, dosage: med.dosage, frequency: med.frequency, instructions: med.instructions, active: med.active, }); }; const submitEdit = async () => { if (!editTarget?.medication_id) return; setSaving(true); try { await updateMedication(editTarget.medication_id, editForm); setEditTarget(null); } catch { /* error surfaced via store */ } finally { setSaving(false); } }; const confirmDelete = async () => { if (!deleteTarget?.medication_id) return; setSaving(true); try { await deleteMedication(deleteTarget.medication_id); setDeleteTarget(null); } catch { /* error surfaced via store */ } finally { setSaving(false); } }; return ( Medications {error && ( {error} )} {isLoading ? ( ) : medications.length === 0 ? ( No medications yet — add one. ) : ( {medications.map((med) => ( {med.name} {' '} {med.dosage} · {med.frequency} {med.instructions && ( {med.instructions} )} {med.medication_id && } openEdit(med)} aria-label="edit"> setDeleteTarget(med)} aria-label="delete" color="error" > ))} )} {/* Create dialog */} setCreateOpen(false)} fullWidth maxWidth="sm"> Add medication setCreateForm({ ...createForm, name: e.target.value })} /> setCreateForm({ ...createForm, dosage: e.target.value })} /> setCreateForm({ ...createForm, frequency: e.target.value })} /> setCreateForm({ ...createForm, route: e.target.value as CreateMedicationRequest['route'] }) } > {ROUTES.map((r) => ( {r} ))} setCreateForm({ ...createForm, instructions: e.target.value || undefined }) } /> {/* Edit dialog */} setEditTarget(null)} fullWidth maxWidth="sm" > Edit medication setEditForm({ ...editForm, name: e.target.value })} /> setEditForm({ ...editForm, dosage: e.target.value })} /> setEditForm({ ...editForm, frequency: e.target.value })} /> setEditForm({ ...editForm, instructions: e.target.value })} /> setEditForm({ ...editForm, active: e.target.value === 'active' })} > active inactive {/* Delete confirm */} setDeleteTarget(null)}> Delete medication? Remove {deleteTarget?.name}? This cannot be undone. ); }; export default MedicationManager;